From e55eb2b093f2fa6e48c242eee51a8e2f14b0369a Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Mon, 27 Jan 2020 15:10:06 +0800 Subject: [PATCH] =?UTF-8?q?Apollo=20=E8=87=AA=E5=8A=A8=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lab-45-apollo-demo-auto-refresh/pom.xml | 30 ++++++++++++++ .../lab45/apollodemo/Application.java | 13 +++++++ .../apollodemo/controller/DemoController.java | 39 +++++++++++++++++++ .../listener/LoggingSystemConfigListener.java | 37 ++++++++++++++++++ .../apollodemo/properties/TestProperties.java | 24 ++++++++++++ .../src/main/resources/application.yaml | 13 +++++++ lab-45/pom.xml | 1 + 7 files changed, 157 insertions(+) create mode 100644 lab-45/lab-45-apollo-demo-auto-refresh/pom.xml create mode 100644 lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/Application.java create mode 100644 lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/controller/DemoController.java create mode 100644 lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/listener/LoggingSystemConfigListener.java create mode 100644 lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/properties/TestProperties.java create mode 100644 lab-45/lab-45-apollo-demo-auto-refresh/src/main/resources/application.yaml diff --git a/lab-45/lab-45-apollo-demo-auto-refresh/pom.xml b/lab-45/lab-45-apollo-demo-auto-refresh/pom.xml new file mode 100644 index 00000000..150bad5e --- /dev/null +++ b/lab-45/lab-45-apollo-demo-auto-refresh/pom.xml @@ -0,0 +1,30 @@ + + + + org.springframework.boot + spring-boot-starter-parent + 2.2.2.RELEASE + + + 4.0.0 + + lab-45-apollo-demo-auto-refresh + + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.ctrip.framework.apollo + apollo-client + 1.5.1 + + + + diff --git a/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/Application.java b/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/Application.java new file mode 100644 index 00000000..b3bdae21 --- /dev/null +++ b/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/Application.java @@ -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); + } + +} diff --git a/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/controller/DemoController.java b/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/controller/DemoController.java new file mode 100644 index 00000000..333314aa --- /dev/null +++ b/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/controller/DemoController.java @@ -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][测试一下]"); + } + +} diff --git a/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/listener/LoggingSystemConfigListener.java b/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/listener/LoggingSystemConfigListener.java new file mode 100644 index 00000000..b88e338f --- /dev/null +++ b/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/listener/LoggingSystemConfigListener.java @@ -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); +// } +// } +// } + +} diff --git a/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/properties/TestProperties.java b/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/properties/TestProperties.java new file mode 100644 index 00000000..6cb5be18 --- /dev/null +++ b/lab-45/lab-45-apollo-demo-auto-refresh/src/main/java/cn/iocoder/springboot/lab45/apollodemo/properties/TestProperties.java @@ -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; + } + +} diff --git a/lab-45/lab-45-apollo-demo-auto-refresh/src/main/resources/application.yaml b/lab-45/lab-45-apollo-demo-auto-refresh/src/main/resources/application.yaml new file mode 100644 index 00000000..27c26fd5 --- /dev/null +++ b/lab-45/lab-45-apollo-demo-auto-refresh/src/main/resources/application.yaml @@ -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。 + diff --git a/lab-45/pom.xml b/lab-45/pom.xml index 8d02ac8e..b730d4ff 100644 --- a/lab-45/pom.xml +++ b/lab-45/pom.xml @@ -14,6 +14,7 @@ lab-45-apollo-demo lab-45-apollo-demo-profiles + lab-45-apollo-demo-auto-refresh