news 2026/4/3 6:26:54

Spring Boot整合Nacos:从入门到精通

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring Boot整合Nacos:从入门到精通

引言

在微服务架构中,服务注册与发现、配置管理是两个核心组件。Nacos作为阿里巴巴开源的一站式服务治理平台,提供了服务发现、配置管理和动态DNS服务等功能。本文将详细介绍如何在Spring Boot项目中整合Nacos,实现服务注册与发现以及配置中心的功能。

一、环境准备

1.1 开发环境要求

在开始整合之前,我们需要确保开发环境满足以下要求:

  • JDK版本:推荐使用JDK 17或更高版本,以获得更好的性能和兼容性。

  • Maven版本:建议使用Maven 3.8或更高版本,用于项目依赖管理。

  • Spring Boot版本:本文基于Spring Boot 3.3.0进行演示,确保与Nacos的兼容性。

  • Nacos版本:推荐使用Nacos 2.4.0或更高版本,以获得最新的功能和稳定性。

1.2 Nacos服务端的安装与启动

1.2.1 下载Nacos

你可以通过以下两种方式下载Nacos:

  1. 官网下载:直接从Nacos官网下载页面选择稳定版的发行包下载。

  2. Github下载:访问Nacos GitHub仓库的发布页面,选择合适的版本,下载nacos-server-$version.zip.tar.gz文件。

1.2.2 解压与启动

  1. Windows环境

    • 解压下载的nacos-server-$version.zip到指定目录。

    • 打开命令提示符,切换到nacos/bin目录下,执行startup.cmd -m standalone来启动Nacos。

  2. Linux/Unix/Mac环境

    • 使用unziptar命令解压下载的文件:

      unzip nacos-server-$version.zip # 或者 tar -xvf nacos-server-$version.tar.gz
    • 进入bin目录,执行启动脚本:
      sh startup.sh -m standalone
    • 若在Ubuntu系统遇到运行错误,尝试使用bash替换sh

      bash startup.sh -m standalone

1.2.3 验证启动状态

访问http://127.0.0.1:8848/nacos,如果能够看到Nacos控制台界面,说明服务已成功启动。默认的用户名和密码均为nacos

二、整合步骤

2.1 添加Maven依赖

在Spring Boot项目的pom.xml文件中添加Spring Cloud Alibaba Nacos Discovery和Config相关依赖:

<dependencies> <!-- Spring Cloud Alibaba Nacos Discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2023.0.1.2</version> </dependency> <!-- Spring Cloud Alibaba Nacos Config --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2023.0.1.2</version> </dependency> <!-- Spring Boot Web Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Spring Cloud Bootstrap --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>4.1.0</version> </dependency> </dependencies>

依赖说明

  • spring-cloud-starter-alibaba-nacos-discovery:用于服务注册与发现。

  • spring-cloud-starter-alibaba-nacos-config:用于配置中心。

  • spring-boot-starter-web:用于构建Web应用。

  • spring-boot-starter-actuator:用于监控和管理应用。

  • spring-cloud-starter-bootstrap:用于加载bootstrap.yml配置文件。

2.2 配置bootstrap.yml文件

src/main/resources目录下创建bootstrap.yml文件,并添加以下配置:

spring: application: name: nacos-demo cloud: nacos: discovery: server-addr: 127.0.0.1:8848 namespace: public group: DEMO_GROUP config: server-addr: 127.0.0.1:8848 namespace: public file-extension: yaml group: DEMO_GROUP refresh-enabled: true shared-configs: ->2.3 配置application.yml文件

src/main/resources目录下创建application.yml文件,并添加以下配置:

server: port: 8080 servlet: context-path: /nacos-demo spring: datasource: url: jdbc:mysql://localhost:3306/nacos_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver jpa: hibernate: ddl-auto: update show-sql: true properties: hibernate: format_sql: true management: endpoints: web: exposure: include: refresh,health,info endpoint: refresh: enabled: true

配置说明

  • server.port:应用的端口号。

  • server.servlet.context-path:应用的上下文路径。

  • spring.datasource:数据库连接配置。

  • spring.jpa:JPA配置,用于数据库操作。

  • management.endpoints.web.exposure.include:暴露的Actuator端点。

  • management.endpoint.refresh.enabled:是否启用配置刷新端点。

2.4 创建启动类并添加@EnableDiscoveryClient注解

在项目的主启动类上添加@EnableDiscoveryClient注解,以启用服务注册与发现功能:

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class NacosDemoApplication { public static void main(String[] args) { SpringApplication.run(NacosDemoApplication.class, args); } }

2.5 编写服务注册与发现示例代码

2.5.1 服务提供者

创建一个简单的控制器,作为服务提供者:

import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ProviderController { @GetMapping("/hello") public String hello() { return "Hello from Nacos Provider!"; } }

2.5.2 服务消费者

创建一个服务消费者,通过RestTemplate调用服务提供者:

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController public class ConsumerController { @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; @GetMapping("/call-provider") public String callProvider() { // 从Nacos获取服务提供者的实例列表 List<String> services = discoveryClient.getServices(); if (services.contains("nacos-demo")) { // 调用服务提供者 return restTemplate.getForObject("http://nacos-demo/hello", String.class); } return "No provider found!"; } }

2.6 编写配置中心使用示例代码

2.6.1 创建配置类

创建一个配置类,用于绑定Nacos中的配置:

import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "demo") @RefreshScope public class DemoConfig { private String message; private int timeout; private boolean enabled; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public int getTimeout() { return timeout; } public void setTimeout(int timeout) { this.timeout = timeout; } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } }

注解说明

  • @ConfigurationProperties:用于绑定配置文件中的属性。

  • @RefreshScope:用于实现配置的动态刷新。

2.6.2 创建控制器

创建一个控制器,用于演示配置中心的使用:

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigController { @Value("${demo.message:Default Message}") private String message; @Value("${demo.timeout:5000}") private int timeout; @Value("${demo.enabled:false}") private boolean enabled; @Autowired private DemoConfig demoConfig; @GetMapping("/config-message") public String getConfigMessage() { return "Message from @Value: " + message + "<br>" + "Timeout from @Value: " + timeout + "<br>" + "Enabled from @Value: " + enabled + "<br>" + "Message from @ConfigurationProperties: " + demoConfig.getMessage() + "<br>" + "Timeout from @ConfigurationProperties: " + demoConfig.getTimeout() + "<br>" + "Enabled from @ConfigurationProperties: " + demoConfig.isEnabled(); } }

三、代码实现

3.1 项目结构

以下是完整的项目结构:

nacos-demo ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ └── example │ │ │ ├── NacosDemoApplication.java │ │ │ ├── controller │ │ │ │ ├── ConsumerController.java │ │ │ │ ├── ProviderController.java │ │ │ │ └── ConfigController.java │ │ │ └── config │ │ │ └── DemoConfig.java │ │ └── resources │ │ ├── application.yml │ │ └── bootstrap.yml │ └── test │ └── java │ └── com │ └── example │ └── NacosDemoApplicationTests.java └── pom.xml

3.2 核心类的完整代码

3.2.1 启动类(NacosDemoApplication.java)

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient public class NacosDemoApplication { public static void main(String[] args) { SpringApplication.run(NacosDemoApplication.class, args); } @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }

3.2.2 服务提供者控制器(ProviderController.java)

import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ProviderController { @GetMapping("/hello") public String hello() { return "Hello from Nacos Provider!"; } }

3.2.3 服务消费者控制器(ConsumerController.java)

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController public class ConsumerController { @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; @GetMapping("/call-provider") public String callProvider() { // 从Nacos获取服务提供者的实例列表 List<String> services = discoveryClient.getServices(); if (services.contains("nacos-demo")) { // 调用服务提供者 return restTemplate.getForObject("http://nacos-demo/hello", String.class); } return "No provider found!"; } }

3.2.4 配置类(DemoConfig.java)

import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "demo") @RefreshScope public class DemoConfig { private String message; private int timeout; private boolean enabled; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public int getTimeout() { return timeout; } public void setTimeout(int timeout) { this.timeout = timeout; } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } }

3.2.5 配置中心控制器(ConfigController.java)

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigController { @Value("${demo.message:Default Message}") private String message; @Value("${demo.timeout:5000}") private int timeout; @Value("${demo.enabled:false}") private boolean enabled; @Autowired private DemoConfig demoConfig; @GetMapping("/config-message") public String getConfigMessage() { return "Message from @Value: " + message + "<br>" + "Timeout from @Value: " + timeout + "<br>" + "Enabled from @Value: " + enabled + "<br>" + "Message from @ConfigurationProperties: " + demoConfig.getMessage() + "<br>" + "Timeout from @ConfigurationProperties: " + demoConfig.getTimeout() + "<br>" + "Enabled from @ConfigurationProperties: " + demoConfig.isEnabled(); } }

四、测试验证

4.1 服务注册验证

  1. 启动Spring Boot应用,观察控制台输出,确认服务已成功注册到Nacos。

  2. 访问Nacos控制台(http://127.0.0.1:8848/nacos),登录后进入“服务管理” -> “服务列表”,可以看到注册的服务。

4.2 配置中心功能验证

  1. 在Nacos控制台中,进入“配置管理” -> “配置列表”,点击“+”按钮创建一个新的配置。

  2. 配置信息如下:

    • Data IDnacos-demo-dev.yaml

    • GroupDEMO_GROUP

    • 配置格式YAML

    • 配置内容

      demo: message: Hello from Nacos Config! timeout: 10000 enabled: true
  3. 发布配置后,访问http://localhost:8080/nacos-demo/config-message,可以看到配置信息已生效。

  4. 在Nacos控制台中修改配置内容,再次访问http://localhost:8080/nacos-demo/config-message,可以看到配置已动态刷新。

4.3 配置动态刷新验证

  1. 在Nacos控制台中修改配置内容,例如将demo.message的值改为Hello from Nacos Config (Updated)!

  2. 发送POST请求到http://localhost:8080/nacos-demo/actuator/refresh,触发配置动态刷新。

  3. 再次访问http://localhost:8080/nacos-demo/config-message,可以看到配置已动态刷新。

4.4 可能遇到的问题及解决方案

4.4.1 服务注册失败

  • 原因:可能是Nacos服务端地址配置错误或网络不通。

  • 解决方案:检查bootstrap.yml中的spring.cloud.nacos.discovery.server-addr配置是否正确,确保Nacos服务端已启动且网络通畅。

4.4.2 配置中心无法加载配置

  • 原因:可能是配置文件的Data ID、Group或Namespace配置错误。

  • 解决方案:检查bootstrap.yml中的spring.cloud.nacos.config相关配置是否正确,确保与Nacos控制台中的配置一致。

4.4.3 配置动态刷新不生效

  • 原因:可能是未添加@RefreshScope注解或配置类未正确绑定。

  • 解决方案:在需要动态刷新的配置类上添加@RefreshScope注解,确保配置类与Nacos中的配置正确绑定。

五、总结与扩展

5.1 整合过程中的注意事项

  1. 版本兼容性:确保Spring Boot、Spring Cloud Alibaba和Nacos的版本兼容性,避免因版本不匹配导致的问题。

  2. 命名空间与分组:合理使用命名空间和分组,实现多环境和多模块的配置隔离。

  3. 配置动态刷新:使用@RefreshScope注解实现配置的动态刷新,但需注意性能影响。

  4. 共享配置:合理使用共享配置,减少重复配置,提高配置的复用性。

5.2 Nacos的其他高级特性

  1. 服务路由与负载均衡:Nacos支持服务路由和负载均衡,可以通过配置实现流量的智能分发。

  2. 配置加密与解密:Nacos支持配置的加密与解密,保护敏感信息的安全。

  3. 服务健康检查:Nacos支持服务健康检查,确保服务的可用性。

  4. 配置灰度发布:Nacos支持配置的灰度发布,逐步推送配置到部分实例,验证无误后再全量推送。

5.3 推荐的学习资源

  • 官方文档:Nacos官方文档提供了详细的使用指南和API文档。

  • GitHub仓库:Nacos GitHub仓库包含了最新的源码和示例代码。

  • Spring Cloud Alibaba文档:Spring Cloud Alibaba官方文档提供了Spring Cloud Alibaba与Nacos的整合指南。

六、结语

通过本文的介绍,我们详细了解了如何在Spring Boot项目中整合Nacos,实现服务注册与发现以及配置中心的功能。Nacos作为一站式服务治理平台,为微服务架构提供了强大的支持。希望本文能够帮助你快速上手Spring Boot与Nacos的整合,提升微服务开发的效率和质量。

参考资料

  • Nacos官方文档
  • Spring Cloud Alibaba官方文档
  • Spring Boot官方文档
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/30 23:44:30

AI 英语学习系统的费用

开发一个 AI 英语学习系统的费用主要由一次性研发费用和持续性运营成本&#xff08;算力与 API&#xff09;两部分组成。与传统软件不同&#xff0c;AI 软件的后续“消耗费”在预算中占比极高。以下是按项目规模划分的费用预估&#xff08;以人民币计算&#xff09;&#xff1a…

作者头像 李华
网站建设 2026/3/28 13:18:38

终于有人把网络安全运维工程师需要学什么讲清了!从零基础入门到精通,收藏这一篇就够了!

【必学收藏】安全运维工程师全面解析&#xff1a;职责、技能、薪资与学习路径 安全运维工程师是网络安全领域的重要岗位&#xff0c;负责服务器、网络设备等安全维护与管理工作。需掌握网络安全产品、网络协议、漏洞防护、操作系统及数据库等技术。由于网络安全人才供不应求&a…

作者头像 李华
网站建设 2026/3/10 18:10:12

Rembg抠图故障恢复:高可用部署方案

Rembg抠图故障恢复&#xff1a;高可用部署方案 1. 背景与痛点分析 在AI图像处理领域&#xff0c;自动去背景技术已成为内容创作、电商上架、设计修图等场景的刚需。Rembg凭借其基于U-Net模型的强大分割能力&#xff0c;实现了无需标注、高精度的通用图像抠图&#xff0c;广受…

作者头像 李华
网站建设 2026/4/3 6:20:30

怎么找出一篇论文的研究问题:实用方法与技巧解析

盯着满屏的PDF&#xff0c;眼前的外语字母开始跳舞&#xff0c;脑子里只剩下“我是谁、我在哪、这到底在说什么”的哲学三问&#xff0c;隔壁实验室的师兄已经用AI工具做完了一周的文献调研。 你也许已经发现&#xff0c;打开Google Scholar直接开搜的“原始人”模式&#xff…

作者头像 李华
网站建设 2026/3/19 4:23:32

大模型学习大模型原理:一份值得收藏的AI使用心得与思维框架

这篇文章通过"用大模型学习大模型原理"的实战案例&#xff0c;分享了与AI有效沟通的方法。作者提出应运用乔哈里窗理论不断扩大共识区&#xff0c;坚持"知之为知之"原则验证AI输出&#xff0c;并构建了"教育学思维模型AI技术AI工具"的AI能力提升…

作者头像 李华
网站建设 2026/3/18 13:04:26

2025年度章鱼大会圆满落幕 以徽章为媒弘扬奥林匹克文化

1月11日&#xff0c;以“共治共享 逐光同行”为主题的2025年度章鱼大会在北京亮马河大厦举办。本次大会采用线上同步直播形式&#xff0c;线上线下实时互动&#xff0c;气氛热烈。来自各地的徽章收藏爱好者、奥林匹克收藏界专家顾问齐聚现场&#xff0c;共话徽章文化传承与奥林…

作者头像 李华