spring cloud eureka高可用配置

要点:配置2个eureka服务,相互连通即可
配置2份配置【ha-a】
server:
  port: 8761
spring:
  profiles: ha-a
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://admin:admin@localhost:8762/eureka/

配置另外一份配置【ha-b】

server:
  port: 8762
spring:
  profiles: ha-b
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://admin:admin@localhost:8761/eureka/

注意点:如果开了登陆验证url应该配上user:password@的前缀

服务启动

ha-a启动日志会有错误信息,因为ha-b没启动,属于正常

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.6.2.jar:1.6.2]
    at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:509) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.getApplicationsInternal(AbstractJerseyEurekaHttpClient.java:194) ~[eureka-client-1.6.2.jar:1.6.2]

ha-b启动则无任何报错

2018-01-26 16:14:59.740  WARN 12236 --- [nio-8762-exec-7] c.n.eureka.resources.InstanceResource    : Not Found (Renew): HA-EUREKA - 192.168.2.201:ha-eureka:8761
2018-01-26 16:14:59.778  INFO 12236 --- [nio-8762-exec-9] c.n.e.registry.AbstractInstanceRegistry  : Registered instance HA-EUREKA/192.168.2.201:ha-eureka:8761 with status UP (replication=false)
2018-01-26 16:15:02.357  INFO 12236 --- [nio-8762-exec-5] c.n.e.registry.AbstractInstanceRegistry  : Registered instance HA-EUREKA/192.168.2.201:ha-eureka:8761 with status UP (replication=false)
2018-01-26 16:15:34.731  INFO 12236 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-01-26 16:16:34.731  INFO 12236 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-01-26 16:17:34.731  INFO 12236 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-01-26 16:18:34.731  INFO 12236 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-01-26 16:19:33.839  INFO 12236 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2018-01-26 16:19:34.731  INFO 12236 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-01-26 16:20:34.731  INFO 12236 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-01-26 16:21:34.731  INFO 12236 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-01-26 16:22:34.731  INFO 12236 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-01-26 16:23:34.731  INFO 12236 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-01-26 16:24:33.847  INFO 12236 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2018-01-26 16:24:34.075  WARN 12236 --- [eerNodesUpdater] c.n.eureka.cluster.PeerEurekaNodes       : The replica size seems to be empty. Check the route 53 DNS Registry
2018-01-26 16:24:34.731  INFO 12236 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms

最后2个eureka控制台如下

spring cloud eureka高可用配置_第1张图片
spring cloud eureka高可用配置_第2张图片

如果是3个高可用,配置应该如下

---
server:
  port: 8761
spring:
  profiles: ha-a
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://admin:admin@localhost:8762/eureka/,http://admin:admin@localhost:8763/eureka/

--- 
server:
  port: 8762
spring:
  profiles: ha-b
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://admin:admin@localhost:8761/eureka/,http://admin:admin@localhost:8763/eureka/

--- 
server:
  port: 8763
spring:
  profiles: ha-c
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://admin:admin@localhost:8761/eureka/,http://admin:admin@localhost:8762/eureka/

你可能感兴趣的:(spring,cloud)