Eureka集群
Eureka集群搭建
高可用集群配置
当注册中心扛不住高并发的时候,这时候 要用集群来扛;
普通操作
我们再新建两个module microservice-eureka-server-2002 microservice-eureka-server-2003
1、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.</modelVersion>
<parent>
<groupId>com.jt</groupId>
<artifactId>microservice</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>
2、2002 2003的主启动类EurekaServerApplication_2002,EurekaServerApplication_2003复制修改下;
package com.jt.microserviceeurekaserver2002; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaServer2002Application { public static void main(String[] args) {
SpringApplication.run(MicroserviceEurekaServer2002Application.class, args);
} }
3、前面单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,麻烦,这里有简单办法,直接配置本机hosts,来实现本机域名映射;
找到 C:\Windows\System32\drivers\etc 打开hosts,加配置
127.0.0.1 eureka2001.jt.com
127.0.0.1 eureka2002.jt.com
127.0.0.1 eureka2003.jt.com
修改三个项目的application.yml文件,主要是修改 hostname和defaultZone,
2001修改:
server:
port:
context-path: / eureka:
instance:
hostname: eureka2001.jt.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2002.jt.com:2002/eureka/,http://eureka2003.jt.com:2003/eureka/ # 集群
2002
server:
port:
context-path: / eureka:
instance:
hostname: eureka2002.jt.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2001.jt.com:2001/eureka/,http://eureka2003.jt.com:2003/eureka/
2003
server:
port:
context-path: / eureka:
instance:
hostname: eureka2003.jt.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2001.jt.com:2001/eureka/,http://eureka2002.jt.com:2002/eureka/
骚操作
上面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.</modelVersion>
<parent>
<groupId>com.jt</groupId>
<artifactId>microservice</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:
context-path: /
eureka:
instance:
hostname: eureka2001.jt.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka2002.jt.com:2002/eureka/,http://eureka2003.jt.com:2003/eureka/
spring:
profiles: eureka2001
---
server:
port:
context-path: /
eureka:
instance:
hostname: eureka2002.jt.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka2001.jt.com:2001/eureka/,http://eureka2003.jt.com:2003/eureka/
spring:
profiles: eureka2002
---
server:
port:
context-path: /
eureka:
instance:
hostname: eureka2003.jt.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka2001.jt.com:2001/eureka/,http://eureka2002.jt.com:2002/eureka/
spring:
profiles: eureka2003
启动类MicroserviceEurekaServerApplication.java
package com.jt.microserviceeurekaserver; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(MicroserviceEurekaServerApplication.class, args);
} }
跑起来的效果跟普通操作的效果是一致的;
Eureka自我保护机制
开发环境,我们经常会遇到这个红色的警告;
当我们长时间为访问服务以及变更服务实例名称的时候,就会出现这个红色警告;
默认情况,如果服务注册中心再一段时间内没有接收到某个微服务实例的心跳,服务注册中心会注销该实例(默认90秒)。
由于正式环境,经常会有网络故障,网络延迟问题发生,服务和注册中心无法正常通信,此时服务是正常的,不应该注销该服务,Eureka这时候,就通过“自我保护模式”来解决问题,当短时间和服务失去通信时,保留服务信息,当恢复网络和通信时候,退出“自我保护模式”;
通过“自我保护模式”,使Eureka集群更加的健壮和稳定;
最新文章
- Jquery remove 高级用法
- Combination Sum II
- IE11下Forms身份认证无法保存Cookie的问题
- 理解MVC模式
- 35.在PCB中删除元件
- linux下的oracle11gR2静默安装,经验分享
- Python将列表中的string元素进行类型转换
- 记录 serverSocket socket 输入,输出流,关闭顺序,阻塞,PrintWriter的一些问题.
- Java中HashMap底层实现原理(JDK1.8)源码分析
- 理解koa2 之 async + await + promise
- 【bfs】1252 走迷宫
- React Router API文档
- InstallShield2015制作安装包----------安装后实现电脑开机自启动
- 51nod 1351 吃点心(贪心)
- 利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD
- linux找到目录下所有目录文件
- LinkedList ArrayList 比较
- [unity3d]角色控制器组件相互间不碰撞
- iptables的地址取反操作
- programming-languages学习笔记--第3部分
热门文章
- CodeForces985G Team Players
- 【Leetcode 做题学算法周刊】第六期
- WPF 画一个3D矩形并旋转
- ARTS-S sed替换
- numpy sum axis详解
- iOS开发 为何 大不如前?原因竟然是这个?
- eclipse新建maven项目报错Could not resolve arachetype org.apache.maven.archetypes:mmaven-archetype-quickstart:1.1 from any of the configured repositories
- python爬虫--代理.让你的ip在坚挺一会!!
- JS基础知识——变量类型和计算(一)
- flink time and watermark