Skip to content

Commit

Permalink
自定义配置源 & 配置动态刷新支持实例demo
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Jun 9, 2021
1 parent 0ee9c1f commit 01e4f2d
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 0 deletions.
4 changes: 4 additions & 0 deletions spring-boot/002-dynamic-envronment/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.*
target/*
*.iml
!.gitignore
21 changes: 21 additions & 0 deletions spring-boot/002-dynamic-envronment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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>spring-boot</artifactId>
<groupId>com.git.hui.boot</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>002-dynamic-envronment</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

</project>
12 changes: 12 additions & 0 deletions spring-boot/002-dynamic-envronment/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## 002-dynamic-envronment

### 项目说明

本项目演示了如何使用自定义的配置源,以及配置的动态刷新

- 自定义 `MapPropertySource`来实现自定义的配置源
- 通过刷新`MapPropertySource`的source,从而实现配置的动态刷新
- 注意将其注册到`Envrionment`之后,可以配合`@Value`注解来试下自定义配置源的配置绑定(注意这时候不支持刷新,如果需要配置刷新,结合 002-properties-value 这个项目来实现动态刷新支持)

### 博文说明

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.git.hui.boot.dynamic.env;

import com.git.hui.boot.dynamic.env.config.FilePropertiesSource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author yihui
* @date 2021/6/9
*/
@EnableScheduling
@RestController
@SpringBootApplication
public class Application {
private Environment environment;

@Bean
public FilePropertiesSource filePropertiesSource(ConfigurableEnvironment environment) {
this.environment = environment;
FilePropertiesSource filePropertiesSource = new FilePropertiesSource();
environment.getPropertySources().addLast(filePropertiesSource);
return filePropertiesSource;
}

@GetMapping(path = "get")
public String getProperty(String key) {
return environment.getProperty(key);
}

public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.git.hui.boot.dynamic.env.config;

import org.springframework.core.env.MapPropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
* 这里以从自定义的配置文件中读取配置,来演示配置的数据源自定义(如果希望从redis/db/http获取配置,只需要修改下面的refreshSource;或者修改 getProperty)
*
* @author yihui
* @date 2021/6/9
*/
public class FilePropertiesSource extends MapPropertySource {
public FilePropertiesSource(String name, Map<String, Object> source) {
super(name, source);
}

public FilePropertiesSource() {
this("filePropertiesSource", new HashMap<>());
}

// 这种方式,适用于一次捞取所有的配置,然后从内存中查询对应的配置,提高服务性能
// 10s 更新一次
@PostConstruct
@Scheduled(fixedRate = 10_000)
public void refreshSource() throws IOException {
String ans =
FileCopyUtils.copyToString(new InputStreamReader(FilePropertiesSource.class.getClassLoader().getResourceAsStream("kv.properties")));
Map<String, Object> map = new HashMap<>();
for (String sub : StringUtils.split(ans, "\n")) {
if (sub.isEmpty()) {
continue;
}
String[] kv = StringUtils.split(sub, "=");
if (kv.length != 2) {
continue;
}

map.put(kv[0].trim(), kv[1].trim());
}

source.clear();
source.putAll(map);
}

/**
* 覆盖这个方法,适用于实时获取配置
*
* @param name
* @return
*/
@Override
public Object getProperty(String name) {
return super.getProperty(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user=xhh
name=一灰灰
age=18
1 change: 1 addition & 0 deletions spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<module>420-prometheus-basic</module>
<module>150-i18n</module>
<module>002-properties-value</module>
<module>002-dynamic-envronment</module>


</modules>
Expand Down

0 comments on commit 01e4f2d

Please sign in to comment.