关于Spring Cloud Eureka的核心概念
Eureka 是 Netflix 开源的一款基于 REST 的服务发现工具,主要用于中间层服务器的云端负载均衡。它通过维护一个服务注册表来实现服务之间的通信1。在 Spring Cloud 中,Eureka 提供了一个高可用的服务注册与发现机制,使得微服务架构中的各个模块可以动态地加入或退出集群
一、核心工作原理
服务注册
服务提供者(Eureka Client)启动时,向Eureka Server发送元数据(IP、端口、健康状态等)完成注册,注册表存储结构为:Map<String, Map<String, Lease<InstanceInfo>>>Map<String, Map<String, Lease<InstanceInfo>>>
其中外层Map键为服务名称,内层Map键为实例ID2。
服务续约
客户端每30秒发送心跳包(eureka.instance.lease-renewal-interval-in-seconds
),服务端收到后更新租约时间。若90秒未收到心跳(eureka.instance.lease-expiration-duration-in-seconds
),标记实例不可用。服务发现
消费者通过服务名称向Eureka Server获取实例列表,默认每30秒更新本地缓存(eureka.client.registry-fetch-interval-seconds
)。服务剔除
Eureka Server每60秒检查失效实例(eureka.server.eviction-interval-timer-in-ms
),启动自我保护模式时暂停剔除
二、服务端配置(EUREKA SERVER)
依赖配置
在pom.xml
中添加Eureka Server依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
接着,在启动类上添加 @EnableEurekaServer
注解即可启用 Eureka Server 功能
配置文件设置
以下是典型的 application.yml
或 application.properties
文件配置示例,用于初始化单节点的 Eureka Server:
server:
port: 8761 # Eureka默认端口
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false # 服务端无需自我注册
fetch-registry: false # 不拉取注册表
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类注解
添加@EnableEurekaServer
以激活服务端:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
三、客户端配置(EUREKA CLIENT)
依赖配置
客户端需引入spring-cloud-starter-netflix-eureka-client
:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
配置文件
application.yml
spring: application: name: user-service # 服务名称 eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ # 注册到Eureka Server
启动类注解
使用@EnableDiscoveryClient
或@EnableEurekaClient
(两者等效):
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
四、核心代码实现步骤
服务注册
客户端启动后自动向Eureka Server注册,可通过http://localhost:8761
查看注册列表。- .服务发现
使用DiscoveryClient
动态获取其他服务实例
@Autowired
private DiscoveryClient discoveryClient;
public List<ServiceInstance> getServiceInstances(String serviceName) {
return discoveryClient.getInstances(serviceName);
}
3. 服务消费者调用
@RestController
public class ConsumerController {
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/call")
public String callService() {
List<ServiceInstance> instances = discoveryClient.getInstances("PROVIDER-SERVICE");
// 负载均衡逻辑...
}
}
五、高级配置示例
高可用集群
部署多个Eureka Server并相互注册YAML:
# 节点1配置
eureka:
client:
service-url:
defaultZone: http://eureka-node2:8762/eureka/
# 节点2配置
eureka:
client:
service-url:
defaultZone: http://eureka-node1:8761/eureka/
安全认证
添加Spring Security依赖并配置YAML:
eureka:
client:
service-url:
defaultZone: http://user:password@localhost:8761/eureka/
健康检查与元数据
启用健康检查并自定义元数据YAML
eureka:
instance:
health-check-url-path: /actuator/health
metadata-map:
zone: us-east