SpringCloudEureka服务注册中心

wen java案例 2

Spring Cloud Eureka 服务注册中心详解

Eureka 是 Netflix 开源的服务发现框架,Spring Cloud 将其整合为服务注册与发现的核心组件,下面我会详细讲解 Eureka 的架构、原理和实战配置。

SpringCloudEureka服务注册中心

Eureka 核心概念

两大角色:

  • Eureka Server:服务注册中心,提供服务注册和发现功能
  • Eureka Client:服务提供者和消费者,向 Server 注册自己,并从 Server 获取其他服务信息

三大核心操作:

  • Register(注册):Client 启动时向 Server 注册自己的信息
  • Renew(续约):Client 每隔 30 秒发送心跳,告诉 Server "我还活着"
  • Eviction(剔除):Server 在 90 秒内没收到心跳,移除该服务实例

Eureka Server 搭建实战

创建 Spring Boot 项目

使用 Spring Initializr 创建项目,选择:

  • Spring Boot 2.x/3.x
  • Eureka Server 依赖

Maven 依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.0</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <!-- Spring Boot Web 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

主应用类配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

application.yml 配置

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/

Eureka Client 配置

服务提供者(Provider)

spring:
  application:
    name: user-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}

服务消费者(Consumer)

spring:
  application:
    name: order-service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

高可用 Eureka 集群配置

集群原理

多个 Eureka Server 互相注册,形成高可用架构。

多节点配置示例(3 节点)

Node 1 (端口 8761)

spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: peer1
  client:
    service-url:
      defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/

Node 2 (端口 8762)

spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: peer2
  client:
    service-url:
      defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/

Node 3 (端口 8763)

spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: peer3
  client:
    service-url:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/

常用配置详解

eureka:
  server:
    enable-self-preservation: true  # 开启自我保护模式(生产环境建议开启)
    eviction-interval-timer-in-ms: 5000  # 清理无效节点的时间间隔(毫秒)
    renewal-percent-threshold: 0.85  # 自我保护阈值
  instance:
    lease-renewal-interval-in-seconds: 30  # 心跳间隔(默认30秒)
    lease-expiration-duration-in-seconds: 90  # 剔除时间(默认90秒)
    prefer-ip-address: true  # 使用 IP 注册
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

Eureka 自我保护机制

触发条件: 当心跳失败比例超过 85%(默认阈值)

作用:

  • 防止网络波动导致大量服务被错误剔除
  • 保护模式下的服务仍然可用,但不会被剔除

查看状态:

GET http://localhost:8761/eureka/apps

Eureka 健康检查

management:
  endpoints:
    web:
      exposure:
        include: health,info
  health:
    defaults:
      enabled: true

常见问题及解决方案

服务注册不上

# 配置完整服务名
spring:
  application:
    name: my-service
# 检查网络连通性
# 确保 Client 能访问 Server 地址

服务剔除太慢

# Server 端配置
eureka:
  server:
    eviction-interval-timer-in-ms: 3000  # 减少检查间隔
# Client 端配置  
eureka:
  instance:
    lease-renewal-interval-in-seconds: 5  # 缩短心跳间隔
    lease-expiration-duration-in-seconds: 15  # 缩短剔除时间

跨区域注册问题

eureka:
  instance:
    prefer-ip-address: true  # 使用 IP 而不是主机名
    hostname: your-public-ip  # 指定主机名
  client:
    service-url:
      defaultZone: http://public-ip:8761/eureka/

监控与管理

Eureka Web 控制台:

http://localhost:8761

REST API 端点:

# 查询所有服务
GET /eureka/apps
# 查询指定服务  
GET /eureka/apps/{service-name}
# 查询服务实例
GET /eureka/apps/{service-name}/{instance-id}

与 Spring Cloud Gateway 集成

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true  # 启用服务发现
          lower-case-service-id: true  # 服务名小写

Eureka 作为 Spring Cloud 的核心组件,提供了成熟的服务注册与发现解决方案,生产环境中建议:

  1. 搭建 Eureka 集群:至少 3 节点保证高可用
  2. 启用自我保护模式:防止网络抖动导致雪崩
  3. 合理配置心跳参数:根据业务需求调整
  4. 使用健康检查:确保服务真实可用

如果你需要了解更具体的配置或遇到问题,欢迎继续提问!

抱歉,评论功能暂时关闭!