Eureka集群高可用配置:
Server端配置:
1,创建一个eurekaserver1模块,pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.QuanTong</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eurekaserver1</artifactId>
<packaging>jar</packaging>
<dependencies>
<!-- 服务发现Eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml配置文件:
---
spring:
application:
name: eurekaserver
2,我用一个项目来启三个EurekaServer服务,占用三个不同端口,实现Eureka集群服务。
添加application-peer1.yml配置文件,peer1服务端口号:8761
---
server:
port: 8761
---
eureka:
instance:
hostname: peer1
# 在Eureka中显示真实IP:端口号
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
leaseRenewalIntervalInSeconds: 10
leaseExpirationDurationInSeconds: 30
client:
# 表示是否将自己注册到Eureka Server,默认为true
# registerWithEureka: false
# 表示是否从Eureka Server获取注册信息,默认为true
fetchRegistry: false
# 注册到另外两个Server端
serviceUrl:
defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/
server:
enableSelfPreservation: false
evictionIntervalTimerInMs: 4000
添加application-peer2.yml配置文件,peer2服务端口号:8762
---
server:
port: 8762
---
eureka:
instance:
hostname: peer2
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
leaseRenewalIntervalInSeconds: 10
leaseExpirationDurationInSeconds: 30
client:
# 表示是否将自己注册到Eureka Server,默认为true
# registerWithEureka: false
# 表示是否从Eureka Server获取注册信息,默认为true
fetchRegistry: false
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/
server:
enableSelfPreservation: false
evictionIntervalTimerInMs: 4000
添加application-peer3.yml配置文件,peer3服务端口号:8763
---
server:
port: 8763
---
eureka:
instance:
hostname: peer3
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
leaseRenewalIntervalInSeconds: 10
leaseExpirationDurationInSeconds: 30
client:
# 表示是否将自己注册到Eureka Server,默认为true
# registerWithEureka: false
# 表示是否从Eureka Server获取注册信息,默认为true
fetchRegistry: false
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer3:8762/eureka/
server:
enableSelfPreservation: false
evictionIntervalTimerInMs: 4000
3,项目中,我们制定了三个不同的端口,我们将peer1的service-url指定到peer2,peer3,其余两个分别指定到另外两个服务,以此来实现三个Server服务之间的相互注册。
由于我们使用了http://peer1这种写法,需要配一下host。Windows的host在C:\Windows\System32\drivers\etc\host,mac的在/private/etc
然后在启动类上加上EnableEurekaServer注解即可。
4,下面我们来看如何分别用peer1、peer2和peer3三个配置启动三个server服务。
分别启动三个Server服务,如下显示,三个服务已经互相注册
client端配置
新建一个Management项目。
pom如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.chinamobile</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>management</artifactId>
<packaging>jar</packaging>
<!-- 引入依赖 -->
<dependencies>
<!-- 服务发现Eureka -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<!-- 项目构建 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml配置如下
---
spring:
profiles:
active: dev
application:
name: management
---
server:
port: 10086
application-dev.yml配置,只注册到三个注册中心的一个上,其余两个也会自动去注册到
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
leaseRenewalIntervalInSeconds: 10
leaseExpirationDurationInSeconds: 30
查看一下三个注册中心,Management服务已经全部注册到注册中心:
其实这样就已经达到了我们的目的,所有的客户端都只指向一个eureka server地址,至于server端是怎么做高可用、怎么处理单体故障是客户端不关心的。倘若client端配置了所有server的地址,那么每当server增加删除了一个服务后,客户端就需要随之改变,那不是我们希望看到的。测试很简单,我们直接关掉peer1,然后看看peer2和peer3是否还能维持住client的发现。
关掉peer1,8761的网页已经打不开了,8762、8763上也已经没有了8761的服务发现,peer2、peer3控制台在一直报错注册不到peer1
就这样,EurekaServer集群高可用配置就配置完成了