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集群更加的健壮和稳定;

原文地址:https://www.cnblogs.com/xcn123/p/11994917.html

时间: 2024-07-30 14:47:50

Eureka集群的相关文章

微服务架构:Eureka集群搭建

版权声明:本文为博主原创文章,转载请注明出处,欢迎交流学习! 服务注册.发现是微服务架构的关键原理之一,由于微服务架构是由一系列职责单一的细粒度服务构成的网状结构,服务之间通过轻量机制进行通信,这就必然引入一个服务注册发现的问题,也就是说服务提供方要注册报告服务地址,服务调用方要能发现目标服务.在我们的微服务架构中我们采用了Eureka来完成微服务的注册与发现.微服务通过Eureka进行注册,服务调用方通过Eureka找到目标服务.由于服务提供方以集群方式提供服务,Eureka也采用集群的方式来

eureka集群基于DNS配置方式

最近在研究spring cloud eureka集群配置的时候碰到问题:多台eureka server如果需要互相注册,需要在配置文件中将其他服务器地址配置写死.同样客户端启用服务发现功能(eureka client)也需要配置服务端地址列表(其实eureka server与其他eureka server通信也是用的是eureka client组件).按照官方案例提供3台server,如果现在需要增加第四台,第五台...那么问题就来了,所有eureka client的serverUrls列表是否

SpringCloud之Eureka集群

前面我们介绍了SpringCloud注册中心Eureka,但是存在一个单点故障的问题,一个注册中心远远不能满足实际的生产环境,现在我们介绍一下如何搭建一个Eureka集群. 一:集群环境搭建 我们先建两个注册中心工程,一个叫eureka_register_master,一个叫eureka_register_salve.master的端口是7998,salve的端口是7999. eureka_register_master的配置文件application.properties如下: server.

基于dns搭建eureka集群

eureka集群方案: 1.通常我们部署的eureka节点多于两个,根据实际需求,只需要将相邻节点进行相互注册(eureka节点形成环状),就达到了高可用性集群,任何一个eureka节点挂掉不会受到影响. 2.可能会有初学者和我一样,一开始的时候没有完全理解eureka集群的原理,直接把每个eureka节点的url写进配置文件,期望所有的eureka节点进行相互注册.实际上,节点间进行信息同步的时候,只会选取配置文件第一个eureka的url,除非发生url错误,才会依次选取有效url进行信息同

eureka集群高可用配置

譬如eureka.client.register-with-eureka和fetch-registry是否要配置,配不配区别在哪里:eureka的客户端添加service-url时,是不是需要把所有的eureka的server地址都写上,还是只需要写一个server就可以了(因为server之间已经相互注册了)?如果写上了所有的server地址,那相当于将每个client服务都往所有的server都添加了一遍,那还配置server间的相互注册有什么意义? 上面的这些问题在多数讲eureka集群教

springcloud费话之Eureka集群

  目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud费话之Eureka接口调用(feign) springcloud费话之断路器(hystrix in feign) 一.容灾server集群 复制上例中的server项目两个,分别命名为x-server2和x-server3,修改yml配置 ①端口:三个服务器的端口分别为9010,9011,901

eureka 集群的实现方式?

注意,本文还是对上一篇博客的延续,需要的配置,在前面的博客里面可以找到. eureka集群版 (正宗的eureka!) 2.1.配置eureka的集群之前首先先配置HOSTNAME和IP的映射 具体的路径. // C:\Windows\System32\drivers\etc 在windows系统中修改hosts文件 /* 127.0.0.1(localhost) eureka01 127.0.0.1(localhost) eureka02 127.0.0.1(localhost) eureka

Eureka集群搭建

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&qu

Spring-Cloud学习之Eureka集群搭建

一.为什么需要集群 上一篇文章讲解了如何搭建单个节点的eureka,这篇讲解如何搭建eureka集群,这里的集群还是本地不同的端口执行三个eureka,因为条件不要允许,没有三台电脑,所以大家将就一下吧,eureka集群的目的就是防止一个点故障导致,整个服务瘫痪的问题,成为单点故障,因为一个点出问题,还有另一个点顶上去,代替这个点工作,所以集群也实现了高可用.高性能 二.集群的原理 每一个注册点都配置有其它点的url,能够与其他server点进行数据的同步,当服务向一个点注册时,该店就会把该服务