Eureka集群

Eureka集群搭建

高可用集群配置

当注册中心扛不住高并发的时候,这时候 要用集群来扛;

普通操作

新建两个stringboot项目

module  microservice-eureka-server-2002    microservice-eureka-server-2003

配置

microservice-eureka-server-2002
microservice-eureka-server-2003

pom.xml 依赖:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.javaqi</groupId>
 7         <artifactId>qimicroservice</artifactId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <artifactId>microservice-eureka-server-2002</artifactId>
11 
12     <properties>
13         <java.version>1.8</java.version>
14     </properties>
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.cloud</groupId>
18             <artifactId>spring-cloud-starter-eureka-server</artifactId>
19         </dependency>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-test</artifactId>
23             <scope>test</scope>
24         </dependency>
25         <!--  修改后立即生效,热部署  -->
26         <dependency>
27             <groupId>org.springframework</groupId>
28             <artifactId>springloaded</artifactId>
29         </dependency>
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-devtools</artifactId>
33         </dependency>
34 
35     </dependencies>
36     <build>
37         <plugins>
38             <plugin>
39                 <groupId>org.springframework.boot</groupId>
40                 <artifactId>spring-boot-maven-plugin</artifactId>
41             </plugin>
42         </plugins>
43     </build>
44 </project>

添加主启动类EurekaServerApplication_2002,EurekaServerApplication_2003注解

@EnableEurekaServer
@SpringBootApplication

3、前面单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,麻烦,这里有简单办法,直接配置本机hosts,来实现本机域名映射;

找到 C:\Windows\System32\drivers\etc  打开hosts,加配置 

修改application.yml文件,主要是修改 hostname和defaultZone,

2001

 1 server:
 2   port: 2001
 3   context-path: /
 4 
 5 eureka:
 6   instance:
 7     # 单机 hostname: localhost #eureka注册中心实例名称
 8     hostname: eureka2001.javaqi.com # 集群
 9   client:
10     register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
11     service-url:
12       defaultZone: http://eureka2002.javaqi.com:2002/eureka/,http://eureka2003.javaqi.com:2003/eureka/ # 集群
13       #单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka注册中心交互的地址,查询服务和注册服务用到

2002

 1 server:
 2   port: 2002
 3   context-path: /
 4 eureka:
 5   instance:
 6     # 单机 hostname: localhost #eureka注册中心实例名称
 7     hostname: eureka2002.javaqi.com # 集群
 8   client:
 9     register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
10     service-url:
11       defaultZone: http://eureka2001.javaqi.com:2001/eureka/,http://eureka2003.javaqi.com:2003/eureka/ # 集群
12       #单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka注册中心交互的地址,查询服务和注册服务用到

2003

 1 server:
 2   port: 2003
 3   context-path: /
 4 eureka:
 5   instance:
 6     # 单机 hostname: localhost #eureka注册中心实例名称
 7     hostname: eureka2003.javaqi.com # 集群
 8   client:
 9     register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
10     service-url:
11       defaultZone: http://eureka2001.javaqi.com:2001/eureka/,http://eureka2002.javaqi.com:2002/eureka/ # 集群
12       #单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka注册中心交互的地址,查询服务和注册服务用到

修改服务提供者项目的application.yml,主要修改eureka.client.service-url.defaultZone

 1 server:
 2   port: 2001
 3   context-path: /
 4 eureka:
 5   instance:
 6     hostname: eureka2001.javaqi.com
 7   client:
 8     register-with-eureka: false
 9     service-url:
10       defaultZone: http://eureka2002.javaqi.com:2002/eureka/,http://eureka2003.javaqi.com:2003/eureka/ # 集群

启动三个注册中心,以及服务提供者项目;

浏览器地址栏访问

 

骚操作

删除之前的三个项目

 

 创建一个microservice-eureka-server(三合一)子工程

配置pom

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.javaqi</groupId>
 7         <artifactId>qimicroservice</artifactId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <artifactId>microservice-eureka-server</artifactId>
11 
12     <properties>
13         <java.version>1.8</java.version>
14     </properties>
15 
16     <dependencies>
17         <dependency>
18             <groupId>org.springframework.cloud</groupId>
19             <artifactId>spring-cloud-starter-eureka-server</artifactId>
20         </dependency>
21 
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-test</artifactId>
25             <scope>test</scope>
26         </dependency>
27 
28         <!--  修改后立即生效,热部署  -->
29         <dependency>
30             <groupId>org.springframework</groupId>
31             <artifactId>springloaded</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-devtools</artifactId>
37         </dependency>
38     </dependencies>
39 
40     <build>
41         <plugins>
42             <plugin>
43                 <groupId>org.springframework.boot</groupId>
44                 <artifactId>spring-boot-maven-plugin</artifactId>
45             </plugin>
46         </plugins>
47     </build>
48 
49 
50 </project>

Yml文件

 1 ---
 2 server:
 3   port: 2001
 4   context-path: /
 5 eureka:
 6   instance:
 7     hostname: eureka2001.javaqi.com
 8   client:
 9     register-with-eureka: false
10     service-url:
11       defaultZone: http://eureka2002.javaqi.com:2002/eureka/,http://eureka2003.javaqi.com:2003/eureka/
12 spring:
13   profiles: eureka2001
14 ---
15 server:
16   port: 2002
17   context-path: /
18 eureka:
19   instance:
20     hostname: eureka2002.javaqi.com
21   client:
22     register-with-eureka: false
23     service-url:
24       defaultZone: http://eureka2001.javaqi.com:2001/eureka/,http://eureka2003.javaqi.com:2003/eureka/
25 spring:
26   profiles: eureka2002
27 ---
28 server:
29   port: 2003
30   context-path: /
31 eureka:
32   instance:
33     hostname: eureka2003.javaqi.com
34   client:
35     register-with-eureka: false
36     service-url:
37       defaultZone: http://eureka2001.javaqi.com:2001/eureka/,http://eureka2002.javaqi.com:2002/eureka/
38 spring:
39   profiles: eureka2003

启动类MicroserviceEurekaServerApplication.java

 1 package com.javaqi.microserviceeurekaserver;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 6 
 7 @EnableEurekaServer
 8 @SpringBootApplication
 9 public class MicroserviceEurekaServerApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(MicroserviceEurekaServerApplication.class, args);
13     }
14 
15 }

配置

 效果跟普通操作的效果是一致的

Eureka自我保护机制

开发环境,我们经常会遇到这个红色的警告;

当我们长时间为访问服务以及变更服务实例名称的时候,就会出现这个红色警告;

默认情况,如果服务注册中心再一段时间内没有接收到某个微服务实例的心跳,服务注册中心会注销该实例(默认90秒)。

由于正式环境,经常会有网络故障,网络延迟问题发生,服务和注册中心无法正常通信,此时服务是正常的,不应该注销该服务,Eureka这时候,就通过“自我保护模式”来解决问题,当短时间和服务失去通信时,保留服务信息,当恢复网络和通信时候,退出“自我保护模式”;

通过“自我保护模式”,使Eureka集群更加的健壮和稳定;

 

 

 

posted @ 2019-12-06 18:00  Mr.XiaoQi  阅读(207)  评论(0编辑  收藏  举报