☰
SpringBoot 快速上手 Nacos
2026/10/3 20:01:06 网站建设 项目流程

一、环境准备与版本匹配

Spring Boot、Spring Cloud、Spring Cloud Alibaba 和 Nacos Server 四者之间存在严格的版本对应关系,任意一处版本不匹配都可能导致启动失败或运行时异常。

1.1 下载并启动 Nacos Server

步骤一:访问 Nacos 的 GitHub Releases 页面(Releases · alibaba/nacos · GitHub),下载对应版本的压缩包。

步骤二:解压后进入nacos/bin目录,启动 Nacos。Windows 系统下直接双击startup.cmd即可启动(默认以单机模式运行)。

步骤三:启动成功后,浏览器访问http://localhost:8848/nacos,使用默认账号密码nacos/nacos登录控制台。

二、服务提供者模块搭建

2.1 父工程依赖管理

在微服务项目中,推荐使用 Maven 多模块结构。在父工程的 pom.xml中引入 Spring Cloud Alibaba 的依赖管理:

<dependencyManagement>
<dependencies>
<!-- Spring Boot 依赖管理 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.x</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud 依赖管理 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.x</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud Alibaba 依赖管理 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2023.0.3.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

2.2 服务提供者 pom.xml

在服务提供者模块的 pom.xml 中加入 Nacos 服务发现依赖:

<dependencies>
<!-- Nacos 服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Web 模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Actuator 监控端点 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

引入 Nacos discovery 依赖时不需要指定版本号,版本由父工程中的dependencyManagement统一管理。

2.3 application.yaml 配置

server:
port: 8081 # 服务端口

spring:
application:
name: nacos-provider # 服务名称,消费者将通过此名称调用
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 # Nacos 服务端地址
# cluster-name: HZ # 集群名称(可选,按地区分组)
# namespace: dev # 命名空间(可选,用于环境隔离)
# group: DEFAULT_GROUP # 服务分组(可选,默认 DEFAULT_GROUP)

# 暴露所有监控端点
management:
endpoints:
web:
exposure:
include: '*' # 暴露所有端点,生产环境建议按需配置
endpoint:
health:
show-details: always

2.4 启动类与 Controller

启动类需要添加@EnableDiscoveryClient注解来启用服务注册发现功能:

package com.example.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient // 开启服务注册发现 public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); } }

编写服务接口:

package com.example.provider.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class ProviderController { @GetMapping("/hello/{name}") public String hello(@PathVariable String name) { return "Hello " + name + "! 我是服务提供者(nacos-provider)"; } }

2.5 集群配置

如果需要搭建服务提供者的集群,只需要使用相同的服务名、不同的端口号即可。

三、服务消费者模块搭建

3.1 pom.xml 依赖

消费者模块的 pom.xml 需要引入 Nacos 服务发现和负载均衡依赖

<dependencies>
<!-- Nacos 服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 负载均衡(Spring Cloud LoadBalancer) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- Web 模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

3.2 application.yaml 配置yaml

server: port: 8082 # 消费者端口 spring: application: name: nacos-consumer # 消费者服务名 cloud: nacos: discovery: server-addr: 127.0.0.1:8848 # Nacos 服务端地址

3.3 配置类 —— RestTemplate 负载均衡

package com.example.consumer.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

@Bean
@LoadBalanced // 启用负载均衡,使用服务名进行调用
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

3.4 启动类与 Controller

package com.example.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

消费者 Controller:

package com.example.consumer.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class ConsumerController { @Autowired private RestTemplate restTemplate; @GetMapping("/call/{name}") public String callProvider(@PathVariable String name) { // 使用服务名调用,而非具体 IP:端口 String url = "http://nacos-provider/hello/" + name; return restTemplate.getForObject(url, String.class); } }

关键注意点:消费者 Controller 的返回值类型必须与服务提供者的返回值类型保持一致。如果提供者返回String,消费者也用String接收;如果提供者返回某个自定义对象(如User),消费者也必须用相同的对象类型接收。否则会出现类型转换异常,或者在解析响应时出现404返回结果。

四、常见问题与避坑指南

  • JDK 版本不兼容:Nacos 2.x 要求 JDK 8-17,Nacos 3.x 要求 JDK 17+。如果使用了 JDK 18+ 或 32 位 JDK,会因类加载器变更或架构不匹配报错。

  • 服务名不一致:消费者调用时使用的服务名必须与提供者的spring.application.name完全一致,注意大小写敏感。

  • 多实例版本不一致:如果提供者有多个实例,其中一个实例没有对应接口,请求转发到该实例时就会返回 404。

  • 类型转换异常:消费者 Controller 的返回类型必须与提供者保持一致。例如提供者返回User对象,消费者如果声明为String接收,就会抛出类型转换异常。建议统一使用 JSON 进行数据交互。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询