在https://blog.csdn.net/GoSaint/article/details/105590470这篇文章中集成了SpringBoot,并且作为注册中心,接下来继续集成配置中心。
1 添加依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
2 在控制台直接配置属性


配置yml文件
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config: #配置中心地址
server-addr: 127.0.0.1:8848
application:
name: nacos-example
server:
port: 9001
在Controller中直接可以使用。
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value(value = "${username:gosaint}")
private String username;
/**
* http://localhost:8080/config/get
*/
@RequestMapping("/get")
public String get() {
return username;
}
}
调用localhost:9001/config/get。返回的是gosaint,我们还可以在配置中心管理界面修改值为gosaint3。再次调用查看情况。发现返回值变化了。

678

被折叠的 条评论
为什么被折叠?



