forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Jan 24, 2020
1 parent
76108a4
commit cbc90d6
Showing
8 changed files
with
191 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
14 changes: 14 additions & 0 deletions
14
...config-demo-actuator/src/main/java/cn/iocoder/springboot/lab44/nacosdemo/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...tuator/src/main/java/cn/iocoder/springboot/lab44/nacosdemo/controller/DemoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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][测试一下]"); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...main/java/cn/iocoder/springboot/lab44/nacosdemo/listener/LoggingSystemConfigListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...tuator/src/main/java/cn/iocoder/springboot/lab44/nacosdemo/properties/TestProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
lab-44/lab-44-nacos-config-demo-actuator/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 两个端点。通过设置 * ,可以开放所有端点。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters