01 eureka注册中心eureka服务注册中心_第1页
01 eureka注册中心eureka服务注册中心_第2页
01 eureka注册中心eureka服务注册中心_第3页
01 eureka注册中心eureka服务注册中心_第4页
01 eureka注册中心eureka服务注册中心_第5页
已阅读5页,还剩37页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Eureka服务注册中心服务注册中心是服务实现服务化管理的核心组件,类似于目录服务的作用,主要用来存储服务信息,譬如提供者 url 串、路由信息等。服务注册中心是微服务架构中最基础的设施之一。在微服务架构流行之前,注册中心就已经开始出现在分布式架构的系统中。Dubbo 是一个在国内比较流行的分布式框架,被大量的中小型互联网公司所采用,它提供了比较完善的服务治理功能,而服务 治理的实现主要依靠的就是注册中心。什么是注册中心注册中心可以说是微服务架构中的“”,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用。举个现实生活

2、中的例子,比如说,我们手机中的的两个使用场景:当我想给张三打电话时,那我需要在中按照名字找到张三,然后就可以找到他的手机号拨 打电话。 服务发现总结:服务注册中心的作用就是服务的注册和服务的发现。常见的注册中心Netflix EurekaAlibaba Nacos HashiCorp Consul Apache ZooKeeper CoreOS EtcdCNCF CoreDNS李四办了手机号并把手机号告诉了我,我把李四的号码存进,后续,我就可以从找到他。 服务注册 ?什么角色特性EurekaNacosConsulZookeeperCAPAPCP + APCPCP健康检查Client BeatT

3、CP/HTTP/MYSQL/Client BeatTCP/HTTP/gRPC/CmdKeep Alive雪崩保护有有无无自动注销实例支持支持不支持支持访问协议HTTPHTTP/DNSHTTP/DNSTCP支持支持支持支持支持多数据中心支持支持支持不支持跨注册中心同步不支持支持支持不支持SpringCloud集成支持支持支持支持为什么需要注册中心了解了什么是注册中心,那么我们继续谈谈,为什么需要注册中心。在分布式系统中,我们不仅仅 是需要在注册中心找到服务和服务地址的映射关系这么简单,我们还需要考虑更多更复杂的问题:服务注册后,如何被及时发现服务宕机后,如何及时下线服务如何有效的水平扩展服务发现

4、时,如何进行路由服务异常时,如何进行降级注册中心如何实现自身的高可用这些问题的解决都依赖于注册中心。简单看,注册中心的功能有点类似于 DNS 服务器或者负载均衡器,而实际上,注册中心作为微服务的基础组件,可能要更加复杂,也需要更多的灵活性和时效性。所 以我们还需要学习更多 Spring Cloud 微服务组件协同完成应用开发。注册中心解决了什么问题服务管理服务的依赖关系管理什么是 Eureka 注册中心Eureka 是Netflix 开发的服务发现组件,本身是一个基于 REST 的服务。Spring Cloud 将它集成在其子项目 Spring Cloud Netflix 中,实现 Sprin

5、g Cloud 的服务注册与发现,同时还提供了负载均衡、故障转移等能力。Eureka 注册中心三种角色Eureka Server通过 Register、Get、Renew 等接口提供服务的注册和发现。Application Service(Service Provider)服务提供方,把自身的服务实例注册到 Eureka Server 中。Application Client(Service Consumer)服务调用方,通过 Eureka Server 获取服务列表,消费服务。Eureka 入门案例创建项目我们创建聚合项目来讲解 Eureka,首先创建一个 pom 父工程。添加依赖pom.x

6、ml1 2 5 4.0.0 678com.example910 eureka-demo11 12 1.0-SNAPSHOT 1314 15 16 17 org.springframework.boot18 spring-boot-starter-parent19 2.2.2.RELEASE20 2122注册中心 eureka-server在刚才的父工程下创建 eureka-server 注册中心的项目。创建项目27 28 29 Hoxton.SR130 3132 33 34 35 36 37 org.springframework.cloud38 spring-cloud-dependenci

7、es39 $spring-cloud.version40 pom41 import42 43 44 4546添加依赖pom.xml1 23 配置文件application.yml1 spring:2 application:3 name: eureka-server # 应用名称 45 # 端口 6 server:7port: 8761854.0.0 67 com.example8 eureka-server9 1.0-SNAPSHOT 101112 13 com.example14 eureka-demo15 1.0-SNAPSHOT16 171819 20 21 22 org.spring

8、framework.cloud23 spring-cloud-starter-netflix-eureka-server24 25 26 27 org.springframework.boot28 spring-boot-starter-web29 3031 32 33 org.springframework.boot34 spring-boot-starter-test35 test36 37 38 org.junit.vintage39 junit-vintage-engine40 41 42 43 4445此时如果直接启动项目是会报错的,错误信息:com.sun.jersey.api.c

9、lient.ClientHandlerException: .ConnectException: Connection refused: connect ,这是因为 Eureka 默认开启了将自己注册至注册中心和从注册中心获取服务注册信息的配置,如果该应用的角色是注册中心并是单节点的话,要关闭这两个配置项。启动类EurekaServerApplication.java访问访问: http:/localhost:8761/1package com.example; 23 import org.springframework.boot.SpringApplication;4 imp

10、ort org.springframework.boot.autoconfigure.SpringBootApplication;5 import flix.eureka.server.EnableEurekaServer; 67 SpringBootApplication8 / 开启 EurekaServer 注解 9 EnableEurekaServer10 public class EurekaServerApplication 1112 public static void main(String args) 13 Spring

11、Application.run(EurekaServerApplication.class, args); 14 15169 # 配置 Eureka Server 注册中心 10 eureka:11 instance:12 hostname: localhost# 主机名,不配置的时候将根据操作系统的主机名来获取 13 client:14 register-with-eureka: false# 是否将自己注册到注册中心,默认为 true15 fetch-registry: false# 是否从注册中心获取服务注册信息,默认为 true16 service-url:# 注册中心对外暴露的注册地

12、址 17 defaultZone: http:/$eureka.instance.hostname:$server.port/eureka/高可用 Eureka 注册中心注册中心 eureka-server创建项目在刚才的父工程下再创建一个 eureka-server02注册中心的项目,如果是多机器部署不用修改端口,通过 IP 区分服务,如果在一台机器上演示需要修改端口区分服务。添加依赖pom.xml1 235 4.0.0 67 com.example8 eureka-server029 1.0-SNAPSHOT 101112 13 com.example14 eureka-demo15 1.

13、0-SNAPSHOT16 171819 20 21 22 org.springframework.cloud23 spring-cloud-starter-netflix-eureka-server24 25 26 27 org.springframework.boot28 spring-boot-starter-web29 3031 32 33 org.springframework.boot34 spring-boot-starter-test35 test36 37 38 org.junit.vintage39 junit-vintage-engine40 41 42 43 4445eu

14、reka-server02 的 application.yml启动类启动类不变,启动两个 server。访问访问: http:/localhost:8761/ 或者 http:/localhost:8762/ 都出现如下图说明互相注册成功。显示 IP + 端口1 spring:2 application:3 name: eureka-server # 应用名称(集群下相同) 45 # 端口 6 server:7port: 876289 # 配置 Eureka Server 注册中心 10 eureka:11 instance:12 hostname: eureka02# 主机名,不配置的时候将

15、根据操作系统的主机名来获取 13 client:14 # 设置服务注册中心地址,指向另一个注册中心 15 service-url:# 注册中心对外暴露的注册地址 16 defaultZone: http:/localhost:8761/eureka/6server:7port: 876189 # 配置 Eureka Server 注册中心 10 eureka:11 instance:12 hostname: eureka01# 主机名,不配置的时候将根据操作系统的主机名来获取 13 client:14 # 设置服务注册中心地址,指向另一个注册中心 15 service-url:# 注册中心对外

16、暴露的注册地址 16 defaultZone: http:/localhost:8762/eureka/一个普通的 Netflix Eureka 实例注册的 ID 等于其主机名(即,每个主机仅提供一项服务)。Spring Cloud Eureka 提供了合理的默认值,定义如下:$spring.cloud.client.hostname:$:$spring.application.i nstance_id:$server.port ,也就是:主机名:应用名:应用端口。我们也可以可以自定义进行修改:服务提供者 service-provider创建项目在

17、刚才的父工程下创建一个 service-provider服务提供者的项目。1 eureka:2 instance:3 prefer-ip-address: true# 是否使用 ip 地址注册 4 instance-id: $spring.cloud.client.ip-address:$server.port # ip:port添加依赖pom.xml1 23 配置文件application.yml1 spring:2 application:54.0.0 67 com.example8 service-provider9 1.0-SNAPSHOT 101112 13 com.example1

18、4 eureka-demo15 1.0-SNAPSHOT16 171819 20 21 22 org.springframework.cloud23 spring-cloud-starter-netflix-eureka-client24 25 26 27 org.springframework.boot28 spring-boot-starter-web29 30 31 32 jectlombok33 lombok34 provided35 3637 38 39 org.springframework.boot40 spring-boot-starter-test41 test

19、42 43 44 org.junit.vintage45 junit-vintage-engine46 47 48 49 5051实体类User.java编写服务UserService.java1package com.example.service; 23import com.example.pojo.User; 45import java.util.List; 67/*8* 用户服务 9*/1package com.example.pojo; 23 import lombok.AllArgsConstructor;4 import lombok.Data;5 import lombok.N

20、oArgsConstructor; 67import java.io.Serializable; 89 Data10 NoArgsConstructor11 AllArgsConstructor12 public class User implements Serializable 1314 private Integer id;15 private String username;16 private Integer age; 17183name: service-provider # 应用名称(集群下相同) 45 # 端口 6 server:7port: 707089 # 配置 Eurek

21、a Server 注册中心 10 eureka:11 instance:12 prefer-ip-address: true# 是否使用 ip 地址注册 13 instance-id: $spring.cloud.client.ip-address:$server.port # ip:port14 client:15 service-url:# 设置服务注册中心地址 16 defaultZone: http:/localhost:8761/eureka/,http:/localhost:8762/eureka/UserServiceImpl.java控制层UserController.java

22、1package com.example.controller; 23 import com.example.pojo.User;4 import com.example.service.UserService;5 import org.springframework.beans.factory.annotation.Autowired;6 import org.springframework.web.bind.annotation.GetMapping;7 import org.springframework.web.bind.annotation.RequestMapping;1packa

23、ge com.example.service.impl; 23 import com.example.pojo.User;4 import com.example.service.UserService;5 import org.springframework.stereotype.Service; 67 import java.util.Arrays;8 import java.util.List; 910/*11* 用户服务 12*/13 Service14 public class UserServiceImpl implements UserService 1516/*17* 查询用户

24、列表 18*19* return20*/21 Override22 public List selectUserList() 23 return Arrays.asList(24new User(1, 张三, 20),25new User(2, 李四, 21),26new User(3, 王五, 22) 27);28293010public interface UserService 1112/*13* 查询用户列表 14*15* return16*/17List selectUserList(); 1819启动类ServiceProviderApplication.java注册中心访问注册中

25、心,可以看到用户服务已经注册至注册中心。1package com.example; 23 import org.springframework.boot.SpringApplication;4 import org.springframework.boot.autoconfigure.SpringBootApplication; 56 SpringBootApplication7 / 开启 EurekaClient 注解,目前版本如果配置了 Eureka 注册中心,默认会开启该注解 8 /EnableEurekaClient9 public class ServiceProviderAppli

26、cation 1011 public static void main(String args) 12 SpringApplication.run(ServiceProviderApplication.class, args); 131415该项目我们可以通过单元测试进行测试,也可以直接通过 url 使用 postman 或者浏览器来进行测试。8import org.springframework.web.bind.annotation.RestController; 910import java.util.List; 1112 RestController13 RequestMapping(

27、/user)14 public class UserController 1516 Autowired17 private UserService userService; 1819/*20* 查询用户列表 21*22* return23*/24 GetMapping(/list)25 public List selectUserList() 26 return userService.selectUserList(); 272829服务消费者 service-consumer创建项目在刚才的父工程下创建一个 service-consumer 服务消费者的项目。添加依赖pom.xml1 23

28、5 4.0.0 67 com.example8 service-consumer9 1.0-SNAPSHOT 101112 13 com.example14 eureka-demo15 1.0-SNAPSHOT16 171819 20 21 22 org.springframework.cloud23 spring-cloud-starter-netflix-eureka-client24 25 26 配置文件application.yml实体类User.java1 spring:2 application:3 name: service-consumer # 应用名称 45 # 端口 6 s

29、erver:7port: 808089 # 配置 Eureka Server 注册中心 10 eureka:11 client:12 register-with-eureka: false# 是否将自己注册到注册中心,默认为 true13 registry-fetch-interval-seconds: 10 # 表示 Eureka Client 间隔多久去服务器拉取注册信息,默认为 30 秒 14 service-url:# 设置服务注册中心地址 15 defaultZone: http:/localhost:8761/eureka/,http:/localhost:8762/eureka/

30、27 org.springframework.boot28 spring-boot-starter-web29 30 31 32 jectlombok33 lombok34 provided35 3637 38 39 org.springframework.boot40 spring-boot-starter-test41 test42 43 44 org.junit.vintage45 junit-vintage-engine46 47 48 49 5051消费服务UserService.java对于服务的消费我们这里讲三种实现方式:DiscoveryClient:通过元数据获

31、取服务信息LoadBalancerClient:Ribbon 的负载均衡器LoadBalanced:通过注解开启 Ribbon 的负载均衡器DiscoveryClient1package com.example.service; 23import com.example.pojo.User; 45import java.util.List; 67/*8* 用户管理 9*/10public interface UserService 1112/*13* 查询用户列表 14*15* return16*/17List selectUserList(); 18191package com.exampl

32、e.pojo; 23 import lombok.AllArgsConstructor;4 import lombok.Data;5 import lombok.NoArgsConstructor; 67import java.io.Serializable; 89 Data10 NoArgsConstructor11 AllArgsConstructor12 public class User implements Serializable 1314 private Integer id;15 private String username;16 private Integer age; 1

33、718UserServiceImpl.java1package com.example.service.impl; 23 import com.example.pojo.User;4 import com.example.service.UserService;5 import org.springframework.beans.factory.annotation.Autowired;6 import org.springframework.cloud.client.ServiceInstance;7 import org.springframework.cloud.client.disco

34、very.DiscoveryClient;8 import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;9 import org.springframework.core.ParameterizedTypeReference;10 import org.springframework.http.HttpMethod;11 import org.springframework.http.ResponseEntity;12 import org.springframework.stereotype.Service

35、;13 import org.springframework.util.CollectionUtils;14 import org.springframework.web.client.RestTemplate; 1516import java.util.List; 1718/*19* 用户管理 20*/21 Service22 public class UserServiceImpl implements UserService 2324 Autowired25 private RestTemplate restTemplate; 2627 Autowired28 private Disco

36、veryClient discoveryClient; 2930/*31* 查询用户列表 32*33* return34*/35 Override36 public List selectUserList() 37 return selectUserListByDiscoveryClient(); 383940 private List selectUserListByDiscoveryClient() 41 StringBuffer sb = null; 4243 / 获取服务列表 44 List serviceIds = discoveryClient.getServices();45 i

37、f (CollectionUtils.isEmpty(serviceIds)46 return null;4748 / 根据服务名称获取服务 49 List serviceInstances = discoveryClient.getInstances(service-provider);50 if (CollectionUtils.isEmpty(serviceInstances)51 return null;5253 ServiceInstance si = serviceInstances.get(0);54 sb = new StringBuffer();55 sb.append(ht

38、tp:/ + si.getHost() + : + si.getPort() + /user/list);LoadBalancerClientSpring Boot 不提供任何自动配置的 RestTemplate RestTemplate 。bean,所以需要在启动类中注入UserServiceImpl.java1package com.example.service.impl; 23 import com.example.pojo.User;4 import com.example.service.UserService;5 import org.springframework.beans.

39、factory.annotation.Autowired;6 import org.springframework.cloud.client.ServiceInstance;7 import org.springframework.cloud.client.discovery.DiscoveryClient;8 import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;9 import org.springframework.core.ParameterizedTypeReference;10 import org.springframework.http.HttpMethod;11 import org.springframework.http.ResponseEntity;1package com.example; 23 import org

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论