mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
Apollo 自动刷新示例
This commit is contained in:
30
lab-45/lab-45-apollo-demo-auto-refresh/pom.xml
Normal file
30
lab-45/lab-45-apollo-demo-auto-refresh/pom.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?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-45-apollo-demo-auto-refresh</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 SpringMVC 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Apollo 客户端,内置对 Apollo 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.ctrip.framework.apollo</groupId>
|
||||
<artifactId>apollo-client</artifactId>
|
||||
<version>1.5.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springboot.lab45.apollodemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package cn.iocoder.springboot.lab45.apollodemo.controller;
|
||||
|
||||
import cn.iocoder.springboot.lab45.apollodemo.properties.TestProperties;
|
||||
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.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.test}")
|
||||
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][测试一下]");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package cn.iocoder.springboot.lab45.apollodemo.listener;
|
||||
|
||||
import org.springframework.boot.logging.LoggingSystem;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@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);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.springboot.lab45.apollodemo.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "test")
|
||||
public class TestProperties {
|
||||
|
||||
/**
|
||||
* 测试属性
|
||||
*/
|
||||
private String test;
|
||||
|
||||
public String getTest() {
|
||||
return test;
|
||||
}
|
||||
|
||||
public TestProperties setTest(String test) {
|
||||
this.test = test;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
server:
|
||||
port: 7070 # 避免和本地的 Apollo Portal 端口冲突
|
||||
|
||||
app:
|
||||
id: demo-application # 使用的 Apollo 的项目(应用)编号
|
||||
apollo:
|
||||
meta: http://127.0.0.1:8080 # Apollo Meta Server 地址
|
||||
bootstrap:
|
||||
enabled: true # 是否开启 Apollo 配置预加载功能。默认为 false。
|
||||
eagerLoad:
|
||||
enable: true # 是否开启 Apollo 支持日志级别的加载时机。默认为 false。
|
||||
namespaces: application # 使用的 Apollo 的命名空间,默认为 application。
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
<modules>
|
||||
<module>lab-45-apollo-demo</module>
|
||||
<module>lab-45-apollo-demo-profiles</module>
|
||||
<module>lab-45-apollo-demo-auto-refresh</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user