springboot 引入nacos
参考官网: https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html 依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
启动类bootstrap.yml
server:
port: 8080
servlet:
context-path: /api
spring:
profiles:
active: dev
servlet:
multipart:
max-file-size: 10MB
application:
name: lcem-server
cloud:
nacos:
config:
server-addr: XXXXX:8848
namespace: f70658b1-8a35-42bc-a219-23a309456ce8
group: DEFAULT_GROUP
file-extension: yaml
enabled: true
refresh-enabled: true
extension-configs:
- data-id: application-dev.yml
refresh: true
- data-id: securedev.yml
refresh: true
authEnabled: true
访问nacos网址:http://localhost:8080/nacos,初始用户名和密码都是nacos nacos配置需要放入的配置文件: 我的data_id 就是我的文件名 获取配置:
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${useLocalCache:false}")
private boolean useLocalCache;
@RequestMapping("/get")
public boolean get() {
return useLocalCache;
}
}
将读取出来的yml文件转换成对象
依赖:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
代码实现:
@Value("${authConfigUrl:secure.yml}")
private String secureUrl;
@Value("${spring.cloud.nacos.config.server-addr}")
private String SERVER_ADDR;
@Value("${spring.cloud.nacos.config.namespace}")
private String NAMESPACE;
@Value("${spring.cloud.nacos.config.group}")
private String GROUP;
@SneakyThrows
public HashMap getCityCodeListByNacosFactory() {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, SERVER_ADDR);
properties.put(PropertyKeyConst.NAMESPACE, NAMESPACE);
ConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(secureUrl, GROUP, 3000L);
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.findAndRegisterModules();
HashMap hashMap = new HashMap();
try {
hashMap = mapper.readValue(content, HashMap.class);
return hashMap;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return hashMap;
}
多环境配置: maven多环境配置 配置目录:
dev,uat,prod不同环境,这写配置是放在项目的pom文件中
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
<deploy.url>http://localhost:8080/manager/text</deploy.url>
<build-type>war</build-type>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
<deploy.url>http://host:port/manager/text</deploy.url>
<build-type>jar</build-type>
</properties>
</profile>
<profile>
<!-- uat环境 -->
<id>uat</id>
<properties>
<profiles.active>uat</profiles.active>
<deploy.url>http://host:port/manager/text</deploy.url>
<build-type>jar</build-type>
</properties>
</profile>
</profiles>
配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 资源根目录排除各环境的配置,使用单独的资源目录来指定 -->
<excludes>
<exclude>prod*.xml</include>
<include>**
idea本地启动成功,远程打包失败: 可以现本地打远程包看看内容 查看内容是否和自己预想的一致
|