Peer Awareness
Eureka can be made even more resilient and available by running multiple instances and asking them to register with each other. In fact, this is the default behaviour, so all you need to do to make it work is add a valid serviceUrl
to a peer, e.g.
--- spring: profiles: peer1 eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: http://peer2/eureka/ --- spring: profiles: peer2 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1/eureka/
In this example we have a YAML file that can be used to run the same server on 2 hosts (peer1 and peer2), by running it in different Spring profiles. You could use this configuration to test the peer awareness on a single host (there’s not much value in doing that in production) by manipulating /etc/hosts
to resolve the host names. In fact, the eureka.instance.hostname
is not needed if you are running on a machine that knows its own hostname (it is looked up using java.net.InetAddress
by default).
You can add multiple peers to a system, and as long as they are all connected to each other by at least one edge, they will synchronize the registrations amongst themselves. If the peers are physically separated (inside a data centre or between multiple data centres) then the system can in principle survive split-brain type failures.
微服务:Eureka+Zuul+Ribbon+Feign+Hystrix构建微服务架构章节中我们搭建了一个单例的微服务,但是如何搭建一个集群式的微服务呢?
本节演示如何搭建一个Eureka集群,开篇附官方eureka服务器的集群配置。
这里拿出我自己的配置文件
One Eureka Server Profile
这里的主机名称切勿当真,理解其本质即可!!!
#集群模式启动Eureka Server
spring:
application:
name: windowHost-EurekaServer
server:
port: 8761 #启动端口
eureka: #向另一个Eureka Server注册自己为一个Client
hostname: windowHost #指定本机器的主机名
client:
serviceUrl:
defaultZone: http://hadoopMaster:${server.port:8761}/eureka/ #去主机名未hadoopMaster的Eureka服务器,将自己注册为一个客户端
注意:eureka.client.serviceUrl—我理解的含义将该Eureka Server做为一个Client注册中服务地址为:http://hadoopMaster:${server.port:8761}/eureka/ 的服务器上,当然在另一个服务器上同样的就应该复制该主机地址,这样就构成了双向的通信机制