Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb6c0bcf

Browse files
committed
completed config
1 parent00836f0 commitb6c0bcf

File tree

13 files changed

+523
-3
lines changed

13 files changed

+523
-3
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,101 @@
11
#实现微服务的集中化配置
22

3+
创建一个`micro-weather-config-client` 作为配置服务器的客户端。
4+
5+
##开发环境
6+
7+
* Gradle 4.0
8+
* Spring Boot 2.0.0.M3
9+
* Spring Cloud Netflix Eureka Client Finchley.M2
10+
* Spring Cloud Config Client Finchley.M2
11+
12+
##更改配置
13+
14+
增加如下配置:
15+
16+
```groovy
17+
dependencies {
18+
//...
19+
20+
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
21+
compile('org.springframework.cloud:spring-cloud-starter-config')
22+
23+
//...
24+
}
25+
```
26+
27+
项目配置:
28+
29+
```
30+
spring.application.name: micro-weather-config-client
31+
server.port=8089
32+
33+
eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
34+
35+
spring.cloud.config.profile=dev
36+
spring.cloud.config.uri= http://localhost:8888/
37+
```
38+
39+
其中:
40+
41+
* spring.cloud.config.uri : 指向了配置服务器`micro-weather-config-server`的位置。
42+
43+
##一个最简单的 Config Client
44+
45+
主应用:
46+
47+
```java
48+
@SpringBootApplication
49+
@EnableDiscoveryClient
50+
publicclassApplication {
51+
52+
publicstaticvoidmain(String[]args) {
53+
SpringApplication.run(Application.class, args);
54+
}
55+
56+
}
57+
```
58+
59+
##如何测试
60+
61+
62+
<https://github.com/waylau/spring-cloud-tutorial/tree/master/config> 我们放置了一个配置文件`micro-weather-config-client-dev.properties`,里面简单的放置了测试内容:
63+
64+
```
65+
auther=waylau.com
66+
```
67+
68+
![micro-weather-config-client](../../images/centralized-configuration/config-github.jpg)
69+
70+
71+
编写测试用例:
72+
73+
```java
74+
@RunWith(SpringRunner.class)
75+
@SpringBootTest
76+
publicclassApplicationTests {
77+
78+
@Value("${auther}")
79+
privateString auther;
80+
81+
@Test
82+
publicvoidcontextLoads() {
83+
System.out.println(auther);
84+
}
85+
86+
}
87+
```
88+
89+
启动在之前章节中搭建的`micro-weather-eureka-server``micro-weather-config-server` 两个项目。
90+
91+
92+
启动测试用例,如果一切正常,可以在控制台看到“waylau.com”字样,说明,我们拿到了`auther`在配置服务器中的内容。
93+
94+
如果同时也启动了`micro-weather-config-client` 项目,则能在
95+
启动在之前章节中搭建的`micro-weather-eureka-server` 管理界面,看到这个服务的信息。
96+
97+
![micro-weather-config-client](../../images/centralized-configuration/micro-weather-config-client.jpg)
98+
99+
##源码
100+
101+
本章节源码,见`micro-weather-config-client`

‎docs/centralized-configuration/config.md‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#如何集成 Config
22

3-
本章节,我们将创建一个`micro-weather-config-server` 作为配置服务器的服务端,创建一个`micro-weather-config-client` 作为配置服务器的客户端
3+
本章节,我们将创建一个`micro-weather-config-server` 作为配置服务器的服务端。
44

55
##开发环境
66

77
* Gradle 4.0
88
* Spring Boot 2.0.0.M3
9+
* Spring Cloud Netflix Eureka Client Finchley.M2
910
* Spring Cloud Config Server Finchley.M2
10-
* Spring Cloud Config Client Finchley.M2
11+
1112

1213
##更改配置
1314

@@ -41,7 +42,50 @@ spring.cloud.config.server.git.searchPaths=config
4142
* spring.cloud.config.server.git.uri:配置Git仓库地址
4243
* spring.cloud.config.server.git.searchPaths:配置查找配置的路径
4344

45+
##一个最简单的 Config Server
46+
47+
主应用:
48+
49+
```java
50+
@SpringBootApplication
51+
@EnableDiscoveryClient
52+
@EnableConfigServer
53+
publicclassApplication {
54+
55+
publicstaticvoidmain(String[]args) {
56+
SpringApplication.run(Application.class, args);
57+
}
58+
59+
}
60+
```
61+
62+
在程序的入口Application类加上`@EnableConfigServer`注解开启配置服务器的功能。
4463

4564
##测试
4665

47-
启动应用
66+
<https://github.com/waylau/spring-cloud-tutorial/tree/master/config> 我们放置了一个配置文件`micro-weather-config-client-dev.properties`,里面简单的放置了测试内容:
67+
68+
```
69+
auther=waylau.com
70+
```
71+
72+
启动应用,访问<http://localhost:8888/auther/dev>,应能看到如下输出内容,说明服务启动正常。
73+
74+
```json
75+
{"name":"auther","profiles":["dev"],"label":null,"version":"00836f0fb49488bca170c8227e3ef13a5aff2d1a","state":null,"propertySources":[]}
76+
```
77+
78+
其中,在配置中心的文件命名规则如下:
79+
80+
```
81+
/{application}/{profile}[/{label}]
82+
/{application}-{profile}.yml
83+
/{label}/{application}-{profile}.yml
84+
/{application}-{profile}.properties
85+
/{label}/{application}-{profile}.properties
86+
```
87+
88+
89+
##源码
90+
91+
本章节源码,见`micro-weather-config-server`
28.5 KB
Loading
78.1 KB
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.gradle
2+
/build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
nbproject/private/
21+
build/
22+
nbbuild/
23+
dist/
24+
nbdist/
25+
.nb-gradle/
26+
/bin/
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
buildscript {
2+
ext {
3+
springBootVersion='2.0.0.M3'
4+
}
5+
repositories {
6+
//mavenCentral()
7+
maven { url"https://repo.spring.io/snapshot" }
8+
maven { url"https://repo.spring.io/milestone" }
9+
maven { url"http://maven.aliyun.com/nexus/content/groups/public/" }
10+
}
11+
dependencies {
12+
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
13+
}
14+
}
15+
16+
applyplugin:'java'
17+
applyplugin:'eclipse'
18+
applyplugin:'org.springframework.boot'
19+
applyplugin:'io.spring.dependency-management'
20+
21+
version='1.0.0'
22+
sourceCompatibility=1.8
23+
24+
repositories {
25+
//mavenCentral()
26+
maven { url"https://repo.spring.io/snapshot" }
27+
maven { url"https://repo.spring.io/milestone" }
28+
maven { url"http://maven.aliyun.com/nexus/content/groups/public/" }
29+
}
30+
31+
ext {
32+
springCloudVersion='Finchley.M2'
33+
}
34+
35+
dependencies {
36+
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
37+
compile('org.springframework.cloud:spring-cloud-starter-config')
38+
testCompile('org.springframework.boot:spring-boot-starter-test')
39+
}
40+
41+
dependencyManagement {
42+
imports {
43+
mavenBom"org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
44+
}
45+
}
46+
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
zipStoreBase=GRADLE_USER_HOME
4+
zipStorePath=wrapper/dists
5+
#distributionUrl=https\://services.gradle.org/distributions/gradle-3.5.1-bin.zip
6+
distributionUrl=file\:/D:/software/webdev/java/gradle-4.0-all.zip

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp