一、Spring Cloud简介

Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等项目。

二、微服务架构

“微服务架构”在这几年非常的火热,以至于关于微服务架构相关的开源产品被反复的提及(比如:netflix、dubbo),Spring Cloud也因Spring社区的强大知名度和影响力也被广大架构师与开发者备受关注。

那么什么是“微服务架构”呢?简单的说,微服务架构就是将一个完整的应用从数据存储开始垂直拆分成多个不同的服务,每个服务都能独立部署、独立维护、独立扩展,服务与服务间通过诸如RESTful API的方式互相调用。
springcloud_arch.png
由于Spring Cloud基于Spring Boot构建,而Spring Cloud Alibaba又基于Spring Cloud Common的规范实现,所以当我们使用Spring Cloud Alibaba来构建微服务应用的时候,需要知道这三者之间的版本关系。

下表整理了目前Spring Cloud Alibaba的版本与Spring Boot、Spring Cloud版本的兼容关系:

Spring Cloud Version Spring Cloud Alibaba Version Spring Boot Version
Spring Cloud Hoxton 2.2.0.RELEASE 2.2.X.RELEASE
Spring Cloud Greenwich 2.1.1.RELEASE 2.1.X.RELEASE
Spring Cloud Finchley 2.0.1.RELEASE 2.0.X.RELEASE
Spring Cloud Edgware 1.5.1.RELEASE 1.5.X.RELEASE

SpringCloud和各子项目版本对应关系:

Component Edgware.SR6 Greenwich.SR2
spring-cloud-bus 1.3.4.RELEASE 2.1.2.RELEASE
spring-cloud-commons 1.3.6.RELEASE 2.1.2.RELEASE
spring-cloud-config 1.4.7.RELEASE 2.1.3.RELEASE
spring-cloud-netflix 1.4.7.RELEASE 2.1.2.RELEASE
spring-cloud-security 1.2.4.RELEASE 2.1.3.RELEASE
spring-cloud-consul 1.3.6.RELEASE 2.1.2.RELEASE
spring-cloud-sleuth 1.3.6.RELEASE 2.1.1.RELEASE
spring-cloud-stream Ditmars.SR5 Fishtown.SR3
spring-cloud-zookeeper 1.2.3.RELEASE 2.1.2.RELEASE
spring-boot 1.5.21.RELEASE 2.1.5.RELEASE
spring-cloud-task 1.2.4.RELEASE 2.1.2.RELEASE
spring-cloud-gateway 1.0.3.RELEASE 2.1.2.RELEASE
spring-cloud-openfeign 暂无 2.1.2.RELEASE

三、SpringCloud子项目简介

  • Spring Cloud Config 集中配置管理工具,分布式系统中统一的外部配置管理,默认使用Git来存储配置,可以支持客户端配置的刷新及加密、解密操作。
  • Spring Cloud Netflix Netflix OSS 开源组件集成,包括Eureka、Hystrix、Ribbon、Feign、Zuul等核心组件。
    • Eureka 服务治理组件,包括服务端的注册中心和客户端的服务发现机制;
    • Ribbon 负载均衡的服务调用组件,具有多种负载均衡调用策略;
    • Hystrix 服务容错组件,实现了断路器模式,为依赖服务的出错和延迟提供了容错能力;
    • Feign 基于Ribbon和Hystrix的声明式服务调用组件;
    • Zuul API网关组件,对请求提供路由及过滤功能。
  • Spring Cloud Bus 用于传播集群状态变化的消息总线,使用轻量级消息代理链接分布式系统中的节点,可以用来动态刷新集群中的服务配置。
  • Spring Cloud Consul 基于Hashicorp Consul的服务治理组件。
  • Spring Cloud Security 安全工具包,对Zuul代理中的负载均衡OAuth2客户端及登录认证进行支持。
  • Spring Cloud Sleuth SpringCloud应用程序的分布式请求链路跟踪,支持使用Zipkin、HTrace和基于日志(例如ELK)的跟踪。
  • Spring Cloud Stream 轻量级事件驱动微服务框架,可以使用简单的声明式模型来发送及接收消息,主要实现为Apache Kafka及RabbitMQ。
  • Spring Cloud Task 用于快速构建短暂、有限数据处理任务的微服务框架,用于向应用中添加功能性和非功能性的特性。
  • Spring Cloud Zookeeper 基于Apache Zookeeper的服务治理组件。
  • Spring Cloud Gateway API网关组件,对请求提供路由及过滤功能。
  • Spring Cloud OpenFeign 基于Ribbon和Hystrix的声明式服务调用组件,可以动态创建基于Spring MVC注解的接口实现用于服务调用,在SpringCloud 2.0中已经取代Feign成为了一等公民。

四、根依赖

后续的项目,都依赖于spring-boot创建,我们可以先添加一个 pom.xml 把所有的公共依赖都添加进来。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.2.6.RELEASE</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.example</groupId>
  13. <artifactId>spring-cloud-demos</artifactId>
  14. <name>spring-cloud-demos</name>
  15. <version>0.0.1</version>
  16. <packaging>pom</packaging>
  17. <description>Demos for Spring Cloud</description>
  18. <properties>
  19. <maven.compiler.source>15</maven.compiler.source>
  20. <maven.compiler.target>15</maven.compiler.target>
  21. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  22. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  23. <java.version>15</java.version>
  24. <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
  25. </properties>
  26. <modules>
  27. <module>eureka</module>
  28. </modules>
  29. <dependencies>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <scope>test</scope>
  38. <exclusions>
  39. <exclusion>
  40. <groupId>org.junit.vintage</groupId>
  41. <artifactId>junit-vintage-engine</artifactId>
  42. </exclusion>
  43. </exclusions>
  44. </dependency>
  45. </dependencies>
  46. <dependencyManagement>
  47. <dependencies>
  48. <dependency>
  49. <groupId>org.springframework.cloud</groupId>
  50. <artifactId>spring-cloud-dependencies</artifactId>
  51. <version>${spring-cloud.version}</version>
  52. <type>pom</type>
  53. <scope>import</scope>
  54. </dependency>
  55. </dependencies>
  56. </dependencyManagement>
  57. <build>
  58. <plugins>
  59. <plugin>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-maven-plugin</artifactId>
  62. </plugin>
  63. </plugins>
  64. </build>
  65. </project>

最主要是看 dependencyManagement 节点,引入了 spring-cloud-dependencies