在上一篇博客中,咱们介绍了《Spring Cloud 系列(一)Eureka 服务注册与发现》介绍了 Spring Cloud Eureka
作为一个服务注册中心的基本概念与知识。可是上述服务,只适用于单点服务,并不知足咱们在生产环境中的需求。git
在微服务架构的分布式环境中,咱们须要充分考虑发生故障的状况,因此在生产环境中,必须对各个组件进行高可用部署。所以,在本篇文章中,咱们主要讲解如何改善 Eureka-Server
github
本次咱们目标将服务改善为如下结构:web
<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>
复制代码
maven 依赖与上篇文章并无区别,主要仍是引入spring-cloud-starter-netflix-eureka-server
spring
咱们须要配置高可用的 Eureka-Server
所以,在单机环境下,咱们经过区分 properties-profile
来区分多个服务segmentfault
## 服务应用名称
spring.applicaiton.name = spring-cloud-eureka-server
## 向注册中心注册
eureka.client.register-with-eureka = true
## 向获取注册信息
eureka.client.fetch-registry = true
复制代码
这里记录公用属性,不须要区分服务。bash
在上一篇文章中,咱们将eureka.client.register-with-eureka
和 eureka.client.fetch-registry
都设置成了 false ,避免在单机状况下将本身注册到注册中心中。而在分布式环境中,咱们须要经过这两个参数来完成注册中心的相互关联服务器
## peer 1 端口 9090
server.port = 9090
## peer 2 主机:localhost , 端口 9091
peer2.server.host = localhost
peer2.server.port = 9091
# Eureka 注册信息
eureka.client.serviceUrl.defaultZone = http://${peer2.server.host}:${peer2.server.port}/eureka
复制代码
这里配置第一台注册中心的信息。能够看到,咱们这里使用 eureka.client.serviceUrl.defaultZone = http://${peer2.server.host}:${peer2.server.port}/eureka
这个地址来注册服务,即将本地服务注册到 peer2 注册中心中。架构
## 配置 服务器端口
## peer 2 端口 9091
server.port = 9091
## peer 1 主机:localhost , 端口 9090
peer1.server.host = localhost
peer1.server.port = 9090
# Eureka 注册信息
eureka.client.serviceUrl.defaultZone = http://${peer1.server.host}:${peer1.server.port}/eureka
复制代码
在启动类SpringCloudAvaliabilityEurkaApplication
中加入 @EnableEurekaServer
注解app
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudAvaliabilityEurkaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudAvaliabilityEurkaApplication.class, args);
}
}
复制代码
在Run Configurations 中设置启动参数,而且启动多个实例 maven
启动结果:
咱们能够留意如下这部份内容:
能够看到,注册中心的副本为 咱们的 peer2
服务
接下来咱们改造 Eureka-Client
服务,注册到高可用的注册中心中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
复制代码
## 服务名称
spring.application.name = spring-cloud-eureka-client
server.port = 8083
## 配置链接 Eureka 服务器
eureka.client.serviceUrl.defaultZone = http://localhost:9090/eureka,http://localhost:9091/eureka
## 调整获取全部应用元信息间隔时间
eureka.client.registryFetchIntervalSeconds = 5
## 调整应用元信息间隔时间
eureka.client.instanceInfoReplicationIntervalSeconds = 5
复制代码
这里设置多个Eureka 服务器经过,
分隔,主要能够参考 EurekaClientConfigBean
中:
public List<String> getEurekaServerServiceUrls(String myZone) {
String serviceUrls = this.serviceUrl.get(myZone);
if (serviceUrls == null || serviceUrls.isEmpty()) {
serviceUrls = this.serviceUrl.get(DEFAULT_ZONE);
}
if (!StringUtils.isEmpty(serviceUrls)) {
final String[] serviceUrlsSplit = StringUtils.commaDelimitedListToStringArray(serviceUrls);
List<String> eurekaServiceUrls = new ArrayList<>(serviceUrlsSplit.length);
for (String eurekaServiceUrl : serviceUrlsSplit) {
if (!endsWithSlash(eurekaServiceUrl)) {
eurekaServiceUrl += "/";
}
eurekaServiceUrls.add(eurekaServiceUrl);
}
return eurekaServiceUrls;
}
return new ArrayList<>();
}
复制代码
源码地址:https://github.com/jaycekon/Spring-Cloud
参考: https://juejin.im/post/5b65479a6fb9a04fe11b0143
http://blog.didispace.com/springcloud6/
https://segmentfault.com/ls/1650000011386794