本文目录导读:

Eureka 是 Netflix 开源的、基于 REST 的服务注册与发现组件,通常作为 Spring Cloud 微服务架构的核心注册中心。
核心角色有两个:
- Eureka Server:服务注册中心(类似“通讯录”)。
- Eureka Client:需要被发现的微服务(如订单服务、用户服务)。
以下是详细的使用步骤,假设你使用 Spring Boot 和 Spring Cloud。
第一步:搭建 Eureka Server(注册中心)
创建 Spring Boot 项目
创建一个新的 Maven/Gradle 项目,添加依赖(以 Maven 为例):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.x</version> <!-- 或 3.x 版本,注意兼容性 -->
</parent>
<dependencies>
<!-- Eureka Server 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.x</version> <!-- 对应 Spring Boot 2.7.x -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置文件 application.yml
server:
port: 8761 # Eureka 默认端口
eureka:
instance:
hostname: localhost
client:
# 是否将自己注册到 Eureka Server,作为服务器不需要
register-with-eureka: false
# 是否从 Eureka 获取注册信息,作为服务器不需要
fetch-registry: false
service-url:
# 服务注册地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // 开启 Eureka Server 功能
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
启动后,访问 http://localhost:8761 可以看到 Eureka 的管理控制台页面。
第二步:创建 Eureka Client(服务提供者/消费者)
假设你有一个订单服务(order-service)需要注册到 Eureka。
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
配置文件 application.yml
server:
port: 8100
spring:
application:
name: order-service # 服务名称,用于被其他服务调用
eureka:
client:
service-url:
# 注册中心的地址
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true # 注册时使用 IP 而非主机名
instance-id: ${spring.cloud.client.ip-address}:${server.port} # 实例 ID
主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // 开启服务发现(将服务注册到 Eureka)
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
启动后,刷新 Eureka 控制台(http://localhost:8761),在 Instances currently registered with Eureka 区域应该能看到 ORDER-SERVICE。
第三步:服务调用(客户端负载均衡)
假设你的 user-service 需要调用 order-service,可以使用 RestTemplate + @LoadBalanced 的方式实现负载均衡。
在 UserService 中配置 RestTemplate
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced // 让 RestTemplate 具备 Ribbon 负载均衡能力
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
在 Controller 中使用服务名调用
@RestController
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getOrder")
public String getOrder() {
// 使用服务名称 "order-service" 而不是具体的 IP:Port
String url = "http://order-service/orders/1";
String result = restTemplate.getForObject(url, String.class);
return "User Service Result: " + result;
}
}
Spring Cloud 会自动从 Eureka 获取 order-service 的可用实例列表,并通过轮询(默认)算法选择一台进行访问。
进阶:高可用 Eureka 集群
生产环境通常需要部署 Eureka 集群,让多个 Server 互相注册、数据同步。
双节点示例:
- Server1(端口 8761):
eureka: client: service-url: defaultZone: http://peer2:8762/eureka/ - Server2(端口 8762):
eureka: client: service-url: defaultZone: http://peer1:8761/eureka/
所有 Client 的 defaultZone 配置为:
defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/
常见问题
-
启动报错:
com.netflix.discovery.shared.transport.TransportException- 通常是因为 Client 启动了但 Server 未启动,或者地址配置错误。
- 检查
defaultZone的 URL 是否正确(有无/eureka/后缀)。
-
服务注册后很快消失(自我保护机制)
- 默认 Eureka 有 自我保护机制(
Renew threshold),开发环境可以关闭以方便调试:eureka: server: enable-self-preservation: false eviction-interval-timer-in-ms: 5000 # 清理无效节点间隔(毫秒)
- 默认 Eureka 有 自我保护机制(
-
Eureka 界面显示
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES...这是自我保护机制的红色警告,生产环境正常,开发环境关闭即可。
-
Spring Boot 版本与 Spring Cloud 版本不匹配
务必对照官方版本对应关系(Spring Boot 2.7.x 对应 Spring Cloud 2021.0.x)。
| 角色 | 核心操作 |
|---|---|
| Server | 添加 eureka-server 依赖配置 register-with-eureka: false启动类加 @EnableEurekaServer |
| Client | 添加 eureka-client 依赖配置 spring.application.name 和 defaultZone启动类加 @EnableDiscoveryClient使用 @LoadBalanced 的 RestTemplate 调用 |
Eureka 的使用思路就是:所有的服务(包括网关)启动后向 Eureka 注册,服务之间通过“服务名”相互调用,Eureka 负责维护这份“服务列表”并完成负载均衡。