Skip to content

Commit 291fc02

Browse files
committed
Add lesson 4
1 parent d6b84cd commit 291fc02

File tree

20 files changed

+863
-0
lines changed

20 files changed

+863
-0
lines changed

spring-cloud/lesson-4/README.md

Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
1+
# Spring Cloud 服务发现/注册
2+
3+
4+
5+
## Spring Cloud Netflix Eureka
6+
7+
8+
9+
### Eureka 服务器
10+
11+
12+
13+
#### 引入 Maven 依赖
14+
15+
16+
17+
```xml
18+
<dependency>
19+
<groupId>org.springframework.cloud</groupId>
20+
<artifactId>spring-cloud-starter-eureka-server</artifactId>
21+
</dependency>
22+
```
23+
24+
25+
26+
#### 激活 Eureka 服务器
27+
28+
29+
30+
```java
31+
package com.segumentfault.springcloudlesson4eurekaserver;
32+
33+
import org.springframework.boot.SpringApplication;
34+
import org.springframework.boot.autoconfigure.SpringBootApplication;
35+
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
36+
37+
@SpringBootApplication
38+
@EnableEurekaServer
39+
public class SpringCloudLesson4EurekaServerApplication {
40+
41+
public static void main(String[] args) {
42+
SpringApplication.run(SpringCloudLesson4EurekaServerApplication.class, args);
43+
}
44+
}
45+
```
46+
47+
48+
49+
#### 调整 Eureka 服务器配置
50+
51+
`application.properties`
52+
53+
```properties
54+
## Spring Cloud Eureka 服务器应用名称
55+
spring.application.name = spring-cloud-eureka-server
56+
57+
## Spring Cloud Eureka 服务器服务端口
58+
server.port = 9090
59+
60+
## 管理端口安全失效
61+
management.security.enabled = false
62+
```
63+
64+
65+
66+
#### 检验 Eureka Server
67+
68+
异常堆栈信息:
69+
70+
```
71+
[nfoReplicator-0] c.n.discovery.InstanceInfoReplicator : There was a problem with the instance info replicator
72+
73+
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
74+
```
75+
76+
77+
78+
http://localhost:9090/
79+
80+
运行效果:
81+
82+
># Instances currently registered with Eureka
83+
84+
85+
86+
问题原因:Eureka Server 既是注册服务器,也是客户端,默认情况,也需要配置注册中心地址。
87+
88+
> "description": "Spring Cloud Eureka Discovery Client"
89+
90+
91+
92+
#### 解决问题的方法
93+
94+
```properties
95+
## Spring Cloud Eureka 服务器作为注册中心
96+
## 通常情况下,不需要再注册到其他注册中心去
97+
## 同时,它也不需要获取客户端信息
98+
### 取消向注册中心注册
99+
eureka.client.register-with-eureka = false
100+
### 取消向注册中心获取注册信息(服务、实例信息)
101+
eureka.client.fetch-registry = false
102+
## 解决 Peer / 集群 连接问题
103+
eureka.instance.hostname = localhost
104+
eureka.client.serviceUrl.defaultZone = http://${eureka.instance.hostname}:${server.port}/eureka
105+
```
106+
107+
108+
109+
110+
111+
### Eureka 客户端
112+
113+
114+
115+
#### 引入 Maven 依赖
116+
117+
```xml
118+
<dependency>
119+
<groupId>org.springframework.cloud</groupId>
120+
<artifactId>spring-cloud-starter-eureka</artifactId>
121+
</dependency>
122+
```
123+
124+
125+
126+
#### 激活 Eureka 客户端
127+
128+
129+
130+
```java
131+
package com.segumentfault.springcloudlesson4eurekaclient;
132+
133+
import org.springframework.boot.SpringApplication;
134+
import org.springframework.boot.autoconfigure.SpringBootApplication;
135+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
136+
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
137+
138+
@SpringBootApplication
139+
@EnableEurekaClient
140+
//@EnableDiscoveryClient
141+
public class SpringCloudLesson4EurekaClientApplication {
142+
143+
public static void main(String[] args) {
144+
SpringApplication.run(SpringCloudLesson4EurekaClientApplication.class, args);
145+
}
146+
}
147+
```
148+
149+
150+
151+
#### 配置 Eureka 客户端
152+
153+
```properties
154+
## Spring Cloud Eureka 客户端应用名称
155+
spring.application.name = spring-cloud-eureka-client
156+
157+
## Spring Cloud Eureka 客户端服务端口
158+
server.port = 8080
159+
160+
## 管理端口安全失效
161+
management.security.enabled = false
162+
```
163+
164+
165+
166+
#### 检验 Eureka 客户端
167+
168+
发现与 Eureka 服务器控制台出现相同异常:
169+
170+
```
171+
[nfoReplicator-0] c.n.discovery.InstanceInfoReplicator : There was a problem with the instance info replicator
172+
173+
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
174+
```
175+
176+
177+
178+
#### 需要再次调整 Eureka 客户端配置
179+
180+
181+
182+
```properties
183+
## Spring Cloud Eureka 客户端应用名称
184+
spring.application.name = spring-cloud-eureka-client
185+
186+
## Spring Cloud Eureka 客户端服务端口
187+
server.port = 8080
188+
189+
## 管理端口安全失效
190+
management.security.enabled = false
191+
192+
## Spring Cloud Eureka 客户端 注册到 Eureka 服务器
193+
eureka.client.serviceUrl.defaultZone = http://localhost:9090/eureka
194+
```
195+
196+
197+
198+
199+
200+
### Spring Cloud Config 与 Eureka 整合
201+
202+
203+
204+
#### 调整 `spring-cloud-config-server` 作为 Eureka 客户端
205+
206+
207+
208+
#### 引入 Maven 依赖
209+
210+
```xml
211+
<dependency>
212+
<groupId>org.springframework.cloud</groupId>
213+
<artifactId>spring-cloud-starter-eureka</artifactId>
214+
</dependency>
215+
```
216+
217+
218+
219+
#### 激活 Eureka 客户端
220+
221+
```java
222+
package com.segmentfault.springcloudlesson4configserver;
223+
224+
import org.springframework.boot.SpringApplication;
225+
import org.springframework.boot.autoconfigure.SpringBootApplication;
226+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
227+
import org.springframework.cloud.config.server.EnableConfigServer;
228+
229+
@SpringBootApplication
230+
@EnableConfigServer
231+
@EnableDiscoveryClient
232+
public class SpringCloudLesson4ConfigServerApplication {
233+
234+
public static void main(String[] args) {
235+
SpringApplication.run(SpringCloudLesson4ConfigServerApplication.class, args);
236+
}
237+
}
238+
```
239+
240+
241+
242+
#### 调整 `spring-cloud-config-server` 配置
243+
244+
245+
246+
```properties
247+
## 配置服务器应用名称
248+
spring.application.name = spring-cloud-config-server
249+
250+
## 配置服务器端口
251+
server.port = 7070
252+
253+
## 关闭管理端actuator 的安全
254+
## /env /health 端口完全开放
255+
management.security.enabled = false
256+
257+
## 配置服务器文件系统git 仓库
258+
## ${user.dir} 减少平台文件系统的不一致
259+
# spring.cloud.config.server.git.uri = ${user.dir}/src/main/resources/configs
260+
261+
## 配置服务器远程 Git 仓库(GitHub)
262+
spring.cloud.config.server.git.uri = https://github.com/mercyblitz/tmp
263+
264+
## 强制拉去 Git 内容
265+
spring.cloud.config.server.git.force-pull = true
266+
267+
## spring-cloud-config-server 注册到 Eureka 服务器
268+
eureka.client.serviceUrl.defaultZone = http://localhost:9090/eureka
269+
```
270+
271+
272+
273+
#### 调整 `spring-cloud-eureka-client` 称为 Config 客户端
274+
275+
##### 引入 `spring-cloud-starter-config` Maven 依赖
276+
277+
```xml
278+
<dependency>
279+
<groupId>org.springframework.cloud</groupId>
280+
<artifactId>spring-cloud-starter-config</artifactId>
281+
</dependency>
282+
```
283+
284+
##### 创建 `bootstrap.properties`
285+
286+
##### 配置 Config 客户端配置
287+
288+
`bootstrap.properties`
289+
290+
```properties
291+
## 配置客户端应用关联的应用
292+
## spring.cloud.config.name 是可选的
293+
## 如果没有配置,采用 ${spring.application.name}
294+
spring.cloud.config.name = segmentfault
295+
## 关联 profile
296+
spring.cloud.config.profile = prod
297+
## 关联 label
298+
spring.cloud.config.label = master
299+
## 激活 Config 服务器发现
300+
spring.cloud.config.discovery.enabled = true
301+
## 配置 Config 服务器的应用名称(Service ID)
302+
spring.cloud.config.discovery.serviceId = spring-cloud-config-server
303+
```
304+
305+
306+
307+
##### 检验效果
308+
309+
启动发现,spring-cloud-config-server 服务无法找到,原因如下:
310+
311+
```
312+
注意:当前应用需要提前获取应用信息,那么将 Eureka 的配置信息提前至 bootstrap.properties 文件
313+
原因:bootstrap 上下文是 Spring Boot 上下文的 父 上下文,那么它最先加载,因此需要最优先加载 Eureka 注册信息
314+
```
315+
316+
317+
318+
##### 调整后配置
319+
320+
`bootstrap.properties`
321+
322+
```properties
323+
## 注意:当前应用需要提前获取应用信息,那么将 Eureka 的配置信息提前至 bootstrap.properties 文件
324+
## 原因:bootstrap 上下文是 Spring Boot 上下文的 父 上下文,那么它最先加载,因此需要最优先加载 Eureka 注册信息
325+
## Spring Cloud Eureka 客户端 注册到 Eureka 服务器
326+
eureka.client.serviceUrl.defaultZone = http://localhost:9090/eureka
327+
328+
## 配置客户端应用关联的应用
329+
## spring.cloud.config.name 是可选的
330+
## 如果没有配置,采用 ${spring.application.name}
331+
spring.cloud.config.name = segmentfault
332+
## 关联 profile
333+
spring.cloud.config.profile = prod
334+
## 关联 label
335+
spring.cloud.config.label = master
336+
## 激活 Config 服务器发现
337+
spring.cloud.config.discovery.enabled = true
338+
## 配置 Config 服务器的应用名称(Service ID)
339+
spring.cloud.config.discovery.serviceId = spring-cloud-config-server
340+
```
341+
342+
343+
344+
##### 再次检验效果
345+
346+
访问 http://localhost:8080/env:
347+
348+
```json
349+
"configService:https://github.com/mercyblitz/tmp/segmentfault-prod.properties": {
350+
"sf.user.id": "001",
351+
"sf.user.name": "xiaomage",
352+
"name": "segumentfault.com"
353+
},
354+
"configService:https://github.com/mercyblitz/tmp/segmentfault.properties": {
355+
"name": "segumentfault.com"
356+
},
357+
```
358+
359+
以上内容来自于`spring-cloud-config-server`:
360+
361+
http://localhost:7070/segmentfault-prod.properties
362+
363+
```properties
364+
name: segumentfault.com
365+
sf.user.id: 001
366+
sf.user.name: xiaomage
367+
```
368+
369+
370+
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)