搭建高可用的服务注册中心背景(作用):
对于服务注册中心Eureka Server,当需注册的服务达到一定的量级向它注册的时候,它的负载是很高的,如果仅仅只是单个服务注册中心时,极大可能会出现服务中心宕机的情况,当它出现宕机的时候,所有的服务就都会终止,这在生产环境是不允许的,因此我们需要一个高可用的服务注册中心来避免这种情况。原理就是运行多个Eureka Server实例运行,多个Eureka Server实例相互感应,当有服务注册时,两个Eureka-eserver是对等的,它们都存有相同的信息、相同的数据,这其实就是用冗余的注册中心来保证可靠性,如果其中有一个注册中心宕机了,服务也不会终止,因为还有好几个。
接下来看具体怎么搭建一个比较简单的高可用服务注册中心:
项目结构图:
在resources目录下创建application.yml、application-pee1.yml、application-pee2.yml三个文件
application-pee1.yml-如下:
server:
port: 8761
spring:
profiles: pee1
eureka:
instance:
hostname: pee1
client:
serviceUrl:
defaultZone: http://pee2:8762/eureka/
fetch-registry: false
register-with-eureka: false
application-pee2.yml-如下:
server:
port: 8762
spring:
profiles: pee2
eureka:
instance:
hostname: pee2
client:
serviceUrl:
defaultZone: http://pee1:8761/eureka/
fetch-registry: false
register-with-eureka: false
application.yml-如下:
spring:
profiles:
active: pee2
application:
name: eurekaserver
pom.xml文件添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在启动类中添加标签@EnableEurekaServer
还需修改hosts文件:(win10系统:C:\Windows\System32\drivers\etc)
Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应网页,如果没有找到,则系统会再将网址提交DNS域名解析服务器进行IP地址的解析。
在hosts文件中添加下面两行内容:
127.0.0.1 pee1
127.0.0.1 pee2
搭建过程中还需要运行一个服务消费者eureka-client来进行测试,它的application.properties如下:
server.port=8763
spring.application.name=EurekaClient
eureka.client.serviceUrl.defaultZone = http://pee1:8761/eureka
接下来需运行多个服务注册中心:
勾选Allow running in parallel:
具体操作:
在完成上面所有操作之后,接下来可以进行如下操作:
运行多个服务注册中心,第一次运行的时候applicaiton.yml里的active是peer1,第二次是peer2,这样在运行服务,就可以运行多个服务注册中心了。
接着运行一个服务消费者eureka-client。
接着访问http://localhost:8761/
接着访问http://localhost:8762/
这样就到了效果。
有道笔记链接http://note.youdao.com/noteshare?id=56b5aee95ab8c682adf3a2f55eb87071&sub=B13815DF4F774AE28B6AA57636C28D68