Skip to content

Commit e1841e7

Browse files
committed
Add lesson 5
1 parent ddd47bd commit e1841e7

File tree

21 files changed

+831
-0
lines changed

21 files changed

+831
-0
lines changed

spring-cloud/lesson-5/README.md

+281
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
# Spring Cloud 高可用服务治理
2+
3+
4+
5+
## Spring Cloud Eureka
6+
7+
8+
9+
### Eureka 客户端
10+
11+
12+
13+
#### 配置多Eureka 注册中心
14+
15+
16+
17+
```properties
18+
## 应用名称
19+
spring.application.name = spring-cloud-eureka-client
20+
21+
## 客户端 端口随即可用
22+
server.port = 0
23+
24+
## 配置连接 Eureka 服务器
25+
## 配置多个 Eureka 注册中心,以"," 分割
26+
eureka.client.serviceUrl.defaultZone = \
27+
http://localhost:9090/eureka,\
28+
http://localhost:9091/eureka
29+
```
30+
31+
32+
33+
#### 激活 :`@EnableEurekaClient` 或者 `@EnableDiscoveryClient`
34+
35+
```java
36+
package com.segumentfault.springcloudlesson5eurekaclient;
37+
38+
import org.springframework.boot.SpringApplication;
39+
import org.springframework.boot.autoconfigure.SpringBootApplication;
40+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
41+
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
42+
43+
@SpringBootApplication
44+
@EnableDiscoveryClient
45+
//@EnableEurekaClient
46+
public class SpringCloudLesson5EurekaClientApplication {
47+
48+
public static void main(String[] args) {
49+
SpringApplication.run(SpringCloudLesson5EurekaClientApplication.class, args);
50+
}
51+
}
52+
```
53+
54+
55+
56+
57+
58+
### Eureka 服务器
59+
60+
61+
62+
#### 配置高可用 Eureka 服务器
63+
64+
65+
66+
##### 设置公用 Eureka 服务器配置
67+
68+
`application.properties`:
69+
70+
```properties
71+
## 定义 应用名称
72+
spring.applicaiton.name = spring-cloud-eureka-server
73+
74+
## 管理端安全失效
75+
management.security.enabled = false
76+
77+
## 公用 Eureka 配置
78+
### 向注册中心注册
79+
eureka.client.register-with-eureka = true
80+
### 向获取注册信息(服务、实例信息)
81+
eureka.client.fetch-registry = true
82+
```
83+
84+
85+
86+
##### 配置 Peer 1 Eureka 服务器
87+
88+
`application-peer1.properties`:(单机情况相当于 profile = "peer1")
89+
90+
```properties
91+
92+
# peer 1 完整配置
93+
94+
## 配置 服务器端口
95+
## peer 1 端口 9090
96+
server.port = 9090
97+
98+
## peer 2 主机:localhost , 端口 9091
99+
peer2.server.host = localhost
100+
peer2.server.port = 9091
101+
102+
# Eureka 注册信息
103+
eureka.client.serviceUrl.defaultZone = http://${peer2.server.host}:${peer2.server.port}/eureka
104+
```
105+
106+
107+
108+
##### 启动 Peer 1 Eureka 服务器
109+
110+
通过启动参数 `—spring.profiles.active=peer1` ,相当于读取了 `application-peer1.properties ``application.properties`
111+
112+
113+
114+
##### 配置 Peer 2 Eureka 服务器
115+
116+
`application-peer2.properties`:(单机情况相当于 profile = "peer2")
117+
118+
```properties
119+
# peer 2 完整配置
120+
121+
## 配置 服务器端口
122+
## peer 2 端口 9091
123+
server.port = 9091
124+
125+
## peer 1 主机:localhost , 端口 9090
126+
peer1.server.host = localhost
127+
peer1.server.port = 9090
128+
129+
# Eureka 注册信息
130+
eureka.client.serviceUrl.defaultZone = http://${peer1.server.host}:${peer1.server.port}/eureka
131+
```
132+
133+
134+
135+
##### 启动 Peer 2 Eureka 服务器
136+
137+
通过启动参数 `—spring.profiles.active=peer2` ,相当于读取了 `application-peer2.properties ``application.properties`
138+
139+
140+
141+
### Spring Cloud Consul
142+
143+
144+
145+
#### 引入依赖
146+
147+
```xml
148+
<dependency>
149+
<groupId>org.springframework.cloud</groupId>
150+
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
151+
</dependency>
152+
```
153+
154+
155+
156+
#### 激活服务发现客户端
157+
158+
```java
159+
package com.segumentfault.springcloudlesson5consulclient;
160+
161+
import org.springframework.boot.SpringApplication;
162+
import org.springframework.boot.autoconfigure.SpringBootApplication;
163+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
164+
165+
@SpringBootApplication
166+
@EnableDiscoveryClient
167+
public class SpringCloudLesson5ConsulClientApplication {
168+
169+
public static void main(String[] args) {
170+
SpringApplication.run(SpringCloudLesson5ConsulClientApplication.class, args);
171+
}
172+
}
173+
```
174+
175+
176+
177+
#### 利用服务发现API 操作
178+
179+
##### 配置应用信息
180+
181+
```properties
182+
## 应用名称
183+
spring.application.name = spring-cloud-consul
184+
185+
## 服务端口
186+
server.port = 8080
187+
188+
## 管理安全失效
189+
management.security.enabled = false
190+
191+
## 连接 Consul 服务器的配置
192+
### Consul 主机地址
193+
spring.cloud.consul.host = localhost
194+
### Consul 服务端口
195+
spring.cloud.consul.port = 8500
196+
```
197+
198+
199+
200+
##### 编写 `DiscoveryClient` Controller
201+
202+
```java
203+
package com.segumentfault.springcloudlesson5consulclient.web.controller;
204+
205+
import org.springframework.beans.factory.annotation.Autowired;
206+
import org.springframework.beans.factory.annotation.Value;
207+
import org.springframework.cloud.client.ServiceInstance;
208+
import org.springframework.cloud.client.discovery.DiscoveryClient;
209+
import org.springframework.web.bind.annotation.GetMapping;
210+
import org.springframework.web.bind.annotation.RestController;
211+
212+
import java.util.LinkedList;
213+
import java.util.List;
214+
215+
/**
216+
* {@link DiscoveryClient} {@link RestController}
217+
*
218+
* @author <a href="mailto:[email protected]">Mercy</a>
219+
* @since 1.0.0
220+
*/
221+
@RestController
222+
public class DiscoveryClientController {
223+
224+
225+
private final DiscoveryClient discoveryClient;
226+
227+
private final String currentApplicationName;
228+
229+
@Autowired
230+
public DiscoveryClientController(DiscoveryClient discoveryClient,
231+
@Value("${spring.application.name}") String currentApplicationName) {
232+
this.discoveryClient = discoveryClient;
233+
this.currentApplicationName = currentApplicationName;
234+
}
235+
236+
/**
237+
* 获取当前应用信息
238+
*
239+
* @return
240+
*/
241+
@GetMapping("/current/service-instance")
242+
public ServiceInstance getCurrentServiceInstance() {
243+
// return discoveryClient.getInstances(currentApplicationName).get(0);
244+
return discoveryClient.getLocalServiceInstance();
245+
246+
}
247+
248+
249+
/**
250+
* 获取所有的服务名
251+
*
252+
* @return
253+
*/
254+
@GetMapping("/list/services")
255+
public List<String> listServices() {
256+
return discoveryClient.getServices();
257+
}
258+
259+
/**
260+
* 获取所有的服务实例信息
261+
*
262+
* @return
263+
*/
264+
@GetMapping("/list/service-instances")
265+
public List<ServiceInstance> listServiceInstances() {
266+
List<String> services = listServices();
267+
List<ServiceInstance> serviceInstances = new LinkedList<>();
268+
269+
services.forEach(serviceName -> {
270+
serviceInstances.addAll(discoveryClient.getInstances(serviceName));
271+
});
272+
273+
return serviceInstances;
274+
}
275+
}
276+
```
277+
278+
279+
280+
281+
Binary file not shown.
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/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.segumentfault</groupId>
7+
<artifactId>spring-cloud-lesson-5-consul-client</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-cloud-lesson-5-consul-client</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.8.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-actuator</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.cloud</groupId>
35+
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-web</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<dependencyManagement>
50+
<dependencies>
51+
<dependency>
52+
<groupId>org.springframework.cloud</groupId>
53+
<artifactId>spring-cloud-dependencies</artifactId>
54+
<version>${spring-cloud.version}</version>
55+
<type>pom</type>
56+
<scope>import</scope>
57+
</dependency>
58+
</dependencies>
59+
</dependencyManagement>
60+
61+
<build>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-maven-plugin</artifactId>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
70+
71+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.segumentfault.springcloudlesson5consulclient;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
7+
@SpringBootApplication
8+
@EnableDiscoveryClient
9+
public class SpringCloudLesson5ConsulClientApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringCloudLesson5ConsulClientApplication.class, args);
13+
}
14+
}

0 commit comments

Comments
 (0)