公众号: java乐园

1、 Eureka作为spring cloud的服务发现与注册中心,在整个的微服务体系中,处于核心位置。单机模式下的eureka服务,显然不能满足高可用的实际生产环境,这就要求配置一个能够应对各种突发情况,具有较强容灾能力的eureka服务。Eureka通过“伙伴机制”实现高可用。每一台Eureka都需要在配置中指定另外两个Eureka的地址伙伴,Eureka启动时会向自己的伙伴节点获取当前已经存在的注册表,这样在向Eureka集群中新加机器时就不需要担心注册表的不完整。
2、 新建三个maven项目,分别为sc-eureka-cluster-server-node1,sc-eureka-cluster-server-node2,sc-eureka-cluster-server-node3。项目结构如下:
2、eureka注册中心集群_第1张图片
备注:pom.xml和EurekaServerApplication.java的内容给单机模式是一模一样的,请参考上篇文章。
3、 主要看下bootstap.yml或者application.yml文件的改动
sc-eureka-cluster-server-node1

spring:
    application:
        name: sc-eureka-cluster-server-node1

server:
    port: 5001

eureka:
    instance:
        hostname: sc-eureka-cluster-server-node1
        lease-renewal-interval-in-seconds: 30
        lease-expiration-duration-in-seconds: 30
        prefer-ip-address: true
    client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
            defaultZone: http://sc-eureka-cluster-server-node2:5002/eureka/,http://sc-eureka-cluster-server-node3:5003/eureka/

sc-eureka-cluster-server-node2

spring:
    application:
        name: sc-eureka-cluster-server-node2

server:
    port: 5002

eureka:
    instance:
        hostname: sc-eureka-cluster-server-node2
        lease-renewal-interval-in-seconds: 30
        lease-expiration-duration-in-seconds: 30
        prefer-ip-address: true
    client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
            defaultZone: http://sc-eureka-cluster-server-node1:5001/eureka/,http://sc-eureka-cluster-server-node3:5003/eureka/

sc-eureka-cluster-server-node3

spring:
    application:
        name: sc-eureka-cluster-server-node3

server:
    port: 5003

eureka:
    instance:
        hostname: sc-eureka-cluster-server-node3
        lease-renewal-interval-in-seconds: 30
        lease-expiration-duration-in-seconds: 30
        prefer-ip-address: true
    client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
            defaultZone: http://sc-eureka-cluster-server-node1:5001/eureka/,http://sc-eureka-cluster-server-node2:5001/eureka/

4、 配置host文件C:\Windows\System32\drivers\etc\hosts
127.0.0.1 sc-eureka-cluster-server-node1
127.0.0.1 sc-eureka-cluster-server-node2
127.0.0.1 sc-eureka-cluster-server-node3

5、 分别启动EurekaServerApplication.java
sc-eureka-cluster-server-node1的日志:
2、eureka注册中心集群_第2张图片

sc-eureka-cluster-server-node2的日志:
2、eureka注册中心集群_第3张图片

sc-eureka-cluster-server-node3的日志
2、eureka注册中心集群_第4张图片
如果出现上面圈住的日志说明启动成功

6、 在浏览器分别访问
http://sc-eureka-cluster-server-node1:5001/
2、eureka注册中心集群_第5张图片
http://sc-eureka-cluster-server-node2:5002/
2、eureka注册中心集群_第6张图片
http://sc-eureka-cluster-server-node3:5002/
2、eureka注册中心集群_第7张图片

下面列出几个重要的配置项类:

org.springframework.boot.autoconfigure.web.ServerProperties
org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean
com.netflix.eureka.EurekaServerConfig
com.netflix.eureka.DefaultEurekaServerConfig
org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean
com.netflix.discovery.EurekaClientConfig
com.netflix.discovery.DefaultEurekaClientConfig
org.springframework.cloud.netflix.eureka.EurekaClientConfigBean