注册中心 —— SpringCloud Netflix Eureka

打印 上一主题 下一主题

主题 875|帖子 875|积分 2625

Eureka 简介

Eureka 是一个基于 REST 的服务发现组件,SpringCloud 将它集成在其子项目 spring-cloud-netflix 中,以实现 SpringCloud 的服务注册与发现,同时提供了负载均衡、故障转移等能力,目前 Eureka2.0 已经不再维护,故不推荐使用
Eureka 有两种角色组件:

  • Eureka Server:服务注册中心组件,提供了服务的注册与发现的接口
  • Eureka Client:各种微服务,把自身的服务实例注册到 Eureka Server 中,也可通过 Eureka Server 获取服务列表,消费服务
微服务客户端在 Eureka 上注册,然后每隔 30 秒发送心跳来更新它们的租约。如果客户端不能多次续订租约,就将在大约 90 秒内从服务器注册表中剔除。注册信息和更新被复制到集群中的所有 Eureka 节点,来自任何区域的客户端都可以查找注册表信息(每30秒发生一次)来定位它们的服务并进行远程调用

搭建 Eureka 注册中心

创建 eureka-server 项目,引入依赖,本项目基于 SpringBoot 2.3.1,SpringCloud Hoxton.SR12
  1. <dependencies>
  2.    
  3.     <dependency>
  4.         <groupId>org.springframework.cloud</groupId>
  5.         <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  6.     </dependency>
  7.    
  8.     ...
  9. <dependencies>
复制代码
在启动类上添加 @EnaleEurekaServer 注解,启用 Euerka 注册中心功能
  1. @EnableEurekaServer
  2. @SpringBootApplication
  3. public class EurekaServerApplication {
  4.     public static void main(String[] args) {
  5.         SpringApplication.run(EurekaServerApplication.class, args);
  6.     }
  7. }
复制代码
在配置文件添加 Eureka 服务端的配置
  1. server:
  2.   port: 8001 # 指定运行端口
  3. spring:
  4.   application:
  5.     name: eureka-server # 指定服务名称
  6. eureka:
  7.   instance:
  8.     hostname: localhost # 指定主机名称
  9.   client:
  10.     fetch-registry: false # 指定能否从注册中心获取服务
  11.     register-with-eureka: false # 指定是否将服务注册到注册中心
复制代码
运行 main 方法启动服务,在浏览器中访问 http://localhost:8001/ 便可以看到 Eureka 注册中心的界面
创建 eureka-client 项目,引入依赖
  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6.     <groupId>org.springframework.cloud</groupId>
  7.     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  8. </dependency>
复制代码
在启动类上添加 @EnableDiscoveryClient 注解,表明是一个 Eureka 客户端
  1. @EnableDiscoveryClient
  2. @SpringBootApplication
  3. public class EurekaClientApplication {
  4.     public static void main(String[] args) {
  5.         SpringApplication.run(EurekaClientApplication.class, args);
  6.     }
  7. }
复制代码
在配置文件添加 Eureka 客户端的配置
  1. server:
  2.   port: 8101 # 指定运行端口
  3. spring:
  4.   application:
  5.     name: eureka-client # 指定服务名称
  6. eureka:
  7.   client:
  8.     fetch-registry: true # 指定能否从注册中心获取服务
  9.     register-with-eureka: true # 指定是否将服务注册到注册中心
  10.     service-url:
  11.       defaultZone: http://localhost:8001/eureka
复制代码
运行 main 方法,启动 eureka-client 项目,刷新 http://localhost:8001/ 页面,即可看到 cureka-client 已经注入 Eurcka 服务

搭建 Eureka 注册中心集群

由于所有服务都会注册到注册中心,服务之间的调用都是通过从注册中心获取服务列表来调用的。注册中心一旦宕机,所有服务调用都会出现问题,因此需要多个注册中心组成集群来提供服务
创建两个 eureka-server 项目,eureka-server-1 项目的配置文件如下:
  1. server:
  2.   port: 8002 # 指定运行端口
  3. spring:
  4.   application:
  5.     name: eureka-server-1 # 指定服务名称
  6. eureka:
  7.   instance:
  8.     hostname: localhost # 指定主机名称
  9.   client:
  10.     fetch-registry: true # 指定能否从注册中心获取服务
  11.     register-with-eureka: true # 指定是否将服务注册到注册中心
  12.     service-url:
  13.       defaultZone: http://localhost:8003/eureka/
复制代码
eureka-server-2 项目的配置文件如下:
  1. server:
  2.   port: 8003 # 指定运行端口
  3. spring:
  4.   application:
  5.     name: eureka-server-1 # 指定服务名称
  6. eureka:
  7.   instance:
  8.     hostname: localhost # 指定主机名称
  9.   client:
  10.     fetch-registry: true # 指定能否从注册中心获取服务
  11.     register-with-eureka: true # 指定是否将服务注册到注册中心
  12.     service-url:
  13.       defaultZone: http://localhost:8002/eureka/
复制代码
通过两个注册中心互相注册,搭建注册中心的双节点集群。分别启动项目,查看 http://localhost:8001/ 和  http://localhost:8002/,可以看到两个注册中心已经分别注册了


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

前进之路

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表