-
Notifications
You must be signed in to change notification settings - Fork 328
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
Showing
7 changed files
with
143 additions
and
0 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,4 @@ | ||
.* | ||
target/* | ||
*.iml | ||
!.gitignore |
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,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> |
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,12 @@ | ||
## 002-dynamic-envronment | ||
|
||
### 项目说明 | ||
|
||
本项目演示了如何使用自定义的配置源,以及配置的动态刷新 | ||
|
||
- 自定义 `MapPropertySource`来实现自定义的配置源 | ||
- 通过刷新`MapPropertySource`的source,从而实现配置的动态刷新 | ||
- 注意将其注册到`Envrionment`之后,可以配合`@Value`注解来试下自定义配置源的配置绑定(注意这时候不支持刷新,如果需要配置刷新,结合 002-properties-value 这个项目来实现动态刷新支持) | ||
|
||
### 博文说明 | ||
|
39 changes: 39 additions & 0 deletions
39
...g-boot/002-dynamic-envronment/src/main/java/com/git/hui/boot/dynamic/env/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,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); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...ic-envronment/src/main/java/com/git/hui/boot/dynamic/env/config/FilePropertiesSource.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,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); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
spring-boot/002-dynamic-envronment/src/main/resources/kv.properties
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,3 @@ | ||
user=xhh | ||
name=一灰灰 | ||
age=18 |
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