支持nacos分布式外部配置

This commit is contained in:
xlongwei 2019-09-28 22:41:50 +08:00
parent 11c4287ee9
commit 3c943bf78b
2 changed files with 35 additions and 0 deletions

View File

@ -29,6 +29,13 @@
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> </dependency>
<!-- 支持nacos分布式外部配置 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.3</version>
</dependency>
<!-- spring-boot-devtools --> <!-- spring-boot-devtools -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -0,0 +1,28 @@
package com.ruoyi.web.core.config;
import com.alibaba.nacos.api.annotation.NacosProperties;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.context.annotation.Configuration;
/**
* nacos分布式外部配置
*
* <pre>
* 1nacos路径默认127.0.0.1:8848可以写配置文件nacos.config.server-addr也可以用环境变量 env 或系统属性 -D
* 2nacos添加配置ruoyi.yml内容格式yml可访问toyaml.com转换properties格式与type=ConfigType.YAML格式一致
* 3DruidProperties使用@Value加载nacos配置没有nacos则使用配置文件信息数据库配置通常不必动态刷新
* 4nacos支持动态更新配置使用@NacosValue注解成员field字段或set方法即可
* 5可以使用{@link com.alibaba.nacos.spring.context.annotation.config.NacosPropertySources NacosPropertySources
* }配置多个{@link NacosPropertySource}例如dataId=ruoyi.yml用于覆盖数据库配置等dataId=ruoyi-dynamic.yml用于动态配置
* </pre>
*
* @author xlongwei
*/
@Configuration
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "${nacos.config.server-addr:127.0.0.1:8848}"))
@NacosPropertySource(dataId = "ruoyi.yml", type = ConfigType.YAML, autoRefreshed = true, first = true)
public class NacosConfig {
}