mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 spring cloud alibaba nacos 配置中心
This commit is contained in:
@@ -124,6 +124,7 @@
|
||||
|
||||
## Spring Cloud Alibaba 全家桶
|
||||
|
||||
* 《芋道 Spring Cloud Alibaba 介绍》
|
||||
* [《芋道 Spring Cloud Alibaba 注册中心 Nacos 入门》](http://www.iocoder.cn/Spring-Cloud-Alibaba/Nacos-Discovery/?github) 对应 [labx-01](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-01)
|
||||
* 《芋道 Spring Cloud Alibaba 服务调用 Dubbo 入门》
|
||||
* [《芋道 Spring Cloud Alibaba 服务容错 Sentinel 入门》](http://www.iocoder.cn/Spring-Cloud-Alibaba/Sentinel/?github) 对应 [labx-04](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-04)
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-provider # Spring 应用名
|
||||
cloud:
|
||||
nacos:
|
||||
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
|
||||
server:
|
||||
port: ${random.int[10000,19999]} # 服务器端口。默认为 8080
|
||||
# port: 18080 # 服务器端口。默认为 8080
|
||||
@@ -12,6 +12,8 @@
|
||||
<artifactId>labx-05-sca-nacos-config-auto-refresh</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
|
||||
@@ -4,7 +4,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
//@NacosConfigurationProperties(prefix = "order", dataId = "${nacos.config.data-id}", type = ConfigType.YAML)
|
||||
@ConfigurationProperties(prefix = "order")
|
||||
public class OrderProperties {
|
||||
|
||||
@@ -18,11 +17,6 @@ public class OrderProperties {
|
||||
*/
|
||||
private Integer createFrequencySeconds;
|
||||
|
||||
// /**
|
||||
// * 配置描述
|
||||
// */
|
||||
// private String desc;
|
||||
|
||||
public Integer getPayTimeoutSeconds() {
|
||||
return payTimeoutSeconds;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,11 @@ package cn.iocoder.springcloudalibaba.labx5.nacosdemo.controller;
|
||||
|
||||
import cn.iocoder.springcloudalibaba.labx5.nacosdemo.config.OrderProperties;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -12,6 +15,7 @@ import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
@RefreshScope
|
||||
public class DemoController {
|
||||
|
||||
@Autowired
|
||||
@@ -39,4 +43,11 @@ public class DemoController {
|
||||
.fluentPut("createFrequencySeconds", createFrequencySeconds);
|
||||
}
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@GetMapping("/logger")
|
||||
public void logger() {
|
||||
logger.debug("[logger][测试一下]");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.springcloudalibaba.labx5.nacosdemo.listener;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DemoEnvironmentChangeListener implements ApplicationListener<EnvironmentChangeEvent> {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private ConfigurableEnvironment environment;
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(EnvironmentChangeEvent event) {
|
||||
for (String key : event.getKeys()) {
|
||||
logger.info("[onApplicationEvent][key({}) 最新 value 为 {}]", key, environment.getProperty(key));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-application
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
# Nacos Config 配置项,对应 NacosConfigProperties 配置属性类
|
||||
config:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
namespace: # 使用的 Nacos 的命名空间,默认为 null
|
||||
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP
|
||||
name: # 使用的 Nacos 配置集的 dataId,默认为 spring.application.name
|
||||
file-extension: yaml # 使用的 Nacos 配置集的 dataId 的文件拓展名,同时也是 Nacos 配置集的配置格式,默认为 properties
|
||||
70
labx-05/labx-05-sca-nacos-config-demo-actuator/pom.xml
Normal file
70
labx-05/labx-05-sca-nacos-config-demo-actuator/pom.xml
Normal file
@@ -0,0 +1,70 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>labx-05</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-05-sca-nacos-config-demo-actuator</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Config 相关依赖,将 Nacos 作为配置中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Actuator 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloudalibaba.labx5.nacosdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package cn.iocoder.springcloudalibaba.labx5.nacosdemo.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
//@NacosConfigurationProperties(prefix = "order", dataId = "${nacos.config.data-id}", type = ConfigType.YAML)
|
||||
@ConfigurationProperties(prefix = "order")
|
||||
public class OrderProperties {
|
||||
|
||||
/**
|
||||
* 订单支付超时时长,单位:秒。
|
||||
*/
|
||||
private Integer payTimeoutSeconds;
|
||||
|
||||
/**
|
||||
* 订单创建频率,单位:秒
|
||||
*/
|
||||
private Integer createFrequencySeconds;
|
||||
|
||||
// /**
|
||||
// * 配置描述
|
||||
// */
|
||||
// private String desc;
|
||||
|
||||
public Integer getPayTimeoutSeconds() {
|
||||
return payTimeoutSeconds;
|
||||
}
|
||||
|
||||
public OrderProperties setPayTimeoutSeconds(Integer payTimeoutSeconds) {
|
||||
this.payTimeoutSeconds = payTimeoutSeconds;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getCreateFrequencySeconds() {
|
||||
return createFrequencySeconds;
|
||||
}
|
||||
|
||||
public OrderProperties setCreateFrequencySeconds(Integer createFrequencySeconds) {
|
||||
this.createFrequencySeconds = createFrequencySeconds;
|
||||
return this;
|
||||
}
|
||||
|
||||
// public String getDesc() {
|
||||
// return desc;
|
||||
// }
|
||||
//
|
||||
// public OrderProperties setDesc(String desc) {
|
||||
// this.desc = desc;
|
||||
// return this;
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package cn.iocoder.springcloudalibaba.labx5.nacosdemo.controller;
|
||||
|
||||
import cn.iocoder.springcloudalibaba.labx5.nacosdemo.config.OrderProperties;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
public class DemoController {
|
||||
|
||||
@Autowired
|
||||
private OrderProperties orderProperties;
|
||||
|
||||
/**
|
||||
* 测试 @ConfigurationProperties 注解的配置属性类
|
||||
*/
|
||||
@GetMapping("/test01")
|
||||
public OrderProperties test01() {
|
||||
return orderProperties;
|
||||
}
|
||||
|
||||
@Value(value = "${order.pay-timeout-seconds}") // @NacosValue(value = "${order.pay-timeout-seconds}")
|
||||
private Integer payTimeoutSeconds;
|
||||
@Value(value = "${order.create-frequency-seconds}") // @NacosValue(value = "${order.create-frequency-seconds}")
|
||||
private Integer createFrequencySeconds;
|
||||
|
||||
/**
|
||||
* 测试 @Value 注解的属性
|
||||
*/
|
||||
@GetMapping("/test02")
|
||||
public Map<String, Object> test02() {
|
||||
return new JSONObject().fluentPut("payTimeoutSeconds", payTimeoutSeconds)
|
||||
.fluentPut("createFrequencySeconds", createFrequencySeconds);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
management:
|
||||
endpoint:
|
||||
# Health 端点配置项,对应 HealthProperties 配置类
|
||||
health:
|
||||
show-details: ALWAYS # 何时显示完整的健康信息。默认为 NEVER 都不展示。可选 WHEN_AUTHORIZED 当经过授权的用户;可选 ALWAYS 总是展示。
|
||||
endpoints:
|
||||
# Actuator HTTP 配置项,对应 WebEndpointProperties 配置类
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
@@ -0,0 +1,13 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-application
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
# Nacos Config 配置项,对应 NacosConfigProperties 配置属性类
|
||||
config:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
namespace: # 使用的 Nacos 的命名空间,默认为 null
|
||||
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP
|
||||
name: # 使用的 Nacos 配置集的 dataId,默认为 spring.application.name
|
||||
file-extension: yaml # 使用的 Nacos 配置集的 dataId 的文件拓展名,同时也是 Nacos 配置集的配置格式,默认为 properties
|
||||
81
labx-05/labx-05-sca-nacos-config-demo-jasypt/pom.xml
Normal file
81
labx-05/labx-05-sca-nacos-config-demo-jasypt/pom.xml
Normal file
@@ -0,0 +1,81 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>labx-05</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-05-sca-nacos-config-demo-jasypt</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Config 相关依赖,将 Nacos 作为配置中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Jasypt 实现自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.github.ulisesbocchio</groupId>
|
||||
<artifactId>jasypt-spring-boot-starter</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<!-- <scope>test</scope>-->
|
||||
</dependency>
|
||||
|
||||
<!-- 方便等会写单元测试 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloudalibaba.labx5.nacosdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.iocoder.springcloudalibaba.labx5.nacosdemo.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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
@RefreshScope
|
||||
public class DemoController {
|
||||
|
||||
@Value("${xxx-password:}")
|
||||
private String xxxPassword;
|
||||
|
||||
@GetMapping("/test")
|
||||
public String test() {
|
||||
return xxxPassword;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package cn.iocoder.springcloudalibaba.labx5.nacosdemo.listener;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jasypt.encryption.StringEncryptor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
||||
import org.springframework.cloud.context.environment.EnvironmentManager;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class JasyptEnvironmentChangeListener implements ApplicationListener<EnvironmentChangeEvent> {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
// Environment 管理器,可以实现配置项的获取和修改
|
||||
@Autowired
|
||||
private EnvironmentManager environmentManager;
|
||||
|
||||
// Jasypt 加密器,可以对配置项进行加密和加密
|
||||
@Autowired
|
||||
private StringEncryptor encryptor;
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(EnvironmentChangeEvent event) {
|
||||
for (String key : event.getKeys()) {
|
||||
// 获得 value
|
||||
Object valueObj = environmentManager.getProperty(key);
|
||||
if (!(valueObj instanceof String)) {
|
||||
continue;
|
||||
}
|
||||
String value = (String) valueObj;
|
||||
// 判断 value 是否为加密。如果是,则进行解密
|
||||
if (value.startsWith("ENC(") && value.endsWith(")")) {
|
||||
value = encryptor.decrypt(StringUtils.substringBetween(value, "ENC(", ")"));
|
||||
logger.info("[onApplicationEvent][key({}) 解密后为 {}]", key, value);
|
||||
// 设置到 Environment 中
|
||||
environmentManager.setProperty(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-application-jasypt
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
# Nacos Config 配置项,对应 NacosConfigProperties 配置属性类
|
||||
config:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
namespace: # 使用的 Nacos 的命名空间,默认为 null
|
||||
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP
|
||||
name: # 使用的 Nacos 配置集的 dataId,默认为 spring.application.name
|
||||
file-extension: yaml # 使用的 Nacos 配置集的 dataId 的文件拓展名,同时也是 Nacos 配置集的配置格式,默认为 properties
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.iocoder.springcloudalibaba.labx5.nacosdemo;
|
||||
|
||||
import org.jasypt.encryption.StringEncryptor;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class JasyptTest {
|
||||
|
||||
@Autowired
|
||||
private StringEncryptor encryptor;
|
||||
|
||||
@Test
|
||||
public void encode() {
|
||||
// 第一个加密
|
||||
String password = "woshimima";
|
||||
System.out.println(encryptor.encrypt(password));
|
||||
|
||||
// 第二个加密
|
||||
password = "bushimima";
|
||||
System.out.println(encryptor.encrypt(password));
|
||||
}
|
||||
|
||||
@Value("${xxx-password:}")
|
||||
private String xxxPassword;
|
||||
|
||||
@Test
|
||||
public void print() {
|
||||
System.out.println(xxxPassword);
|
||||
}
|
||||
|
||||
// @Value("${jasypt.encryptor.password}")
|
||||
// private String password;
|
||||
|
||||
}
|
||||
64
labx-05/labx-05-sca-nacos-config-demo-multi/pom.xml
Normal file
64
labx-05/labx-05-sca-nacos-config-demo-multi/pom.xml
Normal file
@@ -0,0 +1,64 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>labx-05</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-05-sca-nacos-config-demo-multi</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Config 相关依赖,将 Nacos 作为配置中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package cn.iocoder.springcloudalibaba.labx5.nacosdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 启动 Spring Boot 应用
|
||||
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
|
||||
|
||||
// 查看 Environment
|
||||
Environment environment = context.getEnvironment();
|
||||
System.out.println(environment);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-application
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
# Nacos Config 配置项,对应 NacosConfigProperties 配置属性类
|
||||
config:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
namespace: # 使用的 Nacos 的命名空间,默认为 null
|
||||
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP
|
||||
name: ${spring.application.name} # 使用的 Nacos 配置集的 dataId,默认为 spring.application.name
|
||||
file-extension: yaml # 使用的 Nacos 配置集的 dataId 的文件拓展名,同时也是 Nacos 配置集的配置格式,默认为 properties
|
||||
# 拓展配置集数组,对应 Config 数组
|
||||
extension-configs:
|
||||
- data-id: extension-dataId-01.yaml
|
||||
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP
|
||||
refresh: true # 是否自动刷新配置,默认为 false
|
||||
- data-id: extension-dataId-02.yaml
|
||||
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP
|
||||
refresh: true # 是否自动刷新配置,默认为 false
|
||||
# 共享配置集数组,对应 Config 数组
|
||||
shared-configs:
|
||||
- data-id: shared-dataId-01.yaml
|
||||
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP
|
||||
refresh: true # 是否自动刷新配置,默认为 false
|
||||
- data-id: shared-dataId-02.yaml
|
||||
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP
|
||||
refresh: true # 是否自动刷新配置,默认为 false
|
||||
|
||||
profiles:
|
||||
active: dev # 设置开启的 Profiles
|
||||
@@ -15,6 +15,9 @@
|
||||
<module>labx-05-sca-nacos-config-demo</module>
|
||||
<module>labx-05-sca-nacos-config-demo-profiles</module>
|
||||
<module>labx-05-sca-nacos-config-auto-refresh</module>
|
||||
<module>labx-05-sca-nacos-config-demo-jasypt</module>
|
||||
<module>labx-05-sca-nacos-config-demo-actuator</module>
|
||||
<module>labx-05-sca-nacos-config-demo-multi</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user