EurekaServer高可用的注册中心集群搭建

转载请注明出处:https://www.cnblogs.com/mahongchao/p/9773586.html

1、创建springboot工程,工程目录如下:

2、添加gradle依赖

dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server")
}

       

3、在springboot工程的入口类上添加@SpringBootApplication、@EnableEurekaServer注解。

4、resources下创建三个配置文件,内容如下:

   application-peer1.yml

server:
  port: 8761

spring:
  application:
    name: eureka-discovery
  profiles: peer1

eureka:
  instance: 
    hostname: peer1
  client:
    #register-with-eureka: false
    #fetch-registry: false
    serviceUrl:
      defaultZone: http://peer2:8761/eureka/,http://peer3:8761/eureka/
      

   application-peer2.yml

server:
  port: 8761
  
spring:
  application:
    name: eureka-discovery
  profiles: peer2

eureka:
  instance: 
    hostname: peer2
  client:
    #register-with-eureka: false
    #fetch-registry: false
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer3:8761/eureka/
      

   application-peer3.yml

server:
  port: 8761

spring:
  application:
    name: eureka-discovery
  profiles: peer3

eureka:
  instance: 
    hostname: peer3
  client:
    #register-with-eureka: false
    #fetch-registry: false
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8761/eureka/

5、准备三台主机:

192.168.0.101、192.168.0.102、192.168.0.103

在每台主机上分别映射另外两台主机的ip,以192.168.0.101主机为例:

在192.168.0.101终端输入命令:

vi /etc/hosts

编辑hosts文件,增加两行:

192.168.0.102 peer2
192.168.0.103 peer3

编辑192.168.0.102的/etc/hosts文件,增加两行:

192.168.0.101 peer1
192.168.0.103 peer3

编辑192.168.0.103的/etc/hosts文件,增加两行:

192.168.0.101 peer1
192.168.0.102 peer2

全部加完以后,在192.168.0.101的终端输入命令:

ping peer2
ping peer3

其他两台主机也分别测试是能用peer名字相互ping通,如果不行,网上找办法解决,直到能相互ping通。

6、将EurekaServer功程打包成jar,分别拷贝到准备好的三台主机上,用java命令运行起来

在192.168.0.101上的jar包目录下运行

java -jar EurekaServer.jar --spring.profiles.active=peer1

在192.168.0.102上的jar包目录下运行

java -jar EurekaServer.jar --spring.profiles.active=peer2

在192.168.0.103上的jar包目录下运行

java -jar EurekaServer.jar --spring.profiles.active=peer3

 7、在浏览器分别访问三个注册中心地址

访问http://192.168.0.101:8761显示如下:

 

访问http://192.168.0.102:8761显示如下:

 

访问http://192.168.0.103:8761显示如下:

 

 至此,高可用的eurekaServer注册中心搭建完毕。

转自https://www.cnblogs.com/mahongchao/p/9773586.html

原文地址:https://www.cnblogs.com/mahongchao/p/9773586.html