Skip to content

Commit d8b4ba0

Browse files
committed
Add lesson 2
1 parent 58feee7 commit d8b4ba0

File tree

13 files changed

+594
-0
lines changed

13 files changed

+594
-0
lines changed

spring-cloud/lesson-2/README.md

Lines changed: 363 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,363 @@
1+
2+
3+
## Environment
4+
5+
6+
7+
`Environment`: `PropertySources` = 1:1
8+
9+
10+
11+
`PropertySources`: `PropertySource` = 1:N
12+
13+
[0] `PropertySource` (Map)
14+
15+
​ spring.application.name = spring-cloud-config-client
16+
17+
[1] `PropertySource`(Map)
18+
19+
​ spring.application.name = spring-cloud-config-client-demo
20+
21+
22+
23+
### Spring Boot 配置文件
24+
25+
26+
27+
#### application.properties 或 application.xml
28+
29+
30+
31+
加载器:`PropertiesPropertySourceLoader`
32+
33+
34+
35+
#### application.yml 或者 application.yaml
36+
37+
加载器:`YamlPropertySourceLoader`
38+
39+
40+
41+
## Environment 端点
42+
43+
44+
45+
### 请求 URI :`/env`
46+
47+
48+
49+
数据来源:`EnvironmentEndpoint`
50+
51+
Controller 来源:`EnvironmentMvcEndpoint`
52+
53+
54+
55+
56+
57+
## Bootstrap 配置
58+
59+
60+
61+
参考`BootstrapApplicationListener` 实现
62+
63+
64+
65+
> 注:程序启动参数的加载逻辑:
66+
>
67+
> `SpringApplication#configurePropertySources()`
68+
69+
70+
71+
## Bootstrap 配置文件
72+
73+
74+
75+
```java
76+
String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
77+
```
78+
79+
`spring.cloud.bootstrap.name` 存在时,使用该配置项,否则,使用 "bootstrap" 作为默认
80+
81+
82+
83+
```properties
84+
## application.properties
85+
## 通过调整 spring.cloud.bootstrap.enabled = false,尝试关闭 bootstrap 上下文
86+
## 实际测试结果,没有效果
87+
spring.cloud.bootstrap.enabled = false
88+
```
89+
90+
> 注意:`BootstrapApplicationListener` 加载实际早于 `ConfigFileApplicationListener`
91+
>
92+
> 原因是:
93+
>
94+
> `ConfigFileApplicationListener` 的 Order = Ordered.HIGHEST_PRECEDENCE + 10(第十一位)
95+
>
96+
> `BootstrapApplicationListener`的 Order = Ordered.HIGHEST_PRECEDENCE + 5(第六位)
97+
98+
99+
100+
如果需要调整 控制 Bootstrap 上下文行为配置,需要更高优先级,也就是说 Order 需要 < Ordered.HIGHEST_PRECEDENCE + 5 (越小越优先),比如使用程序启动参数:
101+
102+
```
103+
--spring.cloud.bootstrap.enabld = true
104+
```
105+
106+
107+
108+
109+
110+
### 调整 Bootstrap 配置
111+
112+
113+
114+
#### 调整 Bootstrap 配置文件名称
115+
116+
117+
118+
调整程序启动参数
119+
120+
```
121+
--spring.cloud.bootstrap.name=spring-cloud
122+
```
123+
124+
bootstrap 配置文件名称发生了改变"spring-cloud",现有三个文件:
125+
126+
* `application.properties`
127+
* **spring.application.name = spring-cloud-config-client**
128+
* `bootstrap.properties`
129+
* spring.application.name = spring-cloud-config-client-demo
130+
* `spring-cloud.properties`
131+
* **spring.application.name = spring-cloud**
132+
133+
134+
135+
运行结果(部分):
136+
137+
```json
138+
"applicationConfig: [classpath:/application.properties]": {
139+
"spring.cloud.bootstrap.enabled": "false",
140+
"endpoints.env.sensitive": "false",
141+
"spring.application.name": "spring-cloud-config-client"
142+
},
143+
...
144+
"applicationConfig: [classpath:/spring-cloud.properties]": {
145+
"spring.application.name": "spring-cloud-config-client"
146+
}
147+
```
148+
149+
150+
151+
#### 调整 Bootstrap 配置文件路径
152+
153+
154+
155+
保留 **Bootstrap 配置文件名称** 程序启动参数:
156+
157+
```properties
158+
--spring.cloud.bootstrap.name=spring-cloud
159+
```
160+
161+
162+
163+
调整 **Bootstrap 配置文件路径** 程序启动参数:
164+
165+
```properties
166+
--spring.cloud.bootstrap.location=config
167+
```
168+
169+
170+
171+
现有四个文件:
172+
173+
* `application.properties`
174+
* **spring.application.name = spring-cloud-config-client**
175+
* `bootstrap.properties`
176+
* spring.application.name = spring-cloud-config-client-demo
177+
* `spring-cloud.properties`
178+
* **spring.application.name = spring-cloud**
179+
* `config/spring-cloud.properties`
180+
* **spring.application.name = spring-cloud-2**
181+
182+
183+
184+
实际结果:
185+
186+
```json
187+
"applicationConfig: [classpath:/application.properties]": {
188+
"spring.cloud.bootstrap.enabled": "false",
189+
"endpoints.env.sensitive": "false",
190+
"spring.application.name": "spring-cloud-config-client"
191+
},
192+
...
193+
"applicationConfig: [classpath:/config/spring-cloud.properties]": {
194+
"spring.application.name": "spring-cloud-config-client"
195+
},
196+
"applicationConfig: [classpath:/spring-cloud.properties]": {
197+
"spring.application.name": "spring-cloud-config-client"
198+
},
199+
```
200+
201+
202+
203+
#### 覆盖远程配置属性
204+
205+
206+
207+
默认情况,Spring Cloud 是允许覆盖的,`spring.cloud.config.allowOverride=true`
208+
209+
210+
211+
通过程序启动参数,调整这个值为"**false**"
212+
213+
```properties
214+
--spring.cloud.config.allowOverride=false
215+
```
216+
217+
218+
219+
启动后,重新Postman 发送 POST 请求,调整`spring.application.name` 值为 "**spring-cloud-new**"
220+
221+
> 注意官方文档的说明:the remote property source has to grant it permission by setting `spring.cloud.config.allowOverride=true` (it doesn’t work to set this locally).
222+
223+
224+
225+
#### 自定义 Bootstrap 配置
226+
227+
1. 创建`META-INF/spring.factories`文件(类似于 Spring Boot 自定义 Starter)
228+
229+
2. 自定义 Bootstrap 配置 Configuration
230+
231+
```java
232+
package com.segmentfault.springcloudlesson2.boostrap;
233+
234+
import org.springframework.context.ApplicationContextInitializer;
235+
import org.springframework.context.ConfigurableApplicationContext;
236+
import org.springframework.context.annotation.Configuration;
237+
import org.springframework.core.env.ConfigurableEnvironment;
238+
import org.springframework.core.env.MapPropertySource;
239+
import org.springframework.core.env.MutablePropertySources;
240+
import org.springframework.core.env.PropertySource;
241+
242+
import java.util.HashMap;
243+
import java.util.Map;
244+
245+
/**
246+
* Bootstrap 配置 Bean
247+
*
248+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
249+
* @since Configuration
250+
*/
251+
@Configuration
252+
public class MyConfiguration implements ApplicationContextInitializer {
253+
254+
@Override
255+
public void initialize(ConfigurableApplicationContext applicationContext) {
256+
257+
// 从 ConfigurableApplicationContext 获取 ConfigurableEnvironment 实例
258+
ConfigurableEnvironment environment = applicationContext.getEnvironment();
259+
// 获取 PropertySources
260+
MutablePropertySources propertySources = environment.getPropertySources();
261+
// 定义一个新的 PropertySource,并且放置在首位
262+
propertySources.addFirst(createPropertySource());
263+
264+
}
265+
266+
private PropertySource createPropertySource() {
267+
268+
Map<String, Object> source = new HashMap<>();
269+
270+
source.put("name", "小马哥");
271+
272+
PropertySource propertySource = new MapPropertySource("my-property-source", source);
273+
274+
return propertySource;
275+
276+
}
277+
}
278+
```
279+
280+
281+
282+
3. 配置`META-INF/spring.factories`文件,关联Key `org.springframework.cloud.bootstrap.BootstrapConfiguration`
283+
284+
```properties
285+
org.springframework.cloud.bootstrap.BootstrapConfiguration= \
286+
com.segmentfault.springcloudlesson2.boostrap.MyConfiguration
287+
```
288+
289+
290+
291+
292+
293+
#### 自定义 Bootstrap 配置属性源
294+
295+
296+
297+
1. 实现`PropertySourceLocator`
298+
299+
```java
300+
package com.segmentfault.springcloudlesson2.boostrap;
301+
302+
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
303+
import org.springframework.core.env.*;
304+
305+
import java.util.HashMap;
306+
import java.util.Map;
307+
308+
/**
309+
* 自定义 {@link PropertySourceLocator} 实现
310+
*
311+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
312+
* @since PropertySourceLocator
313+
*/
314+
public class MyPropertySourceLocator implements PropertySourceLocator {
315+
316+
@Override
317+
public PropertySource<?> locate(Environment environment) {
318+
319+
if (environment instanceof ConfigurableEnvironment) {
320+
321+
ConfigurableEnvironment configurableEnvironment = ConfigurableEnvironment.class.cast(environment);
322+
323+
// 获取 PropertySources
324+
MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
325+
// 定义一个新的 PropertySource,并且放置在首位
326+
propertySources.addFirst(createPropertySource());
327+
328+
}
329+
return null;
330+
}
331+
332+
private PropertySource createPropertySource() {
333+
334+
Map<String, Object> source = new HashMap<>();
335+
336+
source.put("spring.application.name", "小马哥的 Spring Cloud 程序");
337+
// 设置名称和来源
338+
PropertySource propertySource = new MapPropertySource("over-bootstrap-property-source", source);
339+
340+
return propertySource;
341+
342+
}
343+
}
344+
```
345+
346+
347+
348+
2. 配置`META-INF/spring.factories`
349+
350+
```properties
351+
org.springframework.cloud.bootstrap.BootstrapConfiguration= \
352+
com.segmentfault.springcloudlesson2.boostrap.MyConfiguration,\
353+
com.segmentfault.springcloudlesson2.boostrap.MyPropertySourceLocator
354+
```
355+
356+
357+
358+
359+
360+
361+
362+
363+
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/

0 commit comments

Comments
 (0)