SpringCloud使用Nacos实现配置管理
Nacos的安装可以看:
1、在nacos中添加配置
2、微服务中读取配置
因为项目启动时会先读取nacos的配置文件在读取application.yml配置文件,所以要读取nacao的配置还需要一个bootstrap.yml文件
读取配置的顺序是bootstrap.yml->nacaos配置文件->application.yml
需要在bootstrap.yml文件中配置nacos地址来读取nacos配置
user-service引入Nacos的管理客户端依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
新建bootstrap.yml文件
spring: application: name: userservice # nacos的服务名称 profiles: active: dev # 开发环境,这里是dev cloud: nacos: server-addr: localhost:8848 # nacos地址 config: file-extension: yaml # 文件后缀名
代码中使用配置
package com.demo.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Value("${user.name}") private String userName; @GetMapping("/user/{id}") public String user(@PathVariable String id) { return "用户:" + id + "===========" + "配置的用户名:" + userName; } }
重启服务并访问
配置生效了
3、配置的热更新
两种方式:
方式一:在@Value注入的变量所在类上添加注解@RefreshScope
package com.demo.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.PathVariable; import org.springframework.web.bind.annotation.RestController; @RefreshScope @RestController public class UserController { @Value("${user.name}") private String userName; @GetMapping("/user/{id}") public String user(@PathVariable String id) { return "用户:" + id + "===========" + "配置的用户名:" + userName; } }
方式二:使用@ConfigurationProperties注解
新建一个配置类
package com.demo.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "user") public class UserProperties { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
修改controller类
package com.demo.controller; import com.demo.config.UserProperties; import org.springframework.beans.factory.annotation.Autowired; 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.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserProperties userProperties; @GetMapping("/user/{id}") public String user(@PathVariable String id) { return "用户:" + id + "===========" + "配置的用户名:" + userProperties.getName(); } }
重启user-service并测试
完成,两种方式都可以成功
多环境配置共享
微服务启动时会从nacos读取多个配置文件:
[spring.application.name]-[ spring.profiles.active ]. yaml ,例如: userservice-dev.yaml [spring.application.name]. yaml ,例如: userservice.yaml 无论profile如何变化,[spring.application.name].yaml这个文件一定会加载,因此多环境共享配置可以写入这个文件
注意事项:
-
不是所有的配置都适合放到配置中心,维护起来比较麻烦 建议将一些关键参数,需要运行时调整的参数放到nacos配置中心,一般都是自定义配置