Spring Cloud Eureka 集群高可用配置

本文详细介绍了如何通过配置实现Eureka服务的高可用集群部署,并演示了如何让客户端应用注册到该集群中,确保服务发现的稳定性和可靠性。

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

接上一篇的基础上,实现Eureka简单集群配置,达到高可用要求。Eureka的集群逻辑,和Zookeeper的区别,不做过多介绍


一、修改“ershuai-eureka-server”的application.yml

spring:
  application:
    name: ershuai-eureka-server
  profiles:
    active: 1001 # 這里指定启动的application-1001/1002/1003.yml

二、新建“application-1001/1002/1003.yml”

register-with-eureka、fetch-registry 默认是true

server:
  port: 1001
  
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1002/eureka,http://localhost:1003/eureka
    #register-with-eureka: false # 是否注册自身到eureka服务器
    #fetch-registry: false # 是否从eureka上获取注册信息
  instance:
    hostname: localhost
    instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 指定此实例的ip
    prefer-ip-address: true # 注册时使用ip而不是主机名
  #server:
    #enable-self-preservation: false # 开启保护机制
server:
  port: 1002
  
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1001/eureka,http://localhost:1003/eureka
    #register-with-eureka: false # 是否注册自身到eureka服务器
    #fetch-registry: false # 是否从eureka上获取注册信息
  instance:
    hostname: localhost
    instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 指定此实例的ip
    prefer-ip-address: true # 注册时使用ip而不是主机名
  #server:
    #enable-self-preservation: false # 开启保护机制
server:
  port: 1003
  
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1001/eureka,http://localhost:1002/eureka
    #register-with-eureka: false # 是否注册自身到eureka服务器
    #fetch-registry: false # 是否从eureka上获取注册信息
  instance:
    hostname: localhost
    instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 指定此实例的ip
    prefer-ip-address: true # 注册时使用ip而不是主机名
  #server:
    #enable-self-preservation: false # 开启保护机制


三、分别启动1001/1002/1003端口服务

项目右键 或 Main方法右键 -> Run As -> Run Configurations -> Arguments

设置Name(自定义)

设置Program arguments参数:--spring.profiles.active=1001


点击“Run”启动

這里分别启动1001/1002/1003,需要Run As三次,每次启动一个端口服务

分别访问http://localhost:1001/1002/1003端口

可以看到三个端口的服务都互相注册了



四、注册服务

创建一个“ershuai-eureka-client”子项目

pom.xml文件

<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>${spring-boot.version}</version>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

application.yml文件

spring:
  application:
    name: ershuai-eureka-client

server:
  port: 9001

eureka:
  client:
    service-url:
      defaultZone: http://localhost:1001/eureka,http://localhost:1002/eureka,http://localhost:1003/eureka # 多个以','分隔。也可以只填一个
    #register-with-eureka: false # 是否注册自身到eureka服务器
    #fetch-registry: false # 是否从eureka上获取注册信息
  instance:
    instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 指定此实例的ip
    prefer-ip-address: true # 注册时使用ip而不是主机名

测试controller

@RestController
public class TestController {

	@Autowired
	DiscoveryClient discoveryClient;

	@GetMapping("/test")
	public String test() {
		String services = "Services: " + this.discoveryClient.getServices();
		System.out.println(services);
		return services;
	}
}

Main启动

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClientApplication.class, args);
	}
}

再次访问http://localhost:1001/1002/1003,可以看到9001已注册到Eureka


访问http://localhost:9001/test

可以看到服务正常


尝试关闭1001/1002/1003,任意一个或两个服务,访问http://localhost:9001/test,依然是可以正常运行的



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值