Eureka Server搭建
2026/1/15大约 4 分钟EurekaEureka ServerSpring Cloud
Eureka Server 搭建
一、环境准备
1.1 版本说明
| 组件 | 版本 |
|---|---|
| JDK | 1.8+ |
| Spring Boot | 2.7.x |
| Spring Cloud | 2021.0.x |
| Eureka | 3.1.x |
1.2 版本对应关系
二、创建 Eureka Server
2.1 创建 Maven 项目
<?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>
<groupId>com.example</groupId>
<artifactId>eureka-server</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.8</spring-cloud.version>
</properties>
<dependencies>
<!-- Eureka Server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Boot Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<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>2.2 启动类
package com.example.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);
}
}2.3 配置文件
# application.yml
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
client:
# 是否将自己注册到 Eureka Server(单机模式设为 false)
register-with-eureka: false
# 是否从 Eureka Server 获取注册信息(单机模式设为 false)
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
# 关闭自我保护模式(开发环境)
enable-self-preservation: false
# 清理无效节点的时间间隔(毫秒)
eviction-interval-timer-in-ms: 50002.4 启动验证
启动应用后,访问 http://localhost:8761,可以看到 Eureka 控制台:
Instances currently registered with Eureka
Application AMIs Availability Zones Status
No instances available三、配置详解
3.1 eureka.instance 配置
| 配置项 | 默认值 | 说明 |
|---|---|---|
hostname | - | 实例主机名 |
prefer-ip-address | false | 是否优先使用 IP 地址 |
ip-address | - | 指定 IP 地址 |
instance-id | - | 实例 ID |
lease-renewal-interval-in-seconds | 30 | 心跳间隔(秒) |
lease-expiration-duration-in-seconds | 90 | 服务过期时间(秒) |
3.2 eureka.client 配置
| 配置项 | 默认值 | 说明 |
|---|---|---|
register-with-eureka | true | 是否注册到 Eureka |
fetch-registry | true | 是否获取注册表 |
service-url.defaultZone | - | Eureka Server 地址 |
registry-fetch-interval-seconds | 30 | 获取注册表间隔 |
3.3 eureka.server 配置
| 配置项 | 默认值 | 说明 |
|---|---|---|
enable-self-preservation | true | 是否开启自我保护 |
eviction-interval-timer-in-ms | 60000 | 清理间隔(毫秒) |
renewal-percent-threshold | 0.85 | 自我保护阈值 |
四、Eureka Server 集群搭建
4.1 集群架构
4.2 配置 hosts 文件
# /etc/hosts 或 C:\Windows\System32\drivers\etc\hosts
127.0.0.1 eureka1
127.0.0.1 eureka2
127.0.0.1 eureka34.3 Eureka Server 1 配置
# application-eureka1.yml
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: eureka1
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka2:8762/eureka/,http://eureka3:8763/eureka/4.4 Eureka Server 2 配置
# application-eureka2.yml
server:
port: 8762
spring:
application:
name: eureka-server
eureka:
instance:
hostname: eureka2
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka1:8761/eureka/,http://eureka3:8763/eureka/4.5 Eureka Server 3 配置
# application-eureka3.yml
server:
port: 8763
spring:
application:
name: eureka-server
eureka:
instance:
hostname: eureka3
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka1:8761/eureka/,http://eureka2:8762/eureka/4.6 启动集群
# 启动三个实例
java -jar eureka-server.jar --spring.profiles.active=eureka1
java -jar eureka-server.jar --spring.profiles.active=eureka2
java -jar eureka-server.jar --spring.profiles.active=eureka3五、安全配置
5.1 添加 Spring Security 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>5.2 配置用户名密码
spring:
security:
user:
name: admin
password: 123456
eureka:
client:
service-url:
# 带认证的地址格式
defaultZone: http://admin:123456@localhost:8761/eureka/5.3 安全配置类
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// 关闭 CSRF
http.csrf().disable();
// 开启认证
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
return http.build();
}
}六、常见问题
6.1 启动报错:Cannot execute request on any known server
原因:Eureka Server 配置了 register-with-eureka: true,但无法连接到其他 Server。
解决:单机模式下设置 register-with-eureka: false 和 fetch-registry: false。
6.2 服务注册后显示 UNKNOWN
原因:未配置 spring.application.name。
解决:在配置文件中添加应用名称。
6.3 控制台显示红色警告
原因:触发了自我保护机制。
解决:开发环境可关闭自我保护:eureka.server.enable-self-preservation: false。
七、总结
本章介绍了 Eureka Server 的搭建:
- 单机模式:适合开发测试环境
- 集群模式:适合生产环境,保证高可用
- 安全配置:添加认证保护注册中心
