前一篇文章nacos-简介和初体验(一)我们已经在服务器部署了nacos应用了,接下来让我们尝试在springcloud中整合nacos,代替我们springcloud config。
一、新建springcloud工程
- 新建一个springboot工程,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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.eujian</groupId>
<artifactId>nacos-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 新建一个测试的controller
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/config")
@RefreshScope
public class DemoController {
@Value("${demoKey:demoKey}")
private String demoKey;
/**
* http://localhost:8081/config/getdemo
*/
@GetMapping("getdemo")
public String demo(){
return demoKey;
}
}
-
由于nacos在修改配置的时候会触发spring内部的event事件,所以我们可以监听这个event来做一些日志的打印。和特殊的事件变更。
新建自己的事件变更监听类
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import java.util.Set;
@Slf4j
@Configuration
public class ConfigChangeLister {
@Autowired
private Environment environment;
@EventListener
public void getUserEvent(EnvironmentChangeEvent event) {
Set<String> keys = event.getKeys();
keys.forEach(k->{
String v = environment.getProperty(k);
log.info("配置变更,key={},value={}",k,v);
});
}
}
- 新建一个
bootstrap.properties
文件
server.port=8081
spring.cloud.nacos.config.server-addr=localhost:8848
spring.application.name=config-demo
spring.cloud.nacos.config.file-extension=properties
二、测试动态修改配置
-
启动Boot工程,访问地址
http://localhost:8081/config/getdemo
-
在nacos发布配置
-
再次访问地址
http://localhost:8081/config/getdemo
应用的日志如下:
码云地址:https://gitee.com/guoeryyj/nacos-demo.git
系列文章:
nacos - 简介和初体验(一)
nacos - 作为配置中心与springcloud整合(二)
nacos - 作为注册中心与springcloud整合(三)
nacos - eureka如何平滑迁移到nacos(四)