前言:
我们通过springcloud入门大概的了解了springcloud。在springcloud入门中的案例中我们是直接通过服务消费者向服务提供者发送请求,然后得到结果的,但真正的项目开发中并不是这样的,如图所示,而是服务消费者向eureka发送请求,然后eureka再向服务提供者发送请求找寻到相对应的节点,服务提供者的节点有多个,其中一个宕机了eureka会自动帮你找到第二的,这里采用的是经典的轮询机制,在我们后面的博客里面会详细介绍。
在我们今天的博客里面,主要解决的是当注册中心扛不住高并发的时候发生了宕机应该怎么办,由图可知,解决方案就是让注册中心也有多个,当一个注册中心宕机之后,下一个注册中心可以顶上去,现在就让我们来了解eureka集群。
Eureka集群搭建
高可用集群配置
当注册中心扛不住高并发的时候,这时候就要用集群来扛。
普通操作
我们再新建两个module,也就是简单的Springboot项目:microservice-eureka-server-2002 和microservice-eureka-server-2003
1、两个项目添加的pom.xml 把依赖加下;
<dependencies>
<dependency>
<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>
<scope>test</scope>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
同样的这两个项目我们也需要继承父工程microservice的pom依赖,如图所示:
2、往这两个项目的主启动类MicroserviceEurekaServer2002Application,MicroserviceEurekaServer2003Application,添加注册器标签@EnableEurekaServer
3、前面单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,麻烦,这里有简单办法,直接配置本机hosts,来实现本机域名映射;
找到 C:\Windows\System32\drivers\etc 打开hosts,加配置
127.0.0.1 eureka2001.dengrenli.com
127.0.0.1 eureka2002.dengrenli.com
127.0.0.1 eureka2003.dengrenli.com
4、修改三个项目的application.yml文件,主要是修改 hostname和defaultZone
defaultZone标签写的是除了这个注册中心之外的其他注册器的访问路径。
hostname标签写的就是你在C:\Windows\System32\drivers\etc的hosts里面加的配置。
fetch-registry标签的意思就是注册中心有多个后,其中一个注册中心宕机了,它的下一个注册中心是否可以拿到这个已经宕机的这个注册中心的信息,然后承接上一个注册中心的任务,所以我们要把他设置成true,要它可以拿到上一个注册中心的信息,或者直接不写这个标签,因为不写这个标签,它就直接是默认ture的。
microservice-eureka-server-2001的application.yml修改:
server:
port: 2001
context-path: /
eureka:
instance:
# 单机 hostname: localhost #eureka注册中心实例名称
hostname: eureka2001.dengrenli.com # 集群
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
service-url:
defaultZone: http://eureka2002.dengrenli.com:2002/eureka/,http://eureka2003.dengrenli.com:2003/eureka/ # 集群
#单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka注册中心交互的地址,查询服务和注册服务用到
microservice-eureka-server-2002的application.yml修改:
server:
port: 2002
context-path: /
eureka:
instance:
# 单机 hostname: localhost #eureka注册中心实例名称
hostname: eureka2002.dengrenli.com # 集群
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
service-url:
defaultZone: http://eureka2001.dengrenli.com:2001/eureka/,http://eureka2003.dengrenli.com:2003/eureka/ # 集群
#单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka注册中心交互的地址,查询服务和注册服务用到
microservice-eureka-server-2003的application.yml修改:
server:
port: 2003
context-path: /
eureka:
instance:
# 单机 hostname: localhost #eureka注册中心实例名称
hostname: eureka2003.dengrenli.com # 集群
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
service-url:
defaultZone: http://eureka2001.dengrenli.com:2001/eureka/,http://eureka2002.dengrenli.com:2002/eureka/ # 集群
#单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka注册中心交互的地址,查询服务和注册服务用到
5、修改服务提供者项目的application.yml,主要修改eureka.client.service-url.defaultZone
eureka:
instance:
#eureka客户端主机实例名称
hostname: localhost
#客户端服务名
appname: microservice-student
#客户端实例名称
instance-id: microservice-student:1001
#显示IP
prefer-ip-address: true
client:
service-url:
# 单机 defaultZone: http://localhost:2001/eureka #把服务注册到eureka注册中心
defaultZone: http://eureka2001.dengrenli.com:2001/eureka/,http://eureka2002.dengrenli.com:2002/eureka/,http://eureka2003.dengrenli.com:2003/eureka/
最后我们测试下:
启动三个注册中心,以及服务提供者项目
这里我们在介绍一个小问题,就是如果启动项目的时候报错,如图所示,我们应该如何解决
很简单,把我标红的地方直接删除就可以了,然后重新启动项目,这样就不会有问题了
然后浏览器地址栏输入:
http://eureka2001.dengrenli.com:2001/
http://eureka2002.dengrenli.com:2002/
http://eureka2003.dengrenli.com:2003/
同时也可以查看服务提供者的管理员信息,如图所示:
更加简单的操作,得到的效果和普通操作是一样的
上面eureka服务搭建,除了yml文件不一样,其他文件都一样,那么我们有什么办法能够将多个eureka服务集合到一个工程中去呢?
创建一个microservice-eureka-server(三合一)子工程
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.dengrenli</groupId>
<artifactId>t226microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-eureka-server</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<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>
<scope>test</scope>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
---
server:
port: 2001
context-path: /
eureka:
instance:
hostname: eureka2001.dengrenli.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2002.dengrenli.com:2002/eureka/,http://eureka2003.dengrenli.com:2003/eureka/
spring:
profiles: eureka2001
---
server:
port: 2002
context-path: /
eureka:
instance:
hostname: eureka2002.dengrenli.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2001.dengrenli.com:2001/eureka/,http://eureka2003.dengrenli.com:2003/eureka/
spring:
profiles: eureka2002
---
server:
port: 2003
context-path: /
eureka:
instance:
hostname: eureka2003.dengrenli.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2001.dengrenli.com:2001/eureka/,http://eureka2002.dengrenli.com:2002/eureka/
spring:
profiles: eureka2003
启动类MicroserviceEurekaServerApplication.java
然后最重要的是在配置项目的时候
这三个配置,除了上面的name和我所标记的不一样之外,其他的配置都是相同的
运行起来的效果跟普通操作的效果是一致的
效果如图所示:
Eureka自我保护机制
如图所示:
开发环境,我们经常会遇到这个红色的警告;
当我们长时间为访问服务以及变更服务实例名称的时候,就会出现这个红色警告;
默认情况,如果服务注册中心再一段时间内没有接收到某个微服务实例的心跳,服务注册中心会注销该实例(默认90秒)。
由于正式环境,经常会有网络故障,网络延迟问题发生,服务和注册中心无法正常通信,此时服务是正常的,不应该注销该服务,Eureka这时候,就通过“自我保护模式”来解决问题,当短时间和服务失去通信时,保留服务信息,当恢复网络和通信时候,退出“自我保护模式”;
通过“自我保护模式”,使Eureka集群更加的健壮和稳定;
谢谢大家。多多指教!!!