代码改变世界

详细介绍:[特殊字符]️ Spring Cloud Eureka 三步通:搭建注册中心 + 服务注册 + 服务发现,通俗易懂!

2025-10-26 10:07  tlnshuju  阅读(4)  评论(0)    收藏  举报

引言:什么是服务注册与发现?

在微服务架构中,我们通常会将一个大型系统拆分成多个小服务,比如:

  • 用户服务(user-service)

  • 订单服务(order-service)

  • 支付服务(payment-service)

这些服务可能会​​相互调用​​,比如订单服务要查询用户信息,就需要调用用户服务。

但问题来了:​​订单服务怎么知道用户服务在哪里(IP + 端口)?​

​这就是服务注册与发现要解决的问题!​


什么是 Eureka?

​Eureka 是 Netflix 开源的服务注册与发现组件,Spring Cloud 对其进行了封装,让我们可以非常方便地搭建和使用服务注册中心。​

简单来说:

  • ​Eureka Server​​:就是“服务登记处”,所有服务都来这里“注册”自己的信息(如 IP、端口、服务名)。

  • ​Eureka Client​​:就是“服务提供者 & 消费者”,它们会向 Eureka Server 注册自己,或者从 Eureka Server 查询其他服务。


✅ 一、Eureka 三步通概述

我们要实现的目标很简单,分为三个步骤:

  1. ✅ ​​第一步:搭建 Eureka 注册中心(Eureka Server)​

  2. ✅ ​​第二步:服务注册(让服务自己“登记”到 Eureka)​

  3. ✅ ​​第三步:服务发现(让一个服务找到另一个服务)​


第一步:搭建 Eureka 注册中心(Eureka Server)

这是我们的“服务登记处”,其他服务都会来这里注册。

1.1 创建一个 Spring Boot 项目

使用 Spring Initializr快速生成,或者用 IDE 创建,选择以下依赖:

  • Spring Web

  • ​Eureka Server​​(在 Spring Cloud Dependencies 下)

或者,如果你本地已有 Spring Boot 项目,直接添加依赖:

Maven 依赖(pom.xml):


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server
并确保你引入了 Spring Cloud 的 BOM(版本管理),比如:

    
        
            org.springframework.cloud
            spring-cloud-dependencies
            2022.0.4 
            pom
            import
        
    

1.2 开启 Eureka Server

在你的主启动类上加上注解:

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);
    }
}

1.3 配置 Eureka Server(application.yml 或 application.properties)

application.yml 示例:

server:
  port: 8761  # Eureka Server 默认端口
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false  # 是否将自己注册到 Eureka(自己是服务器,不需要)
    fetch-registry: false        # 是否从 Eureka 获取注册信息(自己就是服务器)
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  # Eureka Server 地址

1.4 启动并访问 Eureka 控制台

启动项目后,访问:

 http://localhost:8761

你会看到 Eureka 的管理界面,默认情况下还没有任何服务注册进来,别急,接下来我们来做“服务注册”。


️ 第二步:服务注册(让服务自己登记到 Eureka)

现在我们来创建一个“用户服务”(user-service),让它向 Eureka Server 注册自己的信息。

2.1 创建用户服务(Spring Boot 项目)

同样使用 Spring Initializr 创建,或者用已有项目,引入以下依赖:

Maven 依赖:


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client


    org.springframework.boot
    spring-boot-starter-web

2.2 开启 Eureka 客户端

在主启动类上添加注解:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient  //  表示这是一个 Eureka 客户端,会向 Eureka 注册
public class UserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }
}

2.3 配置服务注册信息(application.yml)

server:
  port: 8081  # 用户服务端口
spring:
  application:
    name: user-service  # 服务名称,很重要!Eureka 通过这个名称识别服务
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  # Eureka Server 地址
  instance:
    prefer-ip-address: true  # 可选,使用 IP 注册而不是主机名

2.4 添加一个测试接口(可选)

@RestController
public class UserController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello from User Service!";
    }
}

2.5 启动服务,查看 Eureka 控制台

启动 UserServiceApplication,然后​​再次访问 Eureka 控制台:http://localhost:8761

你会看到一个名为 ​​USER-SERVICE​​ 的服务已经注册上去了!

它显示了 IP、端口、运行状态等信息。


第三步:服务发现(一个服务调用另一个服务)

现在,我们来创建一个“订单服务”(order-service),它会通过 Eureka 找到 ​​user-service​​,然后调用它的接口!

3.1 创建订单服务(Spring Boot 项目)

同样引入依赖:


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-openfeign  

或者使用 ​​RestTemplate​​(传统方式)也可以。


3.2 开启 Eureka 客户端

主启动类加上:

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

3.3 配置 Eureka 客户端(application.yml)

server:
  port: 8082

spring:
  application:
    name: order-service

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

3.4 通过服务名调用 user-service(重点!)

方法一:使用 RestTemplate(推荐先学这个)

① 配置 RestTemplate 并加上 @LoadBalanced(关键!)
@Bean
@LoadBalanced  //  这个注解让 RestTemplate 具备服务发现能力
public RestTemplate restTemplate() {
    return new RestTemplate();
}
② 在 Controller 或 Service 中通过 ​​服务名​​ 调用
@Autowired
private RestTemplate restTemplate;
@GetMapping("/call-user")
public String callUserService() {
    // 注意:这里不是写死 IP,而是服务名!
    String result = restTemplate.getForObject(
        "http://user-service/hello",  //  通过服务名调用
        String.class
    );
    return "OrderService 调用了 UserService,返回:" + result;
}

✅ 你没有写死 user-service 的 IP 和端口,而是通过 ​​服务名 user-service​​ 去 Eureka 找到它的实际地址,这就是服务发现!

方法二(可选):使用 Feign(更优雅的声明式调用)

  1. 开启 Feign:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients  //  开启 Feign 客户端
public class OrderServiceApplication { ... }

定义一个 Feign 接口:

@FeignClient("user-service")  //  指定要调用的服务名
public interface UserServiceClient {
    @GetMapping("/hello")
    String hello();
}

注入并调用

@Autowired
private UserServiceClient userServiceClient;
@GetMapping("/call-user-feign")
public String callUserWithFeign() {
    String result = userServiceClient.hello();
    return "通过 Feign 调用 UserService,返回:" + result;
}

3.5 启动并测试

  • 启动:EurekaServer、UserService、OrderService

  • 访问 OrderService 的接口:http://localhost:8082/call-user

你会看到它成功调用了 UserService 的 /hello接口,并返回了内容!

​这就是服务发现的魅力:你不需要关心对方服务跑在哪台机器、哪个端口,只需要通过服务名调用即可!​


✅ 总结:Eureka 三步通,轻松搞定微服务注册与发现!

步骤

操作

说明

​第1步:搭建 Eureka Server​

创建一个 Spring Boot 项目,引入 Eureka Server 依赖,加 @EnableEurekaServer,启动后访问 http://localhost:8761

这是“服务登记中心”

​第2步:服务注册​

创建业务服务(如 user-service),引入 Eureka Client 依赖,加 @EnableEurekaClient,配置 Eureka Server 地址,启动后会自动注册到 Eureka

服务会告诉 Eureka:“我在这儿!”

​第3步:服务发现​

创建另一个服务(如 order-service),通过服务名(如 http://user-service/hello)调用其它服务,结合 RestTemplate 或 Feign,无需关心 IP

服务通过 Eureka 找到目标服务并调用


附加说明

问题

答案

Eureka 用在什么场景?

微服务架构中,服务之间需要动态发现与调用

Eureka Server 可以有多个吗?

✅ 可以集群部署,相互同步注册信息

Eureka 与 Nacos、Consul 有什么区别?

它们都是服务注册与发现组件,Eureka 是 Netflix 出品,Nacos 功能更丰富(注册+配置),Consul 是 HashiCorp 出品

Spring Boot 3 / Spring Cloud 202x 还能用 Eureka 吗?

✅ 可以,但官方推荐使用其它组件如 Nacos 或 Spring Cloud LoadBalancer


写在最后

通过 Eureka 的这三步:

  1. 搭建注册中心 →

  2. 服务注册上去 →

  3. 其它服务发现并调用它

你就掌握了 ​​微服务最核心的机制之一:服务注册与发现!​

​下一步,你可以尝试:​

  • 多个服务互相调用

  • 加入网关(如 Gateway)

  • 使用配置中心(如 Nacos Config)

  • 搭建 Eureka 集群