Spring Cloud + Nacos 微服务从 0 到 1 搭建实战
本文详细讲解了基于 Spring Cloud Alibaba 和 Nacos 构建微服务项目的实战流程。内容包括环境准备(JDK、Maven、Nacos Server)、工程结构搭建(父工程、服务提供者、服务消费者)、核心代码编写及配置文件设置。通过测试验证了服务注册发现与远程调用功能,实现了微服务间的解耦通信,为后续扩展配置中心、熔断降级等企业级特性奠定基础。

本文详细讲解了基于 Spring Cloud Alibaba 和 Nacos 构建微服务项目的实战流程。内容包括环境准备(JDK、Maven、Nacos Server)、工程结构搭建(父工程、服务提供者、服务消费者)、核心代码编写及配置文件设置。通过测试验证了服务注册发现与远程调用功能,实现了微服务间的解耦通信,为后续扩展配置中心、熔断降级等企业级特性奠定基础。

确保你的电脑已安装以下环境,这是运行项目的基础:
JAVA_HOME 环境变量MAVEN_HOMEbin/startup.cmd -m standalone(单机模式)<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 父工程坐标 -->
<groupId>com.example</groupId>
<artifactId>springcloud-nacos-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 子模块声明 -->
<modules>
<module>nacos-provider</module>
<module>nacos-consumer</module>
</modules>
<!-- 依赖版本管理 -->
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-cloud-alibaba.version>2.2.9.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring-boot.version>2.3.12.RELEASE</spring-boot.version>
</properties>
<!-- 依赖版本锁定 -->
<dependencyManagement>
<dependencies>
<!-- Spring Boot 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud Alibaba 依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.example</groupId>
<artifactId>springcloud-nacos-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>nacos-provider</artifactId>
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Nacos 服务注册发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery
server:
port: 8081 # 提供者端口
spring:
application:
name: nacos-provider # 服务名称(核心,Nacos 通过这个名称识别服务)
cloud:
nacos:
discovery:
server-addr: localhost:8848 # Nacos 服务地址
package com.example.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
// 开启服务注册发现(Nacos)
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
// 业务接口:模拟提供数据服务
@RestController
class ProviderController {
// 接口路径:/hello/{name}
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name) {
return "Hello " + name + "! 我是 Nacos 服务提供者";
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.example</groupId>
<artifactId>springcloud-nacos-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>nacos-consumer</artifactId>
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Nacos 服务注册发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-webflux
server:
port: 8082 # 消费者端口
spring:
application:
name: nacos-consumer # 消费者服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 # Nacos 服务地址
package com.example.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
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;
// 开启服务注册发现
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
// 配置 RestTemplate,开启负载均衡(@LoadBalanced)
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
// 消费者接口:调用提供者服务
@RestController
class ConsumerController {
// 注入 RestTemplate
private final RestTemplate restTemplate;
// 构造器注入
public ConsumerController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
String {
+ name;
restTemplate.getForObject(url, String.class);
}
}
}
nacos-provider(8081 端口)nacos-consumer(8082 端口)nacos-provider 和 nacos-consumer 两个服务Hello Java 微服务!我是 Nacos 服务提供者,说明调用成功。spring.application.name(服务名)和 spring.cloud.nacos.discovery.server-addr(Nacos 地址),且启动类需加 @EnableDiscoveryClient。这个实战项目覆盖了企业微服务开发的核心刚需:服务注册发现、跨服务调用,是 Spring Cloud + Nacos 的最小可用模型,后续可扩展配置中心、熔断降级、网关等企业级特性。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online