1.Nacos安装启动
2.nacos添加配置
Data ID:需要应用在配置
3.添加依赖【pom.xml】
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lhy</groupId>
<artifactId>cloud-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-api</name>
<description>cloud-api</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2.2.2.RELEASE</spring-cloud.version>
<spring-nacos.version>2.2.1.RELEASE</spring-nacos.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- nacos 注册中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${spring-nacos.version}</version>
</dependency>
<!-- nacos 配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${spring-nacos.version}</version>
</dependency>
<!-- springcloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>${spring-cloud.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.配置【bootstrap.yml】
server:
port: 12315
spring:
application:
name: cloud-api
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: doctor-dev
file-extension: yml
核心配置
config:server-addr: 127.0.0.1:8848 配置中心地址
file-extension: yml 配置文件格式
prefix 配置文件前缀
Data ID = ${prefix}.${file-extension} 默认为${spring:application:name}+${spring:profiles:active: dev}.${file-extension}
Nacos配置中心的配置必须在bootstrap.yml或者bootstrap-xx.yml中配置,一个算是主要原因的原因:bootstrap.yml的优先级高于application.yml。
5.启动类
@SpringBootApplication
@EnableDiscoveryClient
public class CloudApiApplication {
public static void main(String[] args) {
SpringApplication.run(CloudApiApplication.class, args);
}
}
6.测试
/**
* Class description.
*
* @author ocean liu
* @createTime 2021/11/29 - 10:57
*/
@RequestMapping("/user")
@RestController
@RefreshScope // 支持动态更新
public class UserController {
@Value("${user.headPortrait:123.png}")
private String headPortrait;
@GetMapping("/headPortrait")
public String headPortrait(){
return headPortrait;
}
}
@RefreshScope 支持动态更新(类上注解)
@Value("${user.headPortrait:123.png}") 默认123.png