Skip to content

Commit

Permalink
nacos 配置加密
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Jan 24, 2020
1 parent 76108a4 commit cbc90d6
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 3 deletions.
43 changes: 43 additions & 0 deletions lab-44/lab-44-nacos-config-demo-actuator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-44-nacos-config-demo-actuator</artifactId>

<dependencies>
<!-- 实现对 SpringMVC 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- 实现对 Nacos 作为配置中心的自动化配置 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.4</version>
</dependency>

<!-- 实现对 Actuator 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 实现对 Nacos 作为配置中心的 Actuator 的自动化配置 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-actuator</artifactId>
<version>0.2.4</version>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cn.iocoder.springboot.lab44.nacosdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
// @NacosPropertySource(dataId = "example", type = ConfigType.YAML)
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cn.iocoder.springboot.lab44.nacosdemo.controller;

import cn.iocoder.springboot.lab44.nacosdemo.properties.TestProperties;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class DemoController {

// @Value("${test}")
@NacosValue(value = "${test}", autoRefreshed = true)
private String test;

@GetMapping("/test")
public String test() {
return test;
}

@Autowired
private TestProperties testProperties;

@GetMapping("/test_properties")
public TestProperties testProperties() {
return testProperties;
}

private Logger logger = LoggerFactory.getLogger(getClass());

@GetMapping("/logger")
public void logger() {
logger.debug("[logger][测试一下]");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cn.iocoder.springboot.lab44.nacosdemo.listener;

import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
import com.alibaba.nacos.spring.util.parse.DefaultYamlConfigParse;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Properties;

@Component
public class LoggingSystemConfigListener {

/**
* 日志配置项的前缀
*/
private static final String LOGGER_TAG = "logging.level.";

@Resource
private LoggingSystem loggingSystem;

@NacosConfigListener(dataId = "${nacos.config.data-id}", type = ConfigType.YAML, timeout = 5000)
public void onChange(String newLog) throws Exception {
// 使用 DefaultYamlConfigParse 工具类,解析配置
Properties properties = new DefaultYamlConfigParse().parse(newLog);
// 遍历配置集的每个配置项,判断是否是 logging.level 配置项
for (Object t : properties.keySet()) {
String key = String.valueOf(t);
// 如果是 logging.level 配置项,则设置其对应的日志级别
if (key.startsWith(LOGGER_TAG)) {
// 获得日志级别
String strLevel = properties.getProperty(key, "info");
LogLevel level = LogLevel.valueOf(strLevel.toUpperCase());
// 设置日志级别到 LoggingSystem 中
loggingSystem.setLogLevel(key.replace(LOGGER_TAG, ""), level);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.iocoder.springboot.lab44.nacosdemo.properties;

import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@NacosConfigurationProperties(prefix = "", dataId = "${nacos.config.data-id}", type = ConfigType.YAML, autoRefreshed = true)
public class TestProperties {

/**
* 测试属性
*/
private String test;

public String getTest() {
return test;
}

public TestProperties setTest(String test) {
this.test = test;
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
nacos:
# Nacos 配置中心的配置项,对应 NacosConfigProperties 配置类
config:
server-addr: 127.0.0.1:18848 # Nacos 服务器地址
bootstrap:
enable: true # 是否开启 Nacos 配置预加载功能。默认为 false。
log-enable: true # 是否开启 Nacos 支持日志级别的加载时机。默认为 false。
data-id: example-auto-refresh # 使用的 Nacos 配置集的 dataId。
type: YAML # 使用的 Nacos 配置集的配置格式。默认为 PROPERTIES。
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP。
namespace: # 使用的 Nacos 的命名空间,默认为 null。
auto-refresh: true # 是否自动刷新,默认为 false。

management:
endpoint:
# Health 端点配置项,对应 HealthProperties 配置类
health:
show-details: ALWAYS # 何时显示完整的健康信息。默认为 NEVER 都不展示。可选 WHEN_AUTHORIZED 当经过授权的用户;可选 ALWAYS 总是展示。
endpoints:
# Actuator HTTP 配置项,对应 WebEndpointProperties 配置类
web:
exposure:
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public void encode() {
String applicationName = "demo-application";
System.out.println(encryptor.encrypt(applicationName));

// 第二个加密
applicationName = "demo-app";
System.out.println(encryptor.encrypt(applicationName));
// // 第二个加密
// applicationName = "demo-app";
// System.out.println(encryptor.encrypt(applicationName));
}

@Value("${spring.application.name}")
Expand Down
1 change: 1 addition & 0 deletions lab-44/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<module>lab-44-nacos-config-demo-profiles</module>
<module>lab-44-nacos-config-demo-auto-refresh</module>
<module>lab-44-nacos-config-demo-jasypt</module>
<module>lab-44-nacos-config-demo-actuator</module>
</modules>


Expand Down

0 comments on commit cbc90d6

Please sign in to comment.