统一配置管理
将配置交给 Nacos 管理的步骤:
-
在 Nacos 中添加配置文件。
-
在微服务中引入 nacos 的 config 依赖。
-
在微服务中添加 bootstrap.yml,配置 nacos 地址、当前环境、服务名称、文件后缀名。这些决定了程序启动时去 nacos 读取哪个文件。
具体操作:
1)在 Nacos 中添加配置信息
2)在弹出表单中填写配置信息
3)配置获取的步骤如下
4)引入 Nacos 的配置管理客户端依赖
<!--nacos连接注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!--nacos 配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
5)在 resource 目录添加一个 bootstrap.yml 文件,这个文件是引导文件,优先级高于 application.yml
spring:
application:
name: nacos-provider-payment
cloud:
nacos:
discovery:
namespace: 18b79e76-8c78-4494-b4fe-be0e587ff6d7
# 服务注册地址 ip加端口号
server-addr: 127.0.0.1:8848
config:
prefix: providerId # dataId ${prefix}-${spring.profiles.active}.${file-extension}
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
namespace: 18b79e76-8c78-4494-b4fe-be0e587ff6d7
group: DEFAULT_GROUP
file-extension: yaml
6)测试:将(Nacos 配置内容中的)dataId.result: 111 这个属性注入到 UserController 中
package com.pooj.cloudnacosconfig3355.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;
@RestController
@RequestMapping("/user") ///user/getData
@RefreshScope
public class CloudNacos {
@Value("${dataId.result}")
private String dataid;
@RequestMapping(value = "/getData")
public String getDate(){
return dataid;
}
}
配置热更新
Nacos 配置更改后,微服务可以实现热更新,两种方式如下:
-
通过 @Value 注解注入,结合 @RefreshScope 来刷新。
-
通过 @ConfigurationProperties 注入,自动刷新。
注意事项:
启动类: @EnableDiscoveryClient
-
不是所有的配置都适合放到配置中心,否则维护起来比较麻烦。
-
建议将一些关键参数,需要运行时调整的参数放到 nacos 配置中心,一般都是自定义配置。
方式一:在 @Value 注入的变量所在类上添加注解 @RefreshScope
方式二:使用 @ConfigurationProperties 注解
@Component
@Data
@ConfigurationProperties(prefix="pattern")
public class PatternProperties {
private String dateformat;
}
配置共享
多环境配置共享
微服务会从 nacos 读取的配置文件:
-
[服务名]-[spring.profile.active].yaml
:环境配置(例如 userservice-dev.yaml)。 -
[服务名].yaml
:默认配置,多环境共享(例如 userservice.yaml)。 -
无论 profile 如何变化,
[服务名].yaml
这个文件一定会加载,因此多环境共享配置可以写入这个文件。
配置加载优先级:
多服务配置共享
不同服务之间共享配置文件的两种方式:
-
通过 shared-configs 指定
-
通过 extension-configs 指定
方式一:通过 shared-configs 指定
spring:
application:
name: userservice # 服务名称
profiles:
active: dev # 环境
cloud:
nacos:
server-addr: localhost:8848 # Nacos 地址
config:
file-extension: yaml # 文件后缀名
shared-configs: # 多微服务间共享的配置列表
- dataId: common.yaml # 要共享的配置文件 id
方式二:通过 extension-configs 指定
spring:
application:
name: userservice # 服务名称
profiles:
active: dev # 环境
cloud:
nacos:
server-addr: localhost:8848 # Nacos 地址
config:
file-extension: yaml # 文件后缀名
extends-configs: # 多微服务间共享的配置列表
- dataId: extend.yaml # 要共享的配置文件 id
多种配置的优先级:
如果出现:
Error processing condition on org.springframework.cloud.commons.httpclient.HttpClientConfigur....
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency>