前言
在上一篇博客中, 我们介绍了Spring Cloud 系列 (一)Eureka 服务注册与发现介绍了
Spring Cloud Eureka
做为一个服务注册中心的基本概念与知识. 但是上述服务, 只适用于单点服务, 并不满足我们在生产环境中的需求.
在微服务架构的分布式环境中, 我们需要充分考虑发生故障的情况, 所以在生产环境中, 必须对各个组件进行高可用部署. 因此, 在本篇文章中, 我们主要讲解如何改善 Eureka-Server
本次我们目标将服务改善为以下结构:
Eureka Server
1,Maven 依赖
- <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
2,Properties 配置
我们需要配置高可用的 Eureka-Server 因此, 在单机环境下, 我们通过区分 properties-profile 来区分多个服务
- 2.1 application.properties
- ## 服务应用名称
- spring.applicaiton.name = spring-cloud-eureka-server
- ## 向注册中心注册
- eureka.client.register-with-eureka = true
- ## 向获取注册信息
- eureka.client.fetch-registry = true
复制代码
这里记录公用属性, 不需要区分服务.
在上一篇文章中, 我们将
eureka.client.register-with-eureka
和
eureka.client.fetch-registry
都设置成了 false , 避免在单机情况下将自己注册到注册中心中. 而在分布式环境中, 我们需要通过这两个参数来完成注册中心的相互关联
- 2.2 application-peer1.properties
- ## 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 注册中心中.
- 2.3 application-peer2.properties
- ## 配置 服务器端口
- ## 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
复制代码
3, 启动服务
在启动类
SpringCloudAvaliabilityEurkaApplication
中加入
@EnableEurekaServer
注解
- @SpringBootApplication
- @EnableEurekaServer
- public class SpringCloudAvaliabilityEurkaApplication {
- public static void main(String[] args) {
- SpringApplication.run(SpringCloudAvaliabilityEurkaApplication.class, args);
- }
- }
复制代码
在 Run Configurations 中设置启动参数, 并且启动多个实例
启动结果:
我们可以留意以下这部分内容:
可以看到, 注册中心的副本为 我们的 peer2 服务
Eureka Client
接下来我们改造 Eureka-Client 服务, 注册到高可用的注册中心中
1,Maven 依赖
- <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>
复制代码
2,Properties 配置
- ## 服务名称
- 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<>();
- }
复制代码
3, 启动服务
总结:
源码地址:
https://github.com/jaycekon/Spring-Cloud
参考:
- https://juejin.im/post/5b65479a6fb9a04fe11b0143
- http://blog.didispace.com/springcloud6/
- https://segmentfault.com/ls/1650000011386794
来源: https://juejin.im/post/5b6e90d7e51d45198f5cd3d4