Spring Cloud系列:使用 Nacos 实现配置管理和服务发现
版本说明
IDEA:2021.3
Maven:3.6.3
Jdk:17
Spring-Boot:2.6.13
Spring-Cloud:2021.0.5
Spring-Cloud-Alibaba:2021.0.5.0
创建项目
1、选择Spring Initalizr
2、选择需要安装的版本和依赖
3、修改pom文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mcode.api</groupId>
<artifactId>spring-cloud-nacos-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-nacos-demo</name>
<description>spring-cloud-nacos-demo</description>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
<spring.cloud.version>2021.0.5</spring.cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4、配置bootstrap.yaml
spring:
cloud:
nacos:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
discovery:
namespace: public
config:
file-extension: yaml
loadbalancer:
nacos:
enabled: true
application:
name: 'nacos-service'
profiles:
active: dev
5、添加启动类
package com.mcode.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* ClassName: NacosApplication
* Package: com.mcode.api
* Description:
*
* @Author: robin
* @Create: 2023/8/6 - 10:45 PM
* @Version: v1.0
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosApplication {
public static void main(String[] args) {
SpringApplication.run(NacosApplication.class,args);
}
}
6、添加测试控制器
package com.mcode.api.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* ClassName: NacosController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/6 - 10:42 PM
* @Version: v1.0
*/
@RestController
@RefreshScope
public class NacosController {
@Value("${jwtKey}")
private String jwtKey;
@RequestMapping("testConfig")
public String testConfig(){
return jwtKey;
}
}
7、nacos添加配置
8、测试配置
9、查看服务列表
微信:17873041739