概述:
高可用集群配置、当注册中心扛不住高并发的时候,这时候 要用集群来扛;一个挂了还能有其他的eureka 来顶。
集群搭建
普通操作
1、这里我们先建3个 module microservice-eureka-server-2001、 microservice-eureka-server-2002 、microservice-eureka-server-2003
2、三个项目的pom.xml 文件都一样 这里就写一个 microservice-eureka-server-2001 的完整pom.xml,其他的只有对应的项目名称变了
可是记得要改 <artifactId>microservice-eureka-server-2001</artifactId>
的名字
<?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.thf</groupId>
<artifactId>T226microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-eureka-server-2001</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>
3、在每个项目的启动类上加 @EnableEurekaServer 这个注解
4、前面单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,麻烦,这里有简单办法,直接配置本机hosts,来实现本机域名映射;
找到 C:\Windows\System32\drivers\etc 打开hosts,加配置
5、修改三个项目的application.yml文件,主要是修改 hostname和defaultZone
2001修改:
server:
port: 2001
context-path: /
eureka:
instance:
# 单机 hostname: localhost #eureka注册中心实例名称
hostname: eureka2001.thf.com # 集群
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
service-url:
defaultZone: http://eureka2002.thf.com:2002/eureka/,http://eureka2003.thf.com:2003/eureka/ # 集群
2002修改:
server:
port: 2002
context-path: /
eureka:
instance:
# 单机 hostname: localhost
hostname: eureka2002.thf.com
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
service-url:
defaultZone: http://eureka2001.thf.com:2001/eureka/,http://eureka2003.thf.com:2003/eureka/ # 集群
2003修改:
server:
port: 2003
context-path: /
eureka:
instance:
# 单机 hostname: localhost #eureka注册中心实例名称
hostname: eureka2003.thf.com # 集群
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
service-url:
defaultZone: http://eureka2001.thf.com:2001/eureka/,http://eureka2002.thf.com:2002/eureka/ # 集群
6、还有一点就是我们的被调用方服务,也就是客户端这边,原来是单机eureka所以它的配置文件中只配置了一个eureka的链接,现在是弄集群:
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.thf.com:2001/eureka/,http://eureka2002.thf.com:2002/eureka/,http://eureka2003.thf.com:2003/eureka/
最后我们测试下:
启动三个注册中心,以及服务提供者项目;
然后浏览器地址栏输入:
http://eureka2001.thf.com:2001/
http://eureka2002.thf.com:2002/
http://eureka2003.thf.com:2003/
集群三合一
再进行了上面的操作过后,就可以明白了集群的搭法。
这个是教如何不用建三个项目用一个项目也能开启三个服务注册中心
创建一个microservice-eureka-server(三合一)子工程
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.thf</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>
(重点)配置yml文件
---
server:
port: 2001
context-path: /
eureka:
instance:
hostname: eureka2001.thf.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2002.thf.com:2002/eureka/,http://eureka2003.thf.com:2003/eureka/
spring:
profiles: eureka2001
---
server:
port: 2002
context-path: /
eureka:
instance:
hostname: eureka2002.thf.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2001.thf.com:2001/eureka/,http://eureka2003.thf.com:2003/eureka/
spring:
profiles: eureka2002
---
server:
port: 2003
context-path: /
eureka:
instance:
hostname: eureka2003.thf.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2001.thf.com:2001/eureka/,http://eureka2002.thf.com:2002/eureka/
spring:
profiles: eureka2003
启动类MicroserviceEurekaServerApplication.java
加上:@EnableEurekaServer
配置完毕后就可以配置用这一个项目配置多个spring启动方案了
第二个
第三个
跑起来的效果跟普通操作的效果是一致的;
Eureka自我保护机制
下面的红字出现的原因是因为eureka的自我保护机制,因为服务没有被调用的原因,要解决这个问题
开发环境,我们经常会遇到这个红色的警告;
当我们长时间为访问服务以及变更服务实例名称的时候,就会出现这个红色警告;
默认情况,如果服务注册中心再一段时间内没有接收到某个微服务实例的心跳,服务注册中心会注销该实例(默认90秒)。
由于正式环境,经常会有网络故障,网络延迟问题发生,服务和注册中心无法正常通信,此时服务是正常的,不应该注销该服务,Eureka这时候,就通过“自我保护模式”来解决问题,当短时间和服务失去通信时,保留服务信息,当恢复网络和通信时候,退出“自我保护模式”;
通过“自我保护模式”,使Eureka集群更加的健壮和稳定;
🆗!!!