5.4Eureka高可用集群配置
在高并发的情况下一个注册中心难以满足,因此一般需要集群配置多台。
再新建两个module eureka-server-7002,eureka-server-7003,然后配置,最终的配置结果结构如图:
具体步骤:
第一步:每个模块的pom.xml添加如下依赖:
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>
第二步:编写7002 7003的主启动类EurekaServerApplication_7002,EurekaServerApplication_7003,可以复制EurekaServerApplication_7001的启动类修改名称;
第三步:前面单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,这里有简单办法,直接配置本机hosts,来实现本机域名映射;
找到 C:\Windows\System32\drivers\etc 打开hosts,加配置
127.0.0.1 eureka7001.wfd360.com
127.0.0.1 eureka7002.wfd360.com
127.0.0.1 eureka7003.wfd360.com
注意:在修改hosts文件时,建议先拷贝出来,修改好后再替换原来的hosts文件。
第四步:修改三个项目的application.yml文件,主要是修改 hostname和defaultZone,
7001 的 application.yml文件
server:
port: 7001
context-path: /
# 注册中心服务端配置
eureka:
instance:
#hostname: localhost #注册中心主机地址(单机)
hostname: eureka7001.wfd360.com # 集群
client:
register-with-eureka: false #不向注册中心注册自己
fetch-registry: false #不需要去检索服务,注册中心的职责就是维护服务实例
service-url:
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置注册中心地址(单机)
defaultZone: http://eureka7002.wfd360.com:7002/eureka/,http://eureka7003.wfd360.com:7003/eureka/ # 集群(互相注册)