SpringCloud learning -Eureka Cluster Setup

Build Eureka clusters

Said the last time in SpringCloud applications Eureka registry, used to be a service provider service registration and discovery, but at the same time, it is also a "micro-service", a single application is limited use of space, and therefore zookeeper, it also We need to build a cluster (cluster).

Eureka to build a cluster of principles is to create more eureka applications (different ports), and will jointly address all of the registry together. Below it, for example to create three clusters

First, before the eureka project will create three copies

Three copies in order not to destroy the original project, so that small partners to better learning

  • Create three sub-Module, then copy CITIC pom.xml dependency, copy application.yml, and then create their own startup class
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
server:
  port: 7001

eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
#    这两个变量就是上边定义过的
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。

Second, modify application.yml

This is the main and most crucial step is to modify the eureka.client.service-url.defaultZoneproject. Before this is

http://${eureka.instance.hostname}:${server.port}/eureka/

Before using a variable that is defined in the current file had eureka.instance.hostname and server.port this address, now with the cluster, it is necessary to write directly to die. Modify the following principles:

  • In either eureka project, we will modify this address as the upper address eureka two other items, separated by commas
  • But then, hostname three eureka project is not the same, not all localhost, so the development time, to set up a virtual domain name in the local
  • In the windows of C:\Windows\System32\drivers\etc\HOSTSfile add the following
127.0.0.1 eureka1
127.0.0.1 eureka2
127.0.0.1 eureka3

In this way, you can eureka project a hostname instead of the top three

For example, application.yml after eureka-cluster-7001 project is modified as follows:

server:
  port: 7001

eureka:
  instance:
    hostname: eureka1 #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
#    单个eureka
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
# euraka集群
      defaultZone: http://eureka2:7002/eureka/,http://eureka3:7003/eureka
  • Followed by modifying the other two eureka project, opened last address any of the eureka project and then browser, you can see the following interface

  • In random access to three addresses, you can see two other sets clusters in any one project

Third, modify the service provider to register to cluster

  • Before a single eureka project, so there is only one service provider registered address. Now is the cluster, so you want to modify eureka registered address of the service provider

I changed a demo2-providereureka8001configuration file, modified as follows:

eureka:
  client: #客户端注册进eureka服务列表内
    service-url:
#    这个地址是在eureka的application.yml中定义过的
#     单个eureka项目的的注册地址
#      defaultZone: http://localhost:7001/eureka
#       eureka集群的注册地址
      defaultZone: http://eureka1:7001/eureka,http://eureka2:7002/eureka,http://eureka3:7003/eureka

When you're done, and then start the program provider, you can see this service in any one cluster in the project eureka

Focus on micro-channel public number "small fish and Java" get the item code and more knowledge SpringCloud

Guess you like

Origin www.cnblogs.com/Lyn4ever/p/12370203.html