Skip to content

Commit 33b9a82

Browse files
committed
1. 重新划分章节和目录
2. 部分演示代码更新 3. 完善部分介绍
1 parent 62be57b commit 33b9a82

40 files changed

+130
-91
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 1 微服务简介
2+
3+
## 什么是微服务架构
4+
5+
近年来,在软件开发领域关于微服务的讨论呈现出火爆的局面,有人倾向于在系统设计与开发中采用微服务方式实现软件系统的松耦合、跨部门开发,被认为是IT软件架构的未来方向,Martin Fowler也给微服务架构极高的评价;同时,反对之声也很强烈,持反对观点的人表示微服务增加了系统维护、部署的难度,导致一些功能模块或代码无法复用,同时微服务允许使用不同的语言和框架来开发各个系统模块,这又会增加系统集成与测试的难度,而且随着系统规模的日渐增长,微服务在一定程度上也会导致系统变得越来越复杂。尽管一些公司已经在生产系统中采用了微服务架构,并且取得了良好的效果;但更多公司还是处在观望的态度。
6+
7+
什么是微服务架构呢?简单说就是将一个完整的应用(单体应用)按照一定的拆分规则(后文讲述)拆分成多个不同的服务,每个服务都能独立地进行开发、部署、扩展。服务于服务之间通过注入RESTful api或其他方式调用。大家可以搜索到很多相关介绍和文章。本文暂不细表。在此推荐两个比较好的博客:
8+
9+
>[http://microservices.io/](http://microservices.io/)
10+
>[http://martinfowler.com/articles/microservices.html](http://martinfowler.com/articles/microservices.html)
11+

chapter1/1 序言.md 2 Spring Cloud/2 Spring Cloud.md

+38-23
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
# 序言
2-
3-
## 什么是微服务架构
4-
5-
近年来,在软件开发领域关于微服务的讨论呈现出火爆的局面,有人倾向于在系统设计与开发中采用微服务方式实现软件系统的松耦合、跨部门开发;同时,反对之声也很强烈,持反对观点的人表示微服务增加了系统维护、部署的难度,导致一些功能模块或代码无法复用,同时微服务允许使用不同的语言和框架来开发各个系统模块,这又会增加系统集成与测试的难度,而且随着系统规模的日渐增长,微服务在一定程度上也会导致系统变得越来越复杂。尽管一些公司已经在生产系统中采用了微服务架构,并且取得了良好的效果;但更多公司还是处在观望的态度。
6-
7-
什么是微服务架构呢?简单说就是将一个完整的应用(单体应用)按照一定的拆分规则(后文讲述)拆分成多个不同的服务,每个服务都能独立地进行开发、部署、扩展。服务于服务之间通过注入RESTful api或其他方式调用。大家可以搜索到很多相关介绍和文章。本文暂不细表。
8-
9-
在此推荐两个比较好的博客:
10-
11-
>[http://microservices.io/](http://microservices.io/)
12-
>[http://martinfowler.com/articles/microservices.html](http://martinfowler.com/articles/microservices.html)
13-
14-
1+
# 2 Spring Cloud
152

163
## Spring Cloud 简介
174

18-
Spring Cloud是在Spring Boot的基础上构建的,为开发人员提供快速建立分布式系统中的一些常见的模式
5+
Spring Cloud是在Spring Boot的基础上构建的,用于简化分布式系统构建的工具集,为开发人员提供快速建立分布式系统中的一些常见的模式
196

207
> 例如:配置管理(configuration management),服务发现(service discovery),断路器(circuit breakers),智能路由( intelligent routing),微代理(micro-proxy),控制总线(control bus),一次性令牌( one-time tokens),全局锁(global locks),领导选举(leadership election),分布式会话(distributed sessions),集群状态(cluster state)。
218
@@ -31,15 +18,31 @@ Spring Cloud 项目主页:[http://projects.spring.io/spring-cloud/](http://pro
3118

3219

3320

34-
## 准备
21+
## 准备工作
22+
23+
24+
技术储备:
25+
26+
| 所需技能 | 备注 |
27+
| ----------- | ------------------------------- |
28+
| Java | |
29+
| Maven | 文章涉及到大量的代码,均使用Maven构建 |
30+
| Spring Boot | Spring Cloud是在Spring Boot基础上构建的 |
3531

3632
环境准备:
3733

38-
| 工具 | 版本或描述 |
39-
| ----- | -------------------- |
40-
| JDK | 1.8 |
41-
| IDE | STS 或者 IntelliJ IDEA |
42-
| Maven | 3.x |
34+
| 工具 | 版本或描述 |
35+
| ----- | -------------------------------- |
36+
| JDK | 1.8 |
37+
| IDE | STS 或者 IntelliJ IDEA,本教程使用的是STS. |
38+
| Maven | 3.x |
39+
40+
本课程所使用的软件及版本:
41+
42+
| 使用到的软件 | 版本号 | 是否最新版本 |
43+
| ------------ | ------------- | ------ |
44+
| Spring Boot | 1.4.0.RELEASE ||
45+
| Spring Cloud | Brixton.SR5 ||
4346

4447
主机名配置:
4548

@@ -67,7 +70,9 @@ Spring Cloud 项目主页:[http://projects.spring.io/spring-cloud/](http://pro
6770

6871
Spring Cloud所有的配置项:
6972

70-
> [http://cloud.spring.io/spring-cloud-static/Brixton.SR5/#_appendix_compendium_of_configuration_properties](http://cloud.spring.io/spring-cloud-static/Brixton.SR5/#_appendix_compendium_of_configuration_properties)
73+
[http://cloud.spring.io/spring-cloud-static/Brixton.SR5/#_appendix_compendium_of_configuration_properties](http://cloud.spring.io/spring-cloud-static/Brixton.SR5/#_appendix_compendium_of_configuration_properties)
74+
75+
父项目的建立
7176

7277
在进入主题之前,我们首先创建一个父项目(spring-cloud-microservice-study),这样可以对项目中的Maven依赖进行统一的管理。
7378

@@ -92,6 +97,9 @@ Spring Cloud所有的配置项:
9297
<module>microservice-hystrix-dashboard</module>
9398
<module>microservice-consumer-movie-feign-with-hystrix-stream</module>
9499
<module>microservice-hystrix-turbine</module>
100+
<module>microservice-config-server</module>
101+
<module>microservice-config-client</module>
102+
<module>microservice-api-gateway</module>
95103
</modules>
96104

97105
<!-- 使用最新的spring-boot版本 -->
@@ -111,7 +119,7 @@ Spring Cloud所有的配置项:
111119
<dependency>
112120
<groupId>org.springframework.cloud</groupId>
113121
<artifactId>spring-cloud-dependencies</artifactId>
114-
<version>Brixton.SR4</version>
122+
<version>Brixton.SR5</version>
115123
<type>pom</type>
116124
<scope>import</scope>
117125
</dependency>
@@ -128,3 +136,10 @@ Spring Cloud所有的配置项:
128136
</build>
129137
</project>
130138
```
139+
140+
141+
142+
## TIPS
143+
144+
1. 笔者讲解采用最新的Spring Boot 和Spring Cloud进行讲解,其中可能涉及到部分新特性,笔者尽量指出,同时笔者能力有限,如有理解不到位的地方,还请各位看客指出,定在第一时间进行修正。
145+
2. Spring Cloud版本并不是传统的使用数字的方式标识,而是使用例如:Angel、Brixton、Camden...等等伦敦的地名来命名版本,版本的先后顺序大家可能已经猜到了,就是使用字母表的先后来标识的。笔者在咨询过Spring Cloud的主要贡献者之一Josh Long之后已经确认。

chapter2/2 服务发现.md 2 Spring Cloud/2.1 服务发现.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# 2.1 服务发现
2+
13
## 关于服务发现
24

35
在微服务架构中,服务发现(Service Discovery)是关键原则之一。手动配置每个客户端或某种形式的约定是很难做的,并且很脆弱。Spring Cloud提供了多种服务发现的实现方式,例如:Eureka、Consul、Zookeeper。本文暂时只讲述基于Eureka的服务发现。后续会补上基于Consul和Zookeeper的服务发现。
@@ -74,7 +76,7 @@ eureka:
7476

7577

7678

77-
## 代码地址(任选其一)
79+
## 代码地址(任选其一)
7880

7981
> [http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-discovery-eureka](http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-discovery-eureka)
8082
> [https://github.com/eacdy/spring-cloud-study/tree/master/microservice-discovery-eureka](https://github.com/eacdy/spring-cloud-study/tree/master/microservice-discovery-eureka)

chapter3/3 服务提供者.md 2 Spring Cloud/2.2 服务提供者.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# 2.2 服务提供者
2+
13
## 服务提供者和服务消费者
24

35
下面这张表格,简单描述了服务提供者/消费者是什么:
@@ -214,7 +216,7 @@ public class UserApplication {
214216

215217

216218

217-
## 代码地址(任选其一)
219+
## 代码地址(任选其一)
218220

219221
>[http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-provider-user](http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-provider-user)
220222
>[https://github.com/eacdy/spring-cloud-study/tree/master/microservice-provider-user](https://github.com/eacdy/spring-cloud-study/tree/master/microservice-provider-user)

chapter4/4 服务消费者.md 2 Spring Cloud/2.3 服务消费者.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# 2.3 服务消费者
2+
13
上文我们创建了注册中心,以及服务的提供者microservice-provider-user,并成功地将服务提供者注册到了注册中心上。
24

35
要想消费microservice-provider-user的服务是很简单的,我们只需要使用RestTemplate即可,或者例如HttpClient之类的http工具也是可以的。但是在集群环境下,我们必然是每个服务部署多个实例,那么服务消费者消费服务提供者时的负载均衡又要如何做呢?

chapter4/4.1 Ribbon.md 2 Spring Cloud/2.3.1 Ribbon.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# 第1节:Ribbon
1+
# 2.3.1 Ribbon
22

3-
### Ribbon介绍
3+
4+
5+
## Ribbon介绍
46

57
Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随即连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。简单地说,Ribbon是一个客户端负载均衡器。
68

79

810

9-
### Ribbon代码示例
11+
## Ribbon代码示例
1012

1113
创建一个Maven项目,并在pom.xml中加入如下内容:
1214

chapter4/4.2 Feign.md 2 Spring Cloud/2.3.2 Feign.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
# 第2节:Feign
1+
# 2.3.2. Feign
22

3-
### Feign介绍
3+
4+
## Feign介绍
45

56
Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。创建接口,为接口添加注解,即可使用Feign。Feign可以使用Feign注解或者JAX-RS注解,还支持热插拔的编码器和解码器。Spring Cloud为Feign添加了Spring MVC的注解支持,并整合了Ribbon和Eureka来为使用Feign时提供负载均衡。
67

78
> 翻译自:[http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#spring-cloud-feign](http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#spring-cloud-feign)
89
910

1011

11-
### Feign示例
12+
## Feign示例
1213

1314
创建一个Maven项目,并在pom.xml添加如下内容:
1415

chapter5/5 熔断器.md 2 Spring Cloud/2.4 熔断器.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 第5章:熔断器
1+
# 2.4 熔断器
22

33

44

chapter5/5.1 Hystrix.md 2 Spring Cloud/2.4.1 Hystrix.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 第1节:Hystrix
1+
# 2.4.1. Hystrix
22

33

44

chapter5/5.2 Hystrix Dashboard.md 2 Spring Cloud/2.4.2 Hystrix Dashboard.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 第2节:Hystrix Dashboard
1+
# 2.4.2. Hystrix Dashboard
22

33

44

@@ -10,7 +10,7 @@
1010

1111

1212

13-
### 测试步骤
13+
## 测试步骤
1414

1515
1. 启动:microservice-discovery-eureka
1616

@@ -30,7 +30,7 @@
3030

3131

3232

33-
### Hystrix Dashboard
33+
## Hystrix Dashboard
3434

3535
Hystrix Dashboard可以可视化查看实时监控数据。我们可以下载hystrix-dashboard的war包部署到诸如Tomcat之类的容器就,本文不做赘述另外Spring Cloud也提供了Hystrix Dashboard的整合,下面我们看看Spring Cloud是怎么玩转Hystrix Dashboard的。
3636

@@ -109,15 +109,15 @@ server:
109109
110110
111111
112-
### 代码地址(任选其一):
112+
## 代码地址(任选其一):
113113
114114
>[http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-hystrix-dashboard](http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-hystrix-dashboard)
115115
>
116116
>[https://github.com/eacdy/spring-cloud-study/tree/master/microservice-hystrix-dashboard](https://github.com/eacdy/spring-cloud-study/tree/master/microservice-hystrix-dashboard)
117117
118118
119119
120-
### TIPS
120+
## TIPS
121121
122122
我们启动前文的microservice-consumer-movie-feign-with-hystrix项目后发现其访问[localhost:8021/hystrix.stream](localhost:8021/hystrix.stream) ,是404,查看pom.xml依赖树,发现其没有依赖hystrix-metrics-event-stream项目。故而添加依赖:
123123

chapter5/5.3 Turbine.md 2 Spring Cloud/2.4.3 Turbine.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
# 第3节:Turbine
1+
# 2.4.3. Turbine
22

3-
### Turbine
3+
4+
5+
## Turbine
46

57
在复杂的分布式系统中,相同服务的结点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix提供了一个开源项目(Turbine)来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。 
68

79
和Hystrix Dashboard一样,Turbine也可以下载war包部署到Web容器,本文不做赘述。下面讨论Spring Cloud是怎么使用Turbine的。
810

911

1012

11-
### 编码
13+
## 代码示例
1214

1315
新建Maven项目,并在pom.xml中添加如下内容:
1416

@@ -90,7 +92,7 @@ eureka:
9092

9193

9294

93-
### Turbine测试
95+
## Turbine测试
9496

9597
1. 启动项目:microservice-discovery-eureka
9698
2. 启动项目:microservice-provider-user
@@ -108,7 +110,7 @@ data: {"rollingCountFallbackSuccess":0,"rollingCountFallbackFailure":0,"property
108110

109111
并且会不断刷新以获取实时的监控数据。同样的,我们可以将这些文本内容放入到Dashboard中展示。
110112

111-
10000. 访问Hystrix Dashboard:[http://localhost:8030/hystrix.stream](http://localhost:8030/hystrix.stream),并将[http://localhost:8031/turbine.stream](http://localhost:8031/turbine.stream)输入到其上的输入框,并随意指定一个Title,如下图:
113+
10000000000000000. 访问Hystrix Dashboard:[http://localhost:8030/hystrix.stream](http://localhost:8030/hystrix.stream),并将[http://localhost:8031/turbine.stream](http://localhost:8031/turbine.stream)输入到其上的输入框,并随意指定一个Title,如下图:
112114

113115
![turbine-index](images/turbine-01.png)
114116

@@ -118,7 +120,7 @@ data: {"rollingCountFallbackSuccess":0,"rollingCountFallbackFailure":0,"property
118120

119121

120122

121-
### TIPS
123+
## TIPS
122124

123125
1. 项目microservice-consumer-movie-ribbon-with-hystrix 和 microservice-consumer-movie-feign-with-hystrix-stream 需要配置不同的主机名,并将preferIpAddress设为false或者不设置,否则将会造成在单个主机上测试,Turbine只显示一个图表的情况。项目的代码已经修改好并注释了,这边友谊提示一下。
124126

@@ -134,7 +136,7 @@ data: {"rollingCountFallbackSuccess":0,"rollingCountFallbackFailure":0,"property
134136

135137

136138

137-
### 参考文档
139+
## 参考文档
138140

139141
> [http://blog.csdn.net/liaokailin/article/details/51344281](http://blog.csdn.net/liaokailin/article/details/51344281)
140142
>

chapter6/6 配置中心.md 2 Spring Cloud/2.5 配置中心.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 第6章:配置中心
1+
# 2.5 配置中心
22

33
Spring Cloud Config提供了一种在分布式系统中外部化配置服务器和客户端的支持。配置服务器有一个中心位置,管理所有环境下的应用的外部属性。客户端和服务器映射到相同Spring Eventment 和 PropertySrouce抽象的概念,所以非常适合Spring应用,但也可以在任何语言开发的任何应用中使用。在一个应用从开发、测试到生产的过程中,你可以分别地管理开发、测试、生产环境的配置,并且在迁移的时候获取相应的配置来运行。
44

@@ -14,7 +14,7 @@ Config Server 存储后端默认使用git存储配置信息,因此可以很容
1414
1515

1616

17-
### 准备工作
17+
## 准备工作
1818

1919
准备几个配置文件,命名规范为`项目名称-环境名称.properties`,本文在git仓库:[https://github.com/eacdy/spring-cloud-study/](https://github.com/eacdy/spring-cloud-study/)中,新建目录config-repo,创建以下几个文件:
2020

@@ -26,7 +26,7 @@ microservice-config-client.properties
2626

2727

2828

29-
### Config Server 示例
29+
## Config Server 示例
3030

3131
创建一个Maven项目,在pom.xml文件中添加如下内容:
3232

@@ -105,7 +105,7 @@ spring:
105105
106106
107107
108-
### 测试工作
108+
## 测试工作
109109
110110
| 获取git上的资源信息遵循如下规则 |
111111
| ---------------------------------------- |
@@ -121,13 +121,13 @@ spring:
121121
122122
123123
124-
### 微服务如何获取配置信息
124+
## 微服务如何获取配置信息
125125
126126
我们成功搭建了Config Server,并测试能够正常获取到git仓库中的配置信息。那么对于一个微服务应用,如何才能获取配置信息呢?
127127
128128
129129
130-
#### DEMO
130+
### 配置服务客户端示例
131131
132132
新建一个Maven项目,在pom.xml中添加如下内容:
133133
@@ -222,7 +222,7 @@ spring:
222222
223223
224224
225-
#### 配置的热加载
225+
### 配置的热加载
226226
227227
如果不重启应用,能够做到配置的刷新吗?答案显然是可以的。
228228
@@ -250,7 +250,7 @@ abcd
250250

251251

252252

253-
#### 注意点:
253+
## 注意点:
254254

255255
1. client需要添加以下依赖,否则访问/refresh将会得到404:
256256

0 commit comments

Comments
 (0)