Eureka集群搭建
高可用集群配置
当注册中心扛不住高并发的时候,这时候 要用集群来扛;
提示:部分内容请前往上篇博客查找
普通操作
我们再新建两个module microservice-eureka-server-2002 microservice-eureka-server-2003
1、pom.xml 把依赖加下;
microservice-eureka-server-2002
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.yuan</groupId> 7 <artifactId>t226microservice</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 16 <dependencies> 17 <dependency> 18 <groupId>org.springframework.cloud</groupId> 19 <artifactId>spring-cloud-starter-eureka-server</artifactId> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-test</artifactId> 24 <scope>test</scope> 25 </dependency> 26 <!--<!– 修改后立即生效,热部署 –>--> 27 <dependency> 28 <groupId>org.springframework</groupId> 29 <artifactId>springloaded</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework.boot</groupId> 33 <artifactId>spring-boot-devtools</artifactId> 34 </dependency> 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 45 </project>
microservice-eureka-server-2003
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.yuan</groupId> 7 <artifactId>t226microservice</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <artifactId>microservice-eureka-server-2003</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 </dependencies> 35 <build> 36 <plugins> 37 <plugin> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-maven-plugin</artifactId> 40 </plugin> 41 </plugins> 42 </build> 43 44 </project>
2、2002 2003的主启动类EurekaServerApplication_2002,EurekaServerApplication_2003修改下;
MicroserviceEurekaServer2002Application
1 package com.yuan.microserviceeurekaserver2002; 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 MicroserviceEurekaServer2002Application { 10 11 public static void main(String[] args) { 12 SpringApplication.run(MicroserviceEurekaServer2002Application.class, args); 13 } 14 15 }
MicroserviceEurekaServer2003Application
1 package com.yuan.microserviceeurekaserver2003; 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 MicroserviceEurekaServer2003Application { 10 11 public static void main(String[] args) { 12 SpringApplication.run(MicroserviceEurekaServer2003Application.class, args); 13 } 14 15 }
3、前面单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,麻烦,这里有简单办法,直接配置本机hosts,来实现本机域名映射;
找到 C:\Windows\System32\drivers\etc 打开hosts,加配置
修改配置
127.0.0.1 eureka2001.yuan.com 127.0.0.1 eureka2002.yuan.com 127.0.0.1 eureka2003.yuan.com
4、修改三个项目的application.yml文件,主要是修改 hostname和defaultZone,
2001修改:
1 server: 2 port: 2001 3 context-path: / 4 eureka: 5 instance: 6 # 单机 hostname: localhost #eureka注册中心实例名称 7 hostname: eureka2001.yuan.com # 集群 8 client: 9 register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。 11 service-url: 12 defaultZone: http://eureka2002.yuan.com:2002/eureka/,http://eureka2003.yuan.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: eureka2002.yuan.com 7 client: 8 register-with-eureka: false 9 service-url: 10 defaultZone: http://eureka2001.yuan.com:2001/eureka/,http://eureka2003.yuan.com:2003/eureka/
2003修改:
1 server: 2 port: 2003 3 context-path: / 4 eureka: 5 instance: 6 hostname: eureka2003.yuan.com 7 client: 8 register-with-eureka: false 9 service-url: 10 defaultZone: http://eureka2001.yuan.com:2001/eureka/,http://eureka2002.yuan.com:2002/eureka/
完了之后一次启动:2001==》2002==》2003==》==》1001==》81
访问eureka2001.yuan.com:2001
访问eureka2002.yuan.com:2002
访问eureka2003.yuan.com:2003
消费端localhost:81/student/list
“骚操作”
上面eureka服务搭建,除了yml文件不一样,其他文件都一样,
那么我们有什么办法能够将多个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.yuan</groupId> 7 <artifactId>t226microservice</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 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-test</artifactId> 24 <scope>test</scope> 25 </dependency> 26 <!--<!– 修改后立即生效,热部署 –>--> 27 <dependency> 28 <groupId>org.springframework</groupId> 29 <artifactId>springloaded</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework.boot</groupId> 33 <artifactId>spring-boot-devtools</artifactId> 34 </dependency> 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 45 </project>
修改yml文件
1 --- 2 server: 3 port: 2001 4 context-path: / 5 eureka: 6 instance: 7 hostname: eureka2001.yuan.com 8 client: 9 register-with-eureka: false 10 service-url: 11 defaultZone: http://eureka2002.yuan.com:2002/eureka/,http://eureka2003.yuan.com:2003/eureka/ 12 spring: 13 profiles: eureka2001 14 --- 15 server: 16 port: 2002 17 context-path: / 18 eureka: 19 instance: 20 hostname: eureka2002.yuan.com 21 client: 22 register-with-eureka: false 23 service-url: 24 defaultZone: http://eureka2001.yuan.com:2001/eureka/,http://eureka2003.yuan.com:2003/eureka/ 25 spring: 26 profiles: eureka2002 27 --- 28 server: 29 port: 2003 30 context-path: / 31 eureka: 32 instance: 33 hostname: eureka2003.yuan.com 34 client: 35 register-with-eureka: false 36 service-url: 37 defaultZone: http://eureka2001.yuan.com:2001/eureka/,http://eureka2002.yuan.com:2002/eureka/ 38 spring: 39 profiles: eureka2003
修改运行配置
1、选择Eureka2001ServerApplication,点击最左上角复制按钮然后进行下一步操作==》
2、将用红线划中的地方修改
如右边第一处:Eureka2001ServerApplication
第二处:同样
第三处:eureka2001
启动顺序:2001》2002》2003》1001》81
访问结果: 其他所有运行结果同上次是一样的,就不一一演示
Eureka自我保护机制
开发环境,我们经常会遇到这个红色的警告;
当我们长时间为访问服务以及变更服务实例名称的时候,就会出现这个红色警告;
默认情况,如果服务注册中心再一段时间内没有接收到某个微服务实例的心跳,服务注册中心会注销该实例(默认90秒)。
由于正式环境,经常会有网络故障,网络延迟问题发生,服务和注册中心无法正常通信,此时服务是正常的,不应该注销该服务,Eureka这时候,就通过“自我保护模式”来解决问题,当短时间和服务失去通信时,保留服务信息,当恢复网络和通信时候,退出“自我保护模式”;
通过“自我保护模式”,使Eureka集群更加的健壮和稳定;
谢谢观看!!!
原文地址:https://www.cnblogs.com/ly-0919/p/11996514.html