spring Cloud服务注册中心Eureka集群配置:
在application.yml文件加以下配置:
server:
port: 8761
tomcat:
uri-encoding: UTF-8
eureka:
instance:
hostname: localhost
# 优先使用IP地址方式进行注册服务
prefer-ip-address: true
appname: ${spring.application.name}
# 表示eureka client发送心跳给server端的频率
leaseRenewalIntervalInSeconds: 10
# 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,
# 在这个时间内若没收到下一次心跳,则将移除该instance (缺省为90*1000ms)
leaseExpirationDurationInSeconds: 30
server:
# 关闭自我保护模式(缺省为打开)
enableSelfPreservation: false
# 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
evictionIntervalTimerInMs: 4000
client:
# 是否注册自己
register-with-eureka: true
# 要不要去注册中心获取其他服务的地址
fetch-registry: true
service-url:
defaultZone: http://localhost:8762/eureka,http://localhost:8763/eureka
application-node1.yml文件加以下配置:
server: port: 8762 tomcat: uri-encoding: UTF-8 eureka: instance: hostname: localhost prefer-ip-address: true # 优先使用IP地址方式进行注册服务 appname: ${spring.application.name} leaseRenewalIntervalInSeconds: 10 leaseExpirationDurationInSeconds: 30 server: enableSelfPreservation: false evictionIntervalTimerInMs: 4000 client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8761/eureka,http://localhost:8763/eureka
application-node2.yml文件加以下配置:
server: port: 8763 tomcat: uri-encoding: UTF-8 eureka: instance: hostname: localhost prefer-ip-address: true # 优先使用IP地址方式进行注册服务 appname: ${spring.application.name} leaseRenewalIntervalInSeconds: 10 leaseExpirationDurationInSeconds: 30 server: enableSelfPreservation: false evictionIntervalTimerInMs: 4000 client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
bootstrap.yml配置应用名称:
spring:
application:
name: spring-cloud-eureka
配置java启动类node1、node2
启动EurekaApplication、node1、node2启动类,访问http://localhost:8761/、http://localhost:8762/、http://localhost:8763/成功则说明Eureka集群配置成功。
Eureka集群示例项目代码github地址:https://github.com/yuanzipeng/spring-cloud-eureka cluster分支