首页 / 资讯中心 / 文章详情

SpringBoot 快速上手 Nacos

SpringBoot 快速上手 Nacos ★ FEATURED ARTICLE
一、环境准备与版本匹配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 的依赖管理dependencyManagementdependencies!-- Spring Boot 依赖管理 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion3.2.x/versiontypepom/typescopeimport/scope/dependency!-- Spring Cloud 依赖管理 --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion2023.0.x/versiontypepom/typescopeimport/scope/dependency!-- Spring Cloud Alibaba 依赖管理 --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-dependencies/artifactIdversion2023.0.3.2/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagement2.2 服务提供者 pom.xml在服务提供者模块的 pom.xml 中加入 Nacos 服务发现依赖dependencies!-- Nacos 服务发现 --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!-- Web 模块 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Actuator 监控端点 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-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: always2.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 服务发现 --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!-- 负载均衡Spring Cloud LoadBalancer --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-loadbalancer/artifactId/dependency!-- Web 模块 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependencies3.2 application.yaml 配置yamlserver: 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;Configurationpublic class RestTemplateConfig {BeanLoadBalanced // 启用负载均衡使用服务名进行调用public RestTemplate restTemplate() {return new RestTemplate();}}3.4 启动类与 Controllerpackage com.example.consumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;SpringBootApplicationEnableDiscoveryClientpublic class NacosConsumerApplication {public static void main(String[] args) {SpringApplication.run(NacosConsumerApplication.class, args);}}消费者 Controllerpackage 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-17Nacos 3.x 要求 JDK 17。如果使用了 JDK 18 或 32 位 JDK会因类加载器变更或架构不匹配报错。服务名不一致消费者调用时使用的服务名必须与提供者的spring.application.name完全一致注意大小写敏感。多实例版本不一致如果提供者有多个实例其中一个实例没有对应接口请求转发到该实例时就会返回 404。类型转换异常消费者 Controller 的返回类型必须与提供者保持一致。例如提供者返回User对象消费者如果声明为String接收就会抛出类型转换异常。建议统一使用 JSON 进行数据交互。
阅读完成 · 觉得有帮助?
咨询建站