Achieve high availability and application of Eureka Server registered on the Eureka Sever cluster

1. Recalls

  Eureka Client will regularly connect Eureka Server, access to information in the registry and cached locally. Always use micro data service in the local cache in the consumer remote API. Therefore, in general, even if Eureka Server downtime occurs, it will not affect the calls between the service. But if Eureka Server is down, there have been some micro service unavailable, Eureka Server if the cache is refreshed, it may affect the micro-calling services, and even affect the entire application highly available system. Therefore, in the build environment, often deploy a high-availability Eureka Server cluster.

  Eureka Server high availability can be achieved by deployment and run multiple instances of each registration, Eureka Server instances incremental synchronization information to each other, thereby ensuring consistent data to all nodes. In fact, each registered between nodes is the default behavior of Eureka Server.

2. Construction of a two-node cluster of Eureka Server

  > Copy Project microservice-discovery-eureka, will ArtifactId modified to microservice-discovery-eureka-ha01

  > Copy Project microservice-discovery-eureka, will ArtifactId modified to microservice-discovery-eureka-ha02

  > Modify the hosts file in windows platform, go to the directory: C: \ Windows \ System32 \ the Drivers \ etc , and then open the hosts file with administrator privileges, add a record

127.0. 0.1 Peer1 peer2

  > Modify microservice-discovery-eureka-ha01 profile application.yml

Copy the code
Server: 
  Port: 8762  Eureka:  instance: hostname : peer1 Client: Service- url: defaultzone: HTTP: // peer2: 8763 / Eureka # Set the interaction with Eureka Server address, tracking and registration services will need to rely on this address. spring: application: name: microservice- discovery-eureka-ha
    
Copy the code

  > Modify microservice-discovery-eureka-ha02 profile application.yml

Copy the code
Server: 
  Port: 8763  Eureka:  instance: hostname : peer2 Client: Service- url: defaultzone: HTTP: // peer1: 8762 / Eureka # Set the interaction with Eureka Server address, tracking and registration services will need to rely on this address. spring: application: name: microservice- discovery-eureka-ha
    
Copy the code

  > Spring.application.name two projects must be the same, eureka.instance.hostname must be different, otherwise it will fail. ( At least I tested this)

  > Start two projects, visit: HTTP: // peer1: 8762 / and HTTP: // peer2: 8763 / , this time registered-replicas (registered fragmentation) have url address to another system, and are available -replicase (available slice) of

3. The micro-services registered on the Eureka Server Clusters 

  > Modify the configuration file application.yml microservice-provider-user in the eureka.client.service-url.defaultZone changed to http: // peer1: 8762 / eureka /.

eureka:
  client:
    service-url:
      defaultZone: http://peer1:8762/eureka/

  > Modify the configuration file application.yml microservice-consumer-movie in the eureka.client.service-url.defaultZone changed to http: // peer1: 8762 / eureka /, http: // peer2: 8763 / eureka /.

 

eureka:
  client:
    service-url:
      defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka

 

  > 先启动Eureka Server集群,再启动这两个微服务。访问:http://peer1:8762/ http://peer2:8763/。发现这两个微服务都注册在了集群里。

    说明即使微服务只配置Eureka Server集群中的某个节点,也能正常注册到Eureka Server集群,因为多个Eureka Server之间的数据会相互同步。

    不过为适应某些极端场景,建议在客户端配置多个Eureka Server节点。

1. 回顾

  Eureka Client会定时连接Eureka Server,获取注册表中的信息并缓存到本地。微服务在消费远程API时总是使用本地缓存中的数据。因此一般来说,即使Eureka Server发生宕机,也不会影响到服务之间的调用。但如果Eureka Server宕机时,某些微服务也出现了不可用的情况,Eureka Server中的缓存若不被刷新,就可能会影响到微服务的调用,甚至影响到整个应用系统的高可用。因此,在生成环境中,通常会部署一个高可用的Eureka Server集群。

  Eureka Server可以通过运行多个实例并相互注册的方式实现高可用部署,Eureka Server实例会彼此增量地同步信息,从而确保所有节点数据一致。事实上,节点之间相互注册是Eureka Server的默认行为。

2. 构建一个双节点的Eureka Server集群

  > 复制项目microservice-discovery-eureka,将ArtifactId修改为microservice-discovery-eureka-ha01

  > 复制项目microservice-discovery-eureka,将ArtifactId修改为microservice-discovery-eureka-ha02

  > 修改hosts文件,在windows平台下,进入目录:C:\Windows\System32\drivers\etc,然后用管理员权限打开hosts文件,添加一条记录

127.0.0.1 peer1 peer2

  > 修改microservice-discovery-eureka-ha01的配置文件application.yml

Copy the code
server:
  port: 8762
eureka:
  instance:
    hostname: peer1 client: service-url: defaultZone: http://peer2:8763/eureka # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。 spring: application: name: microservice-discovery-eureka-ha
Copy the code

  > 修改microservice-discovery-eureka-ha02的配置文件application.yml

Copy the code
server:
  port: 8763
eureka:
  instance:
    hostname: peer2 client: service-url: defaultZone: http://peer1:8762/eureka # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。 spring: application: name: microservice-discovery-eureka-ha
Copy the code

  > 两个项目的spring.application.name必须相同,eureka.instance.hostname必须不同,否则都会失败。(至少我测试的是这样)

  > 启动两个项目,访问:http://peer1:8762/http://peer2:8763/ ,此时registered-replicas(已注册分片)都有另一个系统的url地址,并且都在available-replicase(可用分片)中

3. 将微服务注册到Eureka Server集群上 

  > 修改microservice-provider-user中的配置文件application.yml,将eureka.client.service-url.defaultZone改为http://peer1:8762/eureka/。

eureka:
  client:
    service-url:
      defaultZone: http://peer1:8762/eureka/

  > Modify the configuration file application.yml microservice-consumer-movie in the eureka.client.service-url.defaultZone changed to http: // peer1: 8762 / eureka /, http: // peer2: 8763 / eureka /.

 

eureka:
  client:
    service-url:
      defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka

 

  > First start Eureka Server cluster, and then start the two micro-services. Access: HTTP: // peer1: 8762 / and HTTP: // peer2: 8763 / . Micro found that the two services are registered in the cluster.

    Note Even if the micro-services only configure a cluster node Eureka Server, can be properly registered to Eureka Server cluster, because the data between multiple Eureka Server will be synchronized with each other.

    But to adapt to extreme scenarios, it is recommended to configure multiple nodes in Eureka Server client.

Guess you like

Origin www.cnblogs.com/ldsweely/p/11599638.html