业余草 SpringCloud教程 | 第十篇: 高可用的服务注册中心(Finchley版本)

前面第一篇文章:业余草 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)html

介绍了服务注册与发现,其中服务注册中心Eureka Server,是一个实例,当成千上万个服务向它注册的时候,它的负载是很是高的,这在生产环境上是不太合适的,这篇文章主要介绍怎么将Eureka Server集群化。java

1、准备工做

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.linux

摘自官网git

Eureka经过运行多个实例,使其更具备高可用性。事实上,这是它默认的熟性,你须要作的就是给对等的实例一个合法的关联serviceurl。github

这篇文章咱们基于第一篇文章的工程,来作修改。spring

2、改造工做

在eureka-server工程中resources文件夹下,建立配置文件application-peer1.yml:vim

 1 server:
 2   port: 8761
 3  
 4 spring:
 5   profiles: peer1
 6 eureka:
 7   instance:
 8     hostname: peer1
 9   client:
10     serviceUrl:
11       defaultZone: http://peer2:8769/eureka/

而且建立另一个配置文件application-peer2.yml:windows

 1  server:
 2   port: 8769
 3  
 4 spring:
 5   profiles: peer2
 6 eureka:
 7   instance:
 8     hostname: peer2
 9   client:
10     serviceUrl:
11       defaultZone: http://peer1:8761/eureka/

这时eureka-server就已经改造完毕。服务器

ou 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.微信

按照官方文档的指示,须要改变etc/hosts,linux系统经过vim /etc/hosts ,加上:

1 127.0.0.1 peer1
2 127.0.0.1 peer2

windows电脑,在c:/windows/systems/drivers/etc/hosts 修改。

这时须要改造下service-hi:

1 eureka:
2   client:
3     serviceUrl:
4       defaultZone: http://peer1:8761/eureka/
5 server:
6   port: 8762
7 spring:
8   application:
9     name: service-hi

3、启动工程

启动eureka-server:

java -jar eureka-server-0.0.1-SNAPSHOT.jar - -spring.profiles.active=peer1

java -jar eureka-server-0.0.1-SNAPSHOT.jar - -spring.profiles.active=peer2

这里写图片描述

启动service-hi:

java -jar service-hi-0.0.1-SNAPSHOT.jar

访问:localhost:8761,如图:

Paste_Image.png

你会发现注册了service-hi,而且有个peer2节点,同理访问localhost:8769你会发现有个peer1节点。

client只向8761注册,可是你打开8769,你也会发现,8769也有 client的注册信息。

我的感觉:这是经过看官方文档的写的demo ,可是须要手动改host是否是不符合Spring Cloud 的高上大?

Prefer IP Address

In some cases, it is preferable for Eureka to advertise the IP Adresses of services rather than the hostname. Set eureka.instance.preferIpAddress to true and when the application registers with eureka, it will use its IP Address rather than its hostname.

摘自官网

eureka.instance.preferIpAddress=true是经过设置ip让eureka让其余服务注册它。也许能经过去改变去经过改变host的方式。

此时的架构图:

有点丑e.png

Eureka-eserver peer1 8761,Eureka-eserver peer2 8769相互感应,当有服务注册时,两个Eureka-eserver是对等的,它们都存有相同的信息,这就是经过服务器的冗余来增长可靠性,当有一台服务器宕机了,服务并不会终止,由于另外一台服务存有相同的数据。

本文源码下载: 
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter10

4、参考文献

high_availability_zones

业余草微信公众号

感谢您的关注!可加QQ1群:135430763,QQ2群:454796847,QQ3群:187424846。QQ群进群密码:xttblog,想加微信群的朋友,能够微信搜索:xmtxtt,备注:“xttblog”,添加助理微信拉你进群。备注错误不会赞成好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合做可添加助理微信进行沟通!

相关文章
相关标签/搜索