SpringBoot3.5.12集成Eureka注册中心
Spring Boot 3.5.12 创建 Eureka 注册中心
1. 环境与依赖准备
由于 Spring Boot 3.x 版本对应 Spring Framework 6.x,其依赖管理与 Spring Boot 2.x 有显著差异。Eureka 作为 Netflix OSS 组件,其原生依赖在 Spring Boot 3.x 中已不被官方 Spring Cloud 直接支持(Spring Cloud Netflix 项目已进入维护模式)。因此,需要引入特定的 Spring Cloud 版本和依赖配置 。
创建一个标准的 Maven 项目,其 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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka-server</artifactId>
<version>1.0.0</version>
<name>eureka-server</name>
<description>Eureka Server for Spring Boot 3.5.12</description>
<properties>
<java.version>17</java.version>
<!-- 使用 Spring Cloud 2023.0.x 版本,这是兼容 Spring Boot 3.x 的版本系列 -->
<spring-cloud.version>2023.0.4</spring-cloud.version>
</properties>
<dependencies>
<!-- Eureka Server 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Boot Web 依赖,提供Web容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 可选:用于健康检查等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- 引入 Spring Cloud 依赖管理,统一管理版本 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
关键点说明:
- Spring Cloud 版本选择:必须使用
2023.0.x或更高版本,这是官方为 Spring Boot 3.x 设计的兼容版本 。 - Java 版本:Spring Boot 3.x 要求 Java 17 或更高版本。
- 依赖管理:通过
<dependencyManagement>引入spring-cloud-dependencies的 BOM (Bill of Materials),确保所有 Spring Cloud 组件版本一致,避免冲突 。
2. 核心配置详解
创建 application.yml (或 application.properties) 配置文件,这是 Eureka Server 运行的核心。
server:
port: 8761 # Eureka Server 默认端口
spring:
application:
name: eureka-server # 服务名称,用于在集群中标识自身
eureka:
instance:
hostname: localhost # 实例的主机名
# 在云环境或容器中,通常需要配置 prefer-ip-address: true 和 ip-address
client:
# 由于自身是 Server,不需要向其他 Server 注册自己
register-with-eureka: false
# 由于自身是 Server,不需要从其他 Server 获取注册表
fetch-registry: false
# Eureka Server 的服务端地址配置
service-url:
# 默认注册中心地址,单机模式下指向自己
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
# 关闭自我保护模式(适合开发测试环境,生产环境建议开启)
enable-self-preservation: false
# 清理无效服务实例的间隔时间(毫秒)
eviction-interval-timer-in-ms: 5000
# Actuator 端点暴露配置(可选,用于监控)
management:
endpoints:
web:
exposure:
include: health,info,metrics
配置项解析:
| 配置项 | 说明 | 建议值/场景 |
|---|---|---|
eureka.client.register-with-eureka | 是否将自己作为客户端注册到其他 Eureka Server。 | 单机模式设为 false;在 Eureka 集群模式下,每个 Server 节点需要相互注册,应设为 true 。 |
eureka.client.fetch-registry | 是否从其他 Eureka Server 拉取注册表信息。 | 单机模式设为 false;集群模式下设为 true,以实现服务列表同步 。 |
eureka.client.service-url.defaultZone | Eureka Server 的通信地址。 | 单机指向自己;集群模式下,需配置所有对等节点的地址,用逗号分隔,例如:http://peer1:8761/eureka/,http://peer2:8761/eureka/ 。 |
eureka.server.enable-self-preservation | 是否启用自我保护模式。 | 开发测试环境可关闭 (false),便于快速感知服务下线。生产环境强烈建议开启 (true),防止因网络波动导致大量服务被误剔除 。 |
eureka.server.eviction-interval-timer-in-ms | 服务剔除任务执行间隔。 | 默认 60 秒。调小此值(如 5 秒)可使测试时服务下线更快被感知。 |
3. 启动类与注解
创建 Spring Boot 应用的主启动类,并使用 @EnableEurekaServer 注解将其声明为 Eureka 服务注册中心。
package com.example.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // 核心注解:启用 Eureka 服务器功能
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
@EnableEurekaServer 注解是启动 Eureka 服务端功能的关键,它会自动配置必要的 Bean 和端点 。
4. 运行与验证
- 启动服务:运行
EurekaServerApplication的main方法。 - 访问控制台:在浏览器中访问
http://localhost:8761。你将看到 Eureka 自带的监控管理界面 。 - 界面解读:
- DS Replicas:显示 Eureka Server 集群中的副本节点。单机模式下通常为空或显示自身。
- Instances currently registered with Eureka:展示当前已注册到本服务器的所有服务实例列表。初始状态下应为空。
- General Info 和 Instance Info:显示服务器的运行状态、元数据等信息。
5. 服务提供者注册示例
为了验证 Eureka Server 工作正常,可以创建一个简单的服务提供者(Eureka Client)进行注册 。
服务提供者 pom.xml 关键依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
服务提供者 application.yml 配置:
server:
port: 8081 # 服务端口
spring:
application:
name: service-provider # 服务名称,用于在Eureka中标识
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/ # 指向刚才创建的Eureka Server
instance:
prefer-ip-address: true # 优先使用IP地址进行注册,而非主机名
服务提供者启动类:
package com.example.serviceprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // 或使用 @EnableEurekaClient (Eureka专用)
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
启动该服务提供者后,刷新 Eureka Server 的控制台页面 (http://localhost:8761),你将在 Instances currently registered with Eureka 部分看到名为 SERVICE-PROVIDER 的服务实例及其状态(UP)。
6. 注意事项与高级配置
- 高可用集群:生产环境必须部署 Eureka 集群以实现高可用。每个 Eureka Server 节点都需要将
register-with-eureka和fetch-registry设置为true,并在service-url.defaultZone中配置所有其他对等节点的地址 。 - 安全认证:可以为 Eureka Server 添加 Spring Security 依赖,在配置中启用 HTTP Basic 认证,以保护管理界面和注册端点。
- 服务发现与调用:服务消费者(另一个 Client)可以通过
DiscoveryClient或RestTemplate/WebClient配合@LoadBalanced注解,根据服务名从 Eureka 获取实例列表并进行负载均衡调用 。 - 与 Nacos 对比:在 Spring Cloud Alibaba 生态中,Nacos 同时提供了服务注册发现和配置中心功能,且对 Spring Boot 3.x 的支持更原生,是新项目更常见的选择 。Eureka 更适合维护存量项目或特定技术栈。
参考来源
- SpringCloud Eureka(注册中心)
- SpringCloud Alibaba - Eureka注册中心,Nacos配置中心
- Springboot3+cloud 2023 进行 Eureka 学习
- 使用SpringCloud搭建Eureka注册中心
- springboot搭建Eureka注册中心
- 创建Eureka Server(注册中心)详解
更多推荐



所有评论(0)