EurekaServer集群高可用配置

本文详细介绍如何通过配置三个Eureka Server实现集群高可用,包括Maven依赖、YAML配置及服务注册流程,确保微服务架构下服务发现的稳定性和可靠性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Eureka集群高可用配置:

    Server端配置:

    1,创建一个eurekaserver1模块,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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.QuanTong</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eurekaserver1</artifactId>
    <packaging>jar</packaging>
    <dependencies>

        <!-- 服务发现Eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.yml配置文件:

​
---
spring:
  application:
    name: eurekaserver
​

2,我用一个项目来启三个EurekaServer服务,占用三个不同端口,实现Eureka集群服务。

添加application-peer1.yml配置文件,peer1服务端口号:8761

---
server:
  port: 8761
---
eureka:
  instance:
    hostname: peer1
#   在Eureka中显示真实IP:端口号
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    leaseRenewalIntervalInSeconds: 10
    leaseExpirationDurationInSeconds: 30
  client:
#   表示是否将自己注册到Eureka Server,默认为true
#    registerWithEureka: false
#   表示是否从Eureka Server获取注册信息,默认为true
    fetchRegistry: false
#    注册到另外两个Server端
    serviceUrl:
      defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/
    server:
      enableSelfPreservation: false
      evictionIntervalTimerInMs: 4000

添加application-peer2.yml配置文件,peer2服务端口号:8762

---
server:
  port: 8762
---
eureka:
  instance:
    hostname: peer2
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    leaseRenewalIntervalInSeconds: 10
    leaseExpirationDurationInSeconds: 30
  client:
#  表示是否将自己注册到Eureka Server,默认为true
#    registerWithEureka: false
#    表示是否从Eureka Server获取注册信息,默认为true
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/
    server:
      enableSelfPreservation: false
      evictionIntervalTimerInMs: 4000


添加application-peer3.yml配置文件,peer3服务端口号:8763

---
server:
  port: 8763
---
eureka:
  instance:
    hostname: peer3
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    leaseRenewalIntervalInSeconds: 10
    leaseExpirationDurationInSeconds: 30
  client:
#  表示是否将自己注册到Eureka Server,默认为true
#    registerWithEureka: false
#    表示是否从Eureka Server获取注册信息,默认为true
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer3:8762/eureka/
    server:
      enableSelfPreservation: false
      evictionIntervalTimerInMs: 4000

3,项目中,我们制定了三个不同的端口,我们将peer1的service-url指定到peer2,peer3,其余两个分别指定到另外两个服务,以此来实现三个Server服务之间的相互注册。

由于我们使用了http://peer1这种写法,需要配一下host。Windows的host在C:\Windows\System32\drivers\etc\host,mac的在/private/etc

e2662fd3ca1556298d508ad1e8d4accfdc6.jpg


然后在启动类上加上EnableEurekaServer注解即可。

dc739ae54243c9d8fa163c96b26ae798613.jpg4,下面我们来看如何分别用peer1、peer2和peer3三个配置启动三个server服务。

931be48a8720e99f7e99f0150a19db8b443.jpg

分别启动三个Server服务,如下显示,三个服务已经互相注册

40e5615287a79ae5164f4a3e121785121b6.jpg

client端配置

新建一个Management项目。

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<parent>
		<artifactId>springcloud</artifactId>
		<groupId>com.chinamobile</groupId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>

	<artifactId>management</artifactId>
	<packaging>jar</packaging>

	<!-- 引入依赖 -->
	<dependencies>
		<!-- 服务发现Eureka -->
		
			<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>
		</dependency>

	</dependencies>

	<!-- 项目构建 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

application.yml配置如下

---
spring:
  profiles:
    active: dev
  application:
    name: management
---
server:
  port: 10086

application-dev.yml配置,只注册到三个注册中心的一个上,其余两个也会自动去注册到

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    leaseRenewalIntervalInSeconds: 10
    leaseExpirationDurationInSeconds: 30

查看一下三个注册中心,Management服务已经全部注册到注册中心:cb1c22da5ed332964852944fd5350c41aa3.jpg

其实这样就已经达到了我们的目的,所有的客户端都只指向一个eureka server地址,至于server端是怎么做高可用、怎么处理单体故障是客户端不关心的。倘若client端配置了所有server的地址,那么每当server增加删除了一个服务后,客户端就需要随之改变,那不是我们希望看到的。测试很简单,我们直接关掉peer1,然后看看peer2和peer3是否还能维持住client的发现。

关掉peer1,8761的网页已经打不开了,8762、8763上也已经没有了8761的服务发现,peer2、peer3控制台在一直报错注册不到peer1

就这样,EurekaServer集群高可用配置就配置完成了

转载于:https://my.oschina.net/bugkiller/blog/1862032

### 配置和部署高可用的Eureka Server集群 为了确保Eureka Server集群高可用性,在微服务架构中,多个Eureka服务器实例会互相注册并同步它们的服务列表。当一个Eureka服务器不可用时,客户端和服务端能够自动切换至其他健康的Eureka服务器节点继续工作。 #### 设置Eureka Server之间的互连互通 每个Eureka Server应当被配置成能与其他Eureka Servers通信,这意味着每一个Eureka Server不仅作为服务提供者向其它Eureka Servers注册自己,同时也作为一个消费者来获取来自其他Eureka Servers的信息[^2]。这种设计允许即使部分网络分区发生的情况下也能维持一定的功能正常运作。 对于具体的实现方式来说: - **启动多个Eureka Server实例**:在同一台机器或者不同的物理/虚拟主机上启动至少两个以上的Eureka Server实例。 - **修改application.yml文件中的eureka.client.serviceUrl.defaultZone属性**:指定其他的Eureka Server地址以便于相互注册与数据复制。例如,如果存在三个分别监听7001、7002、7003端口的Eureka Server,则可以在各自的`application.yml`里设置如下所示的内容以形成环形拓扑结构[^4]: ```yaml server: port: 7001 spring: application: name: eureka-server eureka: client: service-url: defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/ ``` 上述配置使得当前Eureka Server将尝试连接另外两台位于本地环境下的Eureka Server进行信息交换。同样地,也需要相应调整其余两个Eureka Server上的配置文件指向剩余成员。 #### 启用自我保护模式 默认情况下,Eureka具有自我保护机制,该特性旨在防止因短暂性的网络波动而导致大量健康的服务实例被错误地标记为下线状态。然而,在某些场景下可能希望禁用此行为从而更严格地管理失效检测过程。可以通过在`application.yml`中加入下面这段话来关闭这一选项: ```yaml eureka: server: enable-self-preservation: false ``` 值得注意的是,这样做可能会增加误判的风险,因此需谨慎评估实际需求后再做决定。 #### 添加@EnableEurekaServer注解 最后一步是在主应用程序类上面加上`@EnableEurekaServer`注解,表明这是一个Eureka Server应用,并通过Spring Boot的方式启动它[^5]: ```java package cn.kitey.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaMain7001 { public static void main(String[] args) { SpringApplication.run(EurekaMain7001.class, args); } } ``` 完成以上步骤之后就可以成功建立一个多活冗余的Eureka Server集群了。随着更多Eureka Server加入进来,整个系统的健壮性和容错能力都会得到显著提升。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值