eureka作为注册中心,生产环境必须多节点部署,保证其高可用性。现以两台服务器来完成集群部署。

        服务器A:172.16.21.34

        服务器B:172.16.21.35

方式一:使用ip形式完成。

服务器A:172.16.21.34

server:
  port: 7777

spring:
  application:
    name: register
  #指定eureka客户端的登录账户
  security:
    user:
      name: test
      password: test

eureka:
  environment: prod34
  #ip形式指定其他节点
  instance:
    prefer-ip-address: true
  #获取服务列表
  #将自己注册为服务
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://test:test@172.16.21.35:7777/eureka/
  server:
    enable-self-preservation: true

logging:
  config: classpath:log4j2.xml

服务器B:172.16.21.35

server:
  port: 7777

spring:
  application:
    name: register
  #指定eureka客户端的登录账户
  security:
    user:
      name: test
      password: test

eureka:
  environment: prod35
  #ip形式指定其他节点
  instance:
    prefer-ip-address: true
  #获取服务列表
  #将自己注册为服务
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://test:test@172.16.21.34:7777/eureka/
  server:
    enable-self-preservation: true

logging:
  config: classpath:log4j2.xml

方式二:使用主机名形式完成。

        该方式首先需要在服务器A,B上配置hosts文件信息,增加执行对方主机的域名解析。且主机名不可包含"_"字符,否则URI类获取对应的host信息会报错:Host name may not be null。

172.16.21.34 peer1
172.16.21.35 peer2

服务器A:172.16.21.34

server:
  port: 7777

spring:
  application:
    name: register
  #指定eureka客户端的登录账户
  security:
    user:
      name: test
      password: test

eureka:
  environment: prod34
  #主机名形式
  instance:
    hostname: peer1
  #获取服务列表
  #将自己注册为服务
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://test:test@peer2:7777/eureka/
  server:
    enable-self-preservation: true

logging:
  config: classpath:log4j2.xml

服务器B:172.16.21.35

server:
  port: 7777

spring:
  application:
    name: register
  #指定eureka客户端的登录账户
  security:
    user:
      name: test
      password: test

eureka:
  environment: prod35
  #主机名形式
  instance:
    hostname: peer2
  #获取服务列表
  #将自己注册为服务
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://test:test@peer1:7777/eureka/
  server:
    enable-self-preservation: true

logging:
  config: classpath:log4j2.xml

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐