Spring Cloud 介绍
官方网址 http://projects.spring.io/spring-cloud/
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state).
Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer's own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.
Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为开发分布式应用提供了一些列工具,例如配置管理、服务发现、断路器、智能路由、微代理、控制总线、one-time tokens、全局锁、决策竞选、分布式会话和集群状态管理等。
Spring Cloud专注于提供良好的开箱即用的特性,包括:
- 分布式/版本化配置 Distributed/versioned configuration
- 服务注册和发现 Service registration and discovery
- 路由 Routing
- Service-to-service 调用 Service-to-service calls
- 负载均衡 Load balancing
- 断路器 Circuit Breakers
- 全局锁 Global locks
- 决策竞选和集群状态管理 Leadership election and cluster state
- 分布式消息传递 Distributed messaging
Spring Cloud 的子项目
Spring Cloud Config
Centralized external configuration management backed by a git repository. The configuration resources map directly to SpringEnvironment
but could be used by non-Spring applications if desired.Spring Cloud Netflix
Integration with various Netflix OSS components (Eureka, Hystrix, Zuul, Archaius, etc.).Spring Cloud Bus
An event bus for linking services and service instances together with distributed messaging. Useful for propagating state changes across a cluster (e.g. config change events).Spring Cloud for Cloud Foundry
Integrates your application with Pivotal Cloud Foundry. Provides a service discovery implementation and also makes it easy to implement SSO and OAuth2 protected resources.Spring Cloud Open Service Broker
Provides a starting point for building a service broker that implements the Open Service Broker API.Spring Cloud Cluster
Leadership election and common stateful patterns with an abstraction and implementation for Zookeeper, Redis, Hazelcast, Consul.Spring Cloud Consul
Service discovery and configuration management with Hashicorp Consul.Spring Cloud Security
Provides support for load-balanced OAuth2 rest client and authentication header relays in a Zuul proxy.Spring Cloud Sleuth
Distributed tracing for Spring Cloud applications, compatible with Zipkin, HTrace and log-based (e.g. ELK) tracing.Spring Cloud Data Flow
A cloud-native orchestration service for composable microservice applications on modern runtimes. Easy-to-use DSL, drag-and-drop GUI, and REST-APIs together simplifies the overall orchestration of microservice based data pipelines.Spring Cloud Stream
A lightweight event-driven microservices framework to quickly build applications that can connect to external systems. Simple declarative model to send and receive messages using Apache Kafka or RabbitMQ between Spring Boot apps.Spring Cloud Stream App Starters
Spring Cloud Stream App Starters are Spring Boot based Spring Integration applications that provide integration with external systems.Spring Cloud Task
A short-lived microservices framework to quickly build applications that perform finite amounts of data processing. Simple declarative for adding both functional and non-functional features to Spring Boot apps.Spring Cloud Task App Starters
Spring Cloud Task App Starters are Spring Boot applications that may be any process including Spring Batch jobs that do not run forever, and they end/stop after a finite period of data processing.Spring Cloud Zookeeper
Service discovery and configuration management with Apache Zookeeper.Spring Cloud for Amazon Web Services
Easy integration with hosted Amazon Web Services. It offers a convenient way to interact with AWS provided services using well-known Spring idioms and APIs, such as the messaging or caching API. Developers can build their application around the hosted services without having to care about infrastructure or maintenance.Spring Cloud Connectors
Makes it easy for PaaS applications in a variety of platforms to connect to backend services like databases and message brokers (the project formerly known as "Spring Cloud").Spring Cloud Starters
Spring Boot-style starter projects to ease dependency management for consumers of Spring Cloud. (Discontinued as a project and merged with the other projects after Angel.SR2.)Spring Cloud CLI
Spring Boot CLI plugin for creating Spring Cloud component applications quickly in GroovySpring Cloud Contract
Spring Cloud Contract is an umbrella project holding solutions that help users in successfully implementing the Consumer Driven Contracts approach.Spring Cloud Gateway
Spring Cloud Gateway is an intelligent and programmable router based on Project Reactor.Spring Cloud OpenFeign
Spring Cloud OpenFeign provides integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms.Spring Cloud Pipelines
Spring Cloud Pipelines provides an opinionated deployment pipeline with steps to ensure that your application can be deployed in zero downtime fashion and easilly rolled back of something goes wrong.
Spring Cloud 的版本进化
按字母升序排列,例如 Angel
是第一版,Brixton
是第二版。
注意:
- Finchley 版本基于 Spring Boot 2.0.x, 不支持 Spring Boot 1.5.x.
- Dalston 和 Edgware 版本基于 Spring Boot 1.5.x, 不支持 Spring Boot 2.0.x.
- Camden 版本基于 Spring Boot 1.4.x, 不支持 1.5.x.
- Brixton 和 Angel 版本已结束生命
在接下来的示例中,我们将使用 Finchley 版本的 Spring Cloud 结合 2.0.x 版本的 Spring Boot。
Finchley 版本相对于之前的版本,一些依赖的名称发生了变化,请参考 https://spring.io/blog/2017/10/31/spring-cloud-finchley-m3-released。
Spring Cloud Netflix
在开始示例之前,先来了解下 Spring Cloud Netflix。
它的主要内容是对 Netflix 公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。
它包含如下的组件和特性:
- 服务发现 Service Discovery: Eureka instances can be registered and clients can discover the instances using Spring-managed beans
- 服务发现 Service Discovery: an embedded Eureka server can be created with declarative Java configuration
- 断路器 Circuit Breaker: Hystrix clients can be built with a simple annotation-driven method decorator
- 断路器 Circuit Breaker: embedded Hystrix dashboard with declarative Java configuration
- 声明式的 REST 客户端 Declarative REST Client: Feign creates a dynamic implementation of an interface decorated with JAX-RS or Spring MVC annotations
- 客户端负载均衡 Client Side Load Balancer: Ribbon
- 外部配置 External Configuration: a bridge from the Spring Environment to Archaius (enables native configuration of Netflix components using Spring Boot conventions)
- 路由和过滤 Router and Filter: automatic regsitration of Zuul filters, and a simple convention over configuration approach to reverse proxy creation
通过 Eureka 实现服务注册发现
在这里我们打算建立两个项目:
-
eureka-server
:服务注册中心 -
eureka-client
:服务提供者
eureka-server 服务注册中心
可以通过如下的 Spring Assistant
插件来创建项目,添加 Eureka Server
作为依赖。
从 pom.xml
中可以看出,导入了如下的依赖:
- 2.0.3.RELEASE 版本的 Spring Boot
- Finchley.RELEASE 版本的 Spring Cloud
spring-cloud-starter-netflix-eureka-server
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
随后在启动程序中通过 @EnableEurekaServer
注解启动一个服务注册中心提供给其他应用进行对话:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
随后在 application.properties
:将服务注册中心端口设置为 1234。在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为。
spring.application.name=eureka-server
server.port=1234
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
最后通过 mvn spring-boot:run
命令启动项目,启动完成后,可以通过 http://127.0.0.1:1234 查看服务的注册情况,可以看到目前暂无服务注册到 Eureka Server。
eureka-client 服务提供者
可以通过如下的 Spring Assistant
插件来创建项目,添加 Eureka Discovery
和 Web
作为依赖。
从 pom.xml
中可以看出,导入了如下的依赖:
- 2.0.3.RELEASE 版本的 Spring Boot
- Finchley.RELEASE 版本的 Spring Cloud
spring-cloud-starter-netflix-eureka-client
spring-boot-starter-web
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
随后在启动程序中通过 @EnableDiscoveryClient
来激活 Eureka 中的 DiscoveryClient 实现:
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
随后在 application.properties
:通过 spring.application.name
属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。eureka.client.serviceUrl.defaultZone
属性对应服务注册中心的配置内容,指定服务注册中心的位置。
spring.application.name=eureka-client
server.port=2001
eureka.client.serviceUrl.defaultZone=http://localhost:1234/eureka/
随后,我们创建一个 CalculatorController
类来提供加法服务:
@RestController
public class CalculatorController {
@GetMapping("/add")
public Integer add(@RequestParam Integer operand1, @RequestParam Integer operand2) {
return operand1 + operand2;
}
}
最后通过 mvn spring-boot:run
命令启动项目,启动完成后,可以通过 http://127.0.0.1:1234 查看服务的注册情况,可以看到目前有一个服务注册到 Eureka Server。
通过 http://10.93.67.135:2001/add 可以调用该加法服务:
如果我们将端口修改为 2002,重新打开一个 terminal 启动项目,可以看到该服务对应了两个实例 Zones。
通过 Consul 实现服务注册发现
Spring Cloud Consul项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,它包含多个组件,但是作为一个整体,在微服务架构中为我们的基础设施提供服务发现和服务配置的工具。
此处不详细介绍,参见 Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
引用:
程序猿DD Spring Cloud基础教程
Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
Spring Cloud Dalston中文文档