Eureka高可用原理:
Eureka高可用实际上将自己作为服务向其他服务注册中心注册自己,这样就可以形成一组相互注册的服务注册中心,从而实现服务清单的相互同步,达到高可用效果。
Eureka集群环境搭建(本文展示的是两个服务的集群+一个生产者+一个消费者):
Eureka_01配置:
application.yml
###服务端口号
server:
port: 8100
###eureka 基本信息配置
spring:
application:
name: zxf-eureka-server
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://127.0.0.1:8200/eureka/ #将自己注册到8200中去
###因为自己是为注册中心,不需要自己注册自己 集群模式下改为 ture
register-with-eureka: true
###因为自己是为注册中心,不需要检索服务
fetch-registry: true
pom.xml 有注释说明的为主要依赖
<!-- springCloud管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--SpringCloud eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
Eureka_02配置:
application.yml
###服务端口号
server:
port: 8200
###eureka 基本信息配置
spring:
application:
name: zxf-eureka-server
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://127.0.0.1:8100/eureka/ #将自己注册到8100中去
###因为自己是为注册中心,不需要自己注册自己 如果为集群则为true
register-with-eureka: true
###因为自己是为注册中心,不需要检索服务
fetch-registry: true
pom.xml配置同Eureka_01一样
启动项目
注:项目启动过程中出现如下报错是正常现象:因为两台是互相注册,在启动时另一台肯定还没有启动,所以包如下错误1
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
效果展示:
服务生产者和服务消费者在我的上一篇博客中https://blog.csdn.net/Websphere_zxf/article/details/89501411,这里不做过多解释!特别说明的是:当服务生产者启动时首先会注册到8100中去,而8200中没有该生产者。但是当8100服务关闭后(模仿宕机)该服务生产者会自动注册(约30秒后)到8200中去!