核心概念

  1. 服务注册中心:微服务架构中的核心组件,管理所有服务的注册与发现
  2. 服务注册(Register):服务启动时向Eureka注册自身信息(IP、端口、健康检查URL)
  3. 服务续约(Renew):客户端每30秒发送心跳包维持注册状态
  4. 服务下线(Cancel):服务关闭时主动发送下线请求
  5. 服务剔除(Eviction):Eureka Server检测90秒无心跳的服务自动移除
  6. 自我保护模式:当85%以上客户端心跳丢失时,停止剔除服务(网络分区保护)

详细搭建步骤(Spring Boot 3.x + Spring Cloud 2022.x)

环境准备

  • JDK 17+
  • Maven 3.8+
  • Spring Boot 3.1.4
  • Spring Cloud 2022.0.4

步骤 1:创建项目

# 使用Spring Initializr创建
curl https://start.spring.io/starter.zip \
  -d type=maven-project \
  -d language=java \
  -d bootVersion=3.1.4 \
  -d baseDir=eureka-server \
  -d groupId=com.example \
  -d artifactId=eureka-server \
  -d dependencies=cloud-eureka-server \
  -o eureka-server.zip
unzip eureka-server.zip

步骤 2:配置核心文件

src/main/resources/application.yml

server:
  port: 8761

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false  # 单机模式不注册自己
    fetch-registry: false        # 不获取注册表
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    enable-self-preservation: true  # 开启自我保护
    eviction-interval-timer-in-ms: 10000  # 清理间隔(ms)

步骤 3:添加启动注解

src/main/java/com/example/EurekaServerApplication.java

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

步骤 4:启动服务

mvn spring-boot:run
# 访问 http://localhost:8761

集群部署方案(3节点示例)

节点配置

node1 application.yml

server:
  port: 8761
eureka:
  instance:
    hostname: node1
  client:
    service-url:
      defaultZone: http://node2:8762/eureka,http://node3:8763/eureka

node2 application.yml

server:
  port: 8762
eureka:
  instance:
    hostname: node2
  client:
    service-url:
      defaultZone: http://node1:8761/eureka,http://node3:8763/eureka

node3 application.yml

server:
  port: 8763
eureka:
  instance:
    hostname: node3
  client:
    service-url:
      defaultZone: http://node1:8761/eureka,http://node2:8762/eureka

主机映射(所有节点)

# /etc/hosts
127.0.0.1 node1
127.0.0.1 node2
127.0.0.1 node3

常见错误与解决方案

  1. 注册服务显示为UNKNOWN

    • 原因:未配置spring.application.name
    • 解决:在客户端添加:
      spring:
        application:
          name: user-service
      
  2. 服务无法注册

    • 检查点:
      • Eureka Server地址是否正确
      • 客户端是否添加依赖:
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        
  3. 自我保护模式警告

    • 现象:页面显示 "RENEWALS ARE LESSER THAN THE THRESHOLD"
    • 处理:
      • 生产环境:检查网络分区问题
      • 测试环境:临时关闭自我保护
        eureka:
          server:
            enable-self-preservation: false
        
  4. Zone配置错误

    • 正确配置:
      eureka:
        client:
          availability-zones:
            region1: zone-a,zone-b
          region: region1
          service-url:
            zone-a: http://node1:8761/eureka/
            zone-b: http://node2:8762/eureka/
      

关键注意事项

  1. 生产环境必须部署集群(至少3节点避免脑裂)
  2. 开启安全认证(防止未授权访问):
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.csrf().disable()
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .httpBasic();
        return http.build();
    }
    
  3. 客户端重试机制
    spring:
      cloud:
        loadbalancer:
          retry:
            enabled: true
    eureka:
      client:
        on-demand-update-status-change: true
    

性能优化技巧

  1. 调整心跳间隔(根据网络状况)

    # 客户端配置
    eureka:
      instance:
        lease-renewal-interval-in-seconds: 15  # 心跳间隔(默认30s)
        lease-expiration-duration-in-seconds: 30 # 超时时间(默认90s)
    
  2. 启用响应缓存

    eureka:
      server:
        response-cache-update-interval-ms: 30000  # 缓存更新间隔
    
  3. 注册表存储优化

    @Bean
    public EurekaServerConfigBean eurekaServerConfig() {
      EurekaServerConfigBean config = new EurekaServerConfigBean();
      config.setRegistrySyncRetries(5); // 注册表同步重试次数
      return config;
    }
    

监控与运维

  1. 健康检查端点
    GET /actuator/health
    
  2. 元数据查询
    GET /eureka/apps
    
  3. Prometheus监控集成
    <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
    
    management:
      endpoints:
        web:
          exposure:
            include: health,info,prometheus
    

最佳实践总结

  1. 集群部署:生产环境至少3节点跨可用区部署
  2. 安全加固
    • 启用HTTPS
    • 集成Spring Security基础认证
    • 配置网络ACL限制访问IP
  3. 客户端配置
    eureka:
      client:
        healthcheck:
          enabled: true  # 启用健康检查
        fetch-remote-regions: "region1" # 指定区域
    
  4. 灾备方案
    • 配置备份注册中心(如Consul备用)
    • 实现注册信息持久化到数据库
  5. 版本管理:保持Spring Cloud与Eureka版本兼容 | Spring Cloud | Eureka Client | |-------------|---------------| | 2022.0.x | 4.0.x | | 2021.0.x | 3.1.x |

关键提示:在Kubernetes环境中建议使用K8s原生服务发现,Eureka更适用于虚拟机环境或混合云场景。

通过以上配置和优化,可构建出支撑千级服务实例的高可用注册中心,平均注册延迟可控制在200ms以内,完全满足生产环境需求。