joken-前端工程师

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::
  394 随笔 :: 39 文章 :: 8 评论 :: 20万 阅读

1. 什么是 Spring Cloud Eureka

  • 作用
    • 服务注册:微服务启动时将自身信息(IP、端口、服务名等)注册到 Eureka Server。
    • 服务发现:客户端(包括网关或其他服务)可以从 Eureka Server 获取服务实例的地址,动态调用服务。
  • 核心组件
    • Eureka Server:服务注册中心,负责维护服务实例的注册表。
    • Eureka Client:服务或消费者,注册到 Server 或从 Server 获取服务信息。
  • 特点
    • 高可用(支持集群部署)。
    • 与 Spring Cloud 深度集成(如与 Gateway、Feign 等配合)。

2. 搭建 Eureka Server

首先,我们需要创建一个 Eureka Server,作为服务注册中心。

(1) 创建项目

  1. 使用 Spring Initializr(start.spring.io)创建项目:

    • 依赖:Eureka Server
    • 示例 pom.xml
      <project>
          <modelVersion>4.0.0</modelVersion>
          <groupId>com.example</groupId>
          <artifactId>eureka-server</artifactId>
          <version>1.0-SNAPSHOT</version>
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>3.2.3</version>
          </parent>
          <dependencies>
              <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>2023.0.0</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
              </dependencies>
          </dependencyManagement>
      </project>
      
  2. 主启动类
    添加 @EnableEurekaServer 注解:

    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);
        }
    }
    
  3. 配置文件
    application.yml 中配置 Eureka Server:

    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/
    
  4. 运行

    mvn spring-boot:run
    

    访问 http://localhost:8761,你会看到 Eureka 的管理界面,显示已注册的服务(目前为空)。


3. 创建 Eureka Client(服务提供者)

接下来,创建一个微服务并注册到 Eureka Server。

(1) 创建项目

  1. 依赖:

    <dependencies>
        <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>
    </dependencies>
    
  2. 主启动类
    添加 @EnableDiscoveryClient(可选,Spring Cloud 2020+ 默认启用):

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class UserServiceApplication {
        public static void main(String[] args) {
            SpringApplication.run(UserServiceApplication.class, args);
        }
    }
    
  3. 配置文件

    server:
      port: 8081
    spring:
      application:
        name: user-service # 服务名
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/ # Eureka Server 地址
      instance:
        prefer-ip-address: true # 使用 IP 注册
    
  4. 添加一个简单控制器

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class UserController {
        @GetMapping("/users")
        public String getUsers() {
            return "User Service is running!";
        }
    }
    
  5. 运行

    mvn spring-boot:run
    

    启动后,访问 http://localhost:8761,你会看到 user-service 已注册。


4. 网关集成 Eureka

假设你已有网关,现在让它通过 Eureka 动态路由。

(1) 修改网关依赖

确保网关包含 Eureka Client:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

(2) 配置网关

server:
  port: 8080
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true # 启用服务发现
      routes:
        - id: user-service
          uri: lb://user-service # 使用服务名,lb 表示负载均衡
          predicates:
            - Path=/users/**
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

(3) 运行网关

mvn spring-boot:run

访问 http://localhost:8080/users,网关会通过 Eureka 找到 user-service 并转发请求。


5. 验证

  • Eureka Server:http://localhost:8761,检查 user-servicegateway-service 是否注册。
  • 服务访问:http://localhost:8081/users,直接访问用户服务。
  • 网关访问:http://localhost:8080/users,通过网关访问用户服务。

6. 高可用 Eureka Server(可选)

单点 Eureka Server 有故障风险,可以配置集群:

  1. 修改 application.yml 为多实例:
    • 实例 1(8761 端口):
      spring:
        profiles: peer1
      server:
        port: 8761
      eureka:
        instance:
          hostname: peer1
        client:
          service-url:
            defaultZone: http://peer2:8762/eureka/
      
    • 实例 2(8762 端口):
      spring:
        profiles: peer2
      server:
        port: 8762
      eureka:
        instance:
          hostname: peer2
        client:
          service-url:
            defaultZone: http://peer1:8761/eureka/
      
  2. 启动:
    java -jar eureka-server.jar --spring.profiles.active=peer1
    java -jar eureka-server.jar --spring.profiles.active=peer2
    
  3. Client 配置:
    eureka:
      client:
        service-url:
          defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/
    

7. 注意事项

  • 心跳机制:Eureka Client 每 30 秒发送心跳,Server 90 秒未收到心跳会移除实例。
  • 自我保护:默认开启,避免因网络问题误删实例,可通过 eureka.server.enable-self-preservation=false 关闭。
  • 服务名大小写:Eureka 默认将服务名转为大写,确保路由时一致。

8. 总结

  • Eureka Server:搭建注册中心,管理服务实例。
  • Eureka Client:服务注册到 Server,网关通过 lb:// 调用。
  • 不使用 Eureka:可以用静态地址,但失去动态发现和负载均衡。
posted on   joken1310  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示