SpringCloud-day04-Eureka高可用集群配置

5.4Eureka高可用集群配置

在高并发的状况下一个注册中心难以知足,所以通常须要集群配置多台。java

 

咱们再新建两个module  microservice-eureka-server-2002,  microservice-eureka-server-2003,而后配置,最终的配置结果结构如图:mysql

  

 

具体步骤:git

第一步:每一个模块的pom.xml添加以下依赖:github

 1 <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.cloud</groupId>
 4             <artifactId>spring-cloud-starter-eureka-server</artifactId>
 5         </dependency>
 6         <!-- 修改后当即生效,热部署 -->
 7         <dependency>
 8             <groupId>org.springframework</groupId>
 9             <artifactId>springloaded</artifactId>
10         </dependency>
11         <dependency>
12             <groupId>org.springframework.boot</groupId>
13             <artifactId>spring-boot-devtools</artifactId>
14         </dependency>
15     </dependencies>

 

 

第二步:2002  2003的主启动类EurekaServerApplication_2002,EurekaServerApplication_2003复制EurekaServerApplication_2001的修更名称;spring

 

第三步:前面单机的时候 eureka注册中心实例名称 是localhost,如今是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,麻烦,这里有简单办法,直接配置本机hosts,来实现本机域名映射;sql

找到 C:\Windows\System32\drivers\etc  打开hosts,加配置 浏览器

127.0.0.1  eureka2001.wfd360.com网络

127.0.0.1  eureka2002.wfd360.com并发

127.0.0.1  eureka2003.wfd360.comapp

注意:在修改hosts文件时,建议先拷贝出来,修改好后再替换原来的hosts文件。

 

第四步:修改三个项目的application.yml文件,主要是修改 hostname和defaultZone,

2001 的 application.yml文件

 1 server:
 2   port: 2001
 3   context-path: /
 4 
 5 eureka:
 6   instance:
 7     #hostname: localhost #eureka注册中心实例名称 (单机版)
 8     hostname: eureka2001.wfd360.com # 集群
 9   client:
10     register-with-eureka: false     #false 因为该应用为注册中心,因此设置为false,表明不向注册中心注册本身。
11     fetch-registry: false     #false 因为注册中心的职责就是维护服务实例,它并不须要去检索服务,因此也设置为false
12     service-url:
13        #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #(单机)设置与Eureka注册中心交互的地址,查询服务和注册服务用到
14        defaultZone: http://eureka2002.wfd360.com:2002/eureka/,http://eureka2003.wfd360.com:2003/eureka/ # 集群
View Code

 

2002 的 application.yml文件

 1 server:
 2   port: 2002
 3   context-path: /
 4 
 5 eureka:
 6   instance:
 7     #hostname: localhost #eureka注册中心实例名称 (单机版)
 8     hostname: eureka2002.wfd360.com # 集群
 9   client:
10     register-with-eureka: false     #false 因为该应用为注册中心,因此设置为false,表明不向注册中心注册本身。
11     fetch-registry: false     #false 因为注册中心的职责就是维护服务实例,它并不须要去检索服务,因此也设置为false
12     service-url:
13        #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #(单机)设置与Eureka注册中心交互的地址,查询服务和注册服务用到
14        defaultZone: http://eureka2001.wfd360.com:2001/eureka/,http://eureka2003.wfd360.com:2003/eureka/ # 集群
View Code

 

2003 的 application.yml文件

 1 server:
 2   port: 2003
 3   context-path: /
 4 
 5 eureka:
 6   instance:
 7     #hostname: localhost #eureka注册中心实例名称 (单机版)
 8     hostname: eureka2003.wfd360.com # 集群
 9   client:
10     register-with-eureka: false     #false 因为该应用为注册中心,因此设置为false,表明不向注册中心注册本身。
11     fetch-registry: false     #false 因为注册中心的职责就是维护服务实例,它并不须要去检索服务,因此也设置为false
12     service-url:
13        #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #(单机)设置与Eureka注册中心交互的地址,查询服务和注册服务用到
14        defaultZone: http://eureka2001.wfd360.com:2001/eureka/,http://eureka2002.wfd360.com:2002/eureka/ # 集群
View Code

 

第五步:修改服务提供者项目的application.yml,主要修改eureka.client.service-url.defaultZone,修改后的文件以下

 1 server:
 2   port: 1001
 3   context-path: /
 4 
 5 # 数据源配置
 6 spring:
 7   datasource:
 8     type: com.alibaba.druid.pool.DruidDataSource
 9     driver-class-name: com.mysql.jdbc.Driver
10     url: jdbc:mysql://localhost:3306/db_station
11     username: root
12     password: admin
13   jpa:
14     hibernate:
15       ddl-auto: update
16     show-sql: true
17   thymeleaf:
18     cache: false
19 # eureka 注册中心配置
20 eureka:
21   instance:
22     hostname: localhost  #eureka客户端主机实例名称
23     appname: microservice-ticket  #客户端服务名
24     instance-id: microservice-ticket:1001 #客户端实例名称
25     prefer-ip-address: true #显示IP
26   client:
27     service-url:
28       #defaultZone: http://localhost:2001/eureka #(单机eureka)把服务注册到eureka注册中心,要和前面服务注册中心(microservice-eureka-server-2001)的defaultZone暴露地址一致
29       defaultZone: http://eureka2001.wfd360.com:2001/eureka/,http://eureka2002.wfd360.com:2002/eureka/,http://eureka2003.wfd360.com:2003/eureka/ # 集群
30 
31 # 服务提供者联系信息
32 info:
33    version: v2
34    负责人: 姿式帝 - 博客园
35    微  信: 851298348
View Code

 

 

第六步:测试

启动三个注册中心,以及服务提供者项目;

而后浏览器地址栏输入:http://eureka2001.wfd360.com:2001/ 

或者  http://eureka2002.wfd360.com:2002/ 

或者 http://eureka2003.wfd360.com:2003/ 

界面以下,则集群成功

  

      这里本质是三个服务注册中心都有咱们服务提供者的信息,等后面讲到服务发现和服务调用,咱们经过一些策略(默认轮询),会去找对应的服务注册中心;经过集群,能减轻每一个服务注册中心的压力;

      

值得注意的是,有时候会出现以下状况,这是  Eureka自我保护机制  

当咱们长时间为访问服务以及变动服务实例名称的时候,就会出现这个红色警告;

默认状况,若是服务注册中心再一段时间内没有接收到某个微服务实例的心跳,服务注册中心会注销该实例(默认90秒)。

因为正式环境,常常会有网络故障(若是想模拟该现象,能够先开启服务提供者,再关闭服务提供者,最后再访问注册中心),网络延迟问题发生,服务和注册中心没法正常通讯,此时服务是正常的,不该该注销该服务,

Eureka这时候,就经过“自我保护模式”来解决问题,当短期和服务失去通讯时,保留服务信息,当恢复网络和通讯时候,退出“自我保护模式”;

经过“自我保护模式”,使Eureka集群更加的健壮和稳定;

 

到这里eureka的服务端基本搞定,代码能够在github上下载,版本V4

相关文章
相关标签/搜索