Spring Cloud
大概两年前第一次接触到Spring Cloud,也是第一次开始尝试去对自己负责的项目进行拆分。去理解服务的职责,系统内每个模块的职责。入门已经两年过去了,Spring cloud已经变化了很多,尤其最近开始准备将Spring Boot 1.x 升级到Spring Boot 2.x 的时候,发现和之前相比很多参数和注解的调整范围还是相当多的。
所以在进行版本升级的时候,将这些比较基础的内容整理出来,即是加深自己的印象,也是能够和更多的同学一起学习交流。
Spring Cloud的组件
我们一般说起Spring Cloud的核心组件包含下面内容
- 服务发现(Eureka、Consul)
- 熔断器 (Hystrix)
- 负载均衡(Ribbon)
- 路由网关(Zuul)
- 声明式调用(Feign)
- 数据监控(Turbine)
- 配置中心(Config)
- 数据链路追踪(Sleuth-zipkin)
- 消息队列组件(Stream)
入门的内容
在这两年接触中深深明白,微服务是一个易学难精的知识体系。微服务中每个组件往深了往专业方面扩展都是一个很深的领域。而之所以写写入门的东西,主要是个人经验还是太少精通的地方实在太少,在一些更加专业的内容很可能说一些错误的东西,误导各位同学。另外一方面当然也是时间太少,虽然每天晚上都抽出一两个小时去调试代码但是还是觉得时间远远不够。不过通过这次整理也发现自己在专业的地方欠缺还是太多,后续会尝试针对每个组件进行更加深入的整理。
目录
Spring Cloud 目前整理的目录大概是这样,目前项目已经全部完成,文档内容只记录了一些当时操作的简单流程。
- 服务发现
- 熔断器
- Ribbon 负载均衡
- 路由网关
- 声明式调用
- Turbine 集群数据监控
- 配置中心
- Sleuth - Zipkin 实现微服务链路跟踪
- stream 消息驱动的分布式
- consul 服务发现中心
版本信息
spring boot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
spring cloud版本
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
这里说一句,Spring Cloud的版本和Spring Boot一定要对应。目前因为每个版本对注释和参数都有调整,假如版本不对应很可能出现错误的配置或者不存在的配置
官方的版本限制
Release Train | Boot Version |
---|---|
Greenwich | 2.1.x |
Finchley | 2.0.x |
Edgware | 1.5.x |
Dalston | 1.5.x |
涉及的应用
项目名称 | 内容 | 端口 | 服务名称 | 服务中心端口 |
---|---|---|---|---|
base-config-client | 配置中心客户端 | 8710 | base-config-client | 8000 |
base-config-client-bus | 配置中心客户端使用-bus依赖 | 8716 | base-config-client-bus | 8000 |
base-config-client-encryption | 配置中心客户端使用-参数加密 | 8717 | base-config-client-encryption | 8000 |
base-config-client-start | 配置中心客户端(舍弃application配置) | 8715 | base-config-client-start | 8000 |
base-config-service | 配置中心服务端 | 8700 | base-config-service | 8000 |
base-config-service-bus | 配置中心服务端-bus依赖 | 8701 | base-config-service-bus | 8000 |
base-config-service-encryption | 配置中心服务端-参数加密 | 8703 | base-config-service-encryption | 8000 |
base-consumer | 服务消费方 | 8200 | base-consumer | 8000 |
base-consumer-sleuth | 服务消费方-微服务数据链路追踪 | 8201 | base-consumer-sleuth | 8000 |
base-eureka | 服务发现 | 8000 | base-eureka | 8000 |
base-eureka-cluster | 服务发现集群 | 8001、8002、8003 | base-eureka | 8001、8002、8003 |
base-eureka-security | 服务发现安全模式 | 8004 | base-eureka | 8004 |
base-feign | 声明式调用 | 8453 | base-feign | 8000 |
base-feign-config | 声明式调用-自定义配置 | 8454 | base-feign-config | 8000 |
base-feign-hystrix | 声明式调用支持服务降级 | 8455 | base-feign-hystrix | 8000 |
base-feign-hystrix-factory | 声明式调用支持服务降级factory模式 | 8457 | base-feign-hystrix-factory | 8000 |
base-feign-upload | 声明式调用-提供文件上传 | 8458 | base-feign-upload | 8000 |
base-hystrix | 熔断器 | 8404 | base-hystrix | 8000 |
base-hystrix-cluster | 熔断器集群 | 8405、8406、8407 | base-hystrix-cluster | 8000 |
base-hystrix-dashboard | 熔断器数据监视 | 8408 | base-hystrix-dashboard | 8000 |
base-hystrix-turbine | 熔断器集群数据监视 | 8410 | base-hystrix-turbine | 8000 |
base-producer | 服务提供方 | 8100 | base-producer | 8000 |
base-producer-cluster | 服务提供方集群 | 8101、8102、8103 | base-producer-cluster | 8000 |
base-producer-security | 服务提供方安全模式 | 8104 | base-producer-security | 8004 |
base-producer-sleuth | 服务提供应用-微服务数据链路追踪 | 8106 | base-producer-sleuth | 8000 |
base-producer-upload | 服务提供应用支持上传 | 8105 | base-producer-upload | 8000 |
base-ribbon | 负载均衡 | 8600 | base-ribbon | 8000 |
base-stream-client | stream实现消息队列-客户端 | 8703 | base-stream-client | 8000 |
base-stream-producer | stream实现消息队列-服务端 | 8704 | base-stream-producer | 8000 |
base-zuul | 服务网关 | 8300 | base-zuul | 8000 |
base-zuul-fallback | 服务网关支持服务降级 | 8302 | base-zuul-fallback | 8000 |
base-zuul-upload | 服务网关文件上传限制 | 8301 | base-zuul-upload | 8000 |
extra-consul-client | 使用consul实现服务发现-客户端 | 8201 | extra-consul-client | 8500 |
extra-consul-producer | 使用consul实现服务发现-服务端 | 8200 | extra-consul-producer | 8500 |
项目顶级依赖,上面应用的父级依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--lombok工具-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>