微信登录

服务注册与发现 - Nacos - 阿里巴巴服务注册中心

Java Web Spring:服务注册与发现 - Nacos - 阿里巴巴服务注册中心

一、引言

在当今的微服务架构中,服务注册与发现是至关重要的一环。它允许各个微服务实例能够动态地注册自己的信息,并让其他服务可以方便地发现和调用它们。Nacos 是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台,它提供了一组简单易用的特性集,帮助我们快速实现服务的注册与发现。本文将详细介绍如何在 Java Web Spring 项目中使用 Nacos 进行服务注册与发现,并给出相应的演示代码。

二、Nacos 简介

Nacos 致力于帮助您发现、配置和管理微服务。它提供了一组简单易用的特性集,支持几乎所有主流类型的“服务”的发现、配置和管理:

  • 服务发现与服务健康监测:Nacos 可以让服务提供者方便地注册自己的服务信息,服务消费者可以通过 Nacos 发现可用的服务实例,并根据健康检查结果选择合适的实例进行调用。
  • 动态配置服务:Nacos 提供了动态配置管理功能,允许您在不重启服务的情况下修改服务的配置信息。
  • 动态 DNS 服务:Nacos 支持基于 DNS 的服务发现,允许您通过 DNS 解析的方式访问服务。

三、环境准备

  1. 安装 Nacos
    • 从 Nacos 的 GitHub 仓库(https://github.com/alibaba/nacos/releases)下载最新版本的 Nacos。
    • 解压下载的文件,进入 bin 目录。
    • 在 Linux 或 macOS 系统上,执行 sh startup.sh -m standalone 启动 Nacos 单机模式;在 Windows 系统上,执行 cmd startup.cmd -m standalone
    • 启动成功后,访问 http://localhost:8848/nacos,使用默认的用户名 nacos 和密码 nacos 登录 Nacos 控制台。
  2. 创建 Spring Boot 项目

四、服务提供者示例

1. 项目配置

application.propertiesapplication.yml 中添加 Nacos 配置:

  1. spring:
  2. application:
  3. name: service-provider
  4. cloud:
  5. nacos:
  6. discovery:
  7. server-addr: localhost:8848
  8. server:
  9. port: 8081

2. 编写服务接口

  1. import org.springframework.web.bind.annotation.GetMapping;
  2. import org.springframework.web.bind.annotation.RestController;
  3. @RestController
  4. public class HelloController {
  5. @GetMapping("/hello")
  6. public String hello() {
  7. return "Hello from Service Provider!";
  8. }
  9. }

3. 启动服务

创建一个 Spring Boot 主类并启动服务:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  4. @SpringBootApplication
  5. @EnableDiscoveryClient
  6. public class ServiceProviderApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(ServiceProviderApplication.class, args);
  9. }
  10. }

五、服务消费者示例

1. 项目配置

同样在 application.propertiesapplication.yml 中添加 Nacos 配置:

  1. spring:
  2. application:
  3. name: service-consumer
  4. cloud:
  5. nacos:
  6. discovery:
  7. server-addr: localhost:8848
  8. server:
  9. port: 8082

2. 编写服务调用代码

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.cloud.client.ServiceInstance;
  3. import org.springframework.cloud.client.discovery.DiscoveryClient;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import org.springframework.web.client.RestTemplate;
  7. import java.util.List;
  8. @RestController
  9. public class ConsumerController {
  10. @Autowired
  11. private DiscoveryClient discoveryClient;
  12. @GetMapping("/callHello")
  13. public String callHello() {
  14. // 获取服务提供者的实例列表
  15. List<ServiceInstance> instances = discoveryClient.getInstances("service-provider");
  16. if (instances!= null &&!instances.isEmpty()) {
  17. // 选择第一个实例
  18. ServiceInstance instance = instances.get(0);
  19. String url = instance.getUri() + "/hello";
  20. RestTemplate restTemplate = new RestTemplate();
  21. return restTemplate.getForObject(url, String.class);
  22. }
  23. return "No service provider available.";
  24. }
  25. }

3. 启动服务

创建一个 Spring Boot 主类并启动服务:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  4. @SpringBootApplication
  5. @EnableDiscoveryClient
  6. public class ServiceConsumerApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(ServiceConsumerApplication.class, args);
  9. }
  10. }

六、测试服务

  1. 启动服务提供者和服务消费者。
  2. 访问 Nacos 控制台(http://localhost:8848/nacos),在“服务管理” - “服务列表”中可以看到 service-providerservice-consumer 服务已经成功注册。
  3. 访问服务消费者的 /callHello 接口(http://localhost:8082/callHello),如果一切正常,您将看到服务提供者返回的 Hello from Service Provider! 消息。

七、总结

通过以上步骤,我们成功地在 Java Web Spring 项目中使用 Nacos 实现了服务的注册与发现。Nacos 提供了简单易用的接口和丰富的功能,帮助我们轻松构建微服务架构。以下是一个简单的总结表格:

角色 功能 关键代码
服务提供者 注册服务信息 @EnableDiscoveryClient 注解,spring.cloud.nacos.discovery.server-addr 配置
服务消费者 发现服务并调用 DiscoveryClient 获取服务实例列表,RestTemplate 调用服务接口

希望本文能够帮助您快速上手使用 Nacos 进行服务注册与发现。在实际应用中,您可以根据具体需求进一步扩展和优化代码。

服务注册与发现 - Nacos - 阿里巴巴服务注册中心