Spring Cloud 介紹
官方網(wǎng)址 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實現(xiàn)的云應(yīng)用開發(fā)工具彩库,它為開發(fā)分布式應(yīng)用提供了一些列工具阁猜,例如配置管理麦牺、服務(wù)發(fā)現(xiàn)责语、斷路器棺牧、智能路由、微代理鸟赫、控制總線负芋、one-time tokens、全局鎖调窍、決策競選宝冕、分布式會話和集群狀態(tài)管理等。
Spring Cloud專注于提供良好的開箱即用的特性邓萨,包括:
- 分布式/版本化配置 Distributed/versioned configuration
- 服務(wù)注冊和發(fā)現(xiàn) Service registration and discovery
- 路由 Routing
- Service-to-service 調(diào)用 Service-to-service calls
- 負載均衡 Load balancing
- 斷路器 Circuit Breakers
- 全局鎖 Global locks
- 決策競選和集群狀態(tài)管理 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 版本已結(jié)束生命
在接下來的示例中宝剖,我們將使用 Finchley 版本的 Spring Cloud 結(jié)合 2.0.x 版本的 Spring Boot。
Finchley 版本相對于之前的版本歉甚,一些依賴的名稱發(fā)生了變化诈闺,請參考 https://spring.io/blog/2017/10/31/spring-cloud-finchley-m3-released。
Spring Cloud Netflix
在開始示例之前雅镊,先來了解下 Spring Cloud Netflix。
它的主要內(nèi)容是對 Netflix 公司一系列開源產(chǎn)品的包裝刃滓,它為Spring Boot應(yīng)用提供了自配置的Netflix OSS整合仁烹。通過一些簡單的注解,開發(fā)者就可以快速的在應(yīng)用中配置一下常用模塊并構(gòu)建龐大的分布式系統(tǒng)咧虎。
它包含如下的組件和特性:
- 服務(wù)發(fā)現(xiàn) Service Discovery: Eureka instances can be registered and clients can discover the instances using Spring-managed beans
- 服務(wù)發(fā)現(xiàn) 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 實現(xiàn)服務(wù)注冊發(fā)現(xiàn)
在這里我們打算建立兩個項目:
-
eureka-server
:服務(wù)注冊中心 -
eureka-client
:服務(wù)提供者
eureka-server 服務(wù)注冊中心
可以通過如下的 Spring Assistant
插件來創(chuàng)建項目卓缰,添加 Eureka Server
作為依賴。
從 pom.xml
中可以看出砰诵,導(dǎo)入了如下的依賴:
- 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
注解啟動一個服務(wù)注冊中心提供給其他應(yīng)用進行對話:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
隨后在 application.properties
:將服務(wù)注冊中心端口設(shè)置為 1234征唬。在默認設(shè)置下,該服務(wù)注冊中心也會將自己作為客戶端來嘗試注冊它自己茁彭,所以我們需要禁用它的客戶端注冊行為总寒。
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 查看服務(wù)的注冊情況摄闸,可以看到目前暫無服務(wù)注冊到 Eureka Server。
eureka-client 服務(wù)提供者
可以通過如下的 Spring Assistant
插件來創(chuàng)建項目妹萨,添加 Eureka Discovery
和 Web
作為依賴年枕。
從 pom.xml
中可以看出,導(dǎo)入了如下的依賴:
- 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 實現(xiàn):
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
隨后在 application.properties
:通過 spring.application.name
屬性乎完,我們可以指定微服務(wù)的名稱后續(xù)在調(diào)用的時候只需要使用該名稱就可以進行服務(wù)的訪問熏兄。eureka.client.serviceUrl.defaultZone
屬性對應(yīng)服務(wù)注冊中心的配置內(nèi)容,指定服務(wù)注冊中心的位置树姨。
spring.application.name=eureka-client
server.port=2001
eureka.client.serviceUrl.defaultZone=http://localhost:1234/eureka/
隨后摩桶,我們創(chuàng)建一個 CalculatorController
類來提供加法服務(wù):
@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 查看服務(wù)的注冊情況典格,可以看到目前有一個服務(wù)注冊到 Eureka Server。
通過 http://10.93.67.135:2001/add 可以調(diào)用該加法服務(wù):
如果我們將端口修改為 2002台丛,重新打開一個 terminal 啟動項目耍缴,可以看到該服務(wù)對應(yīng)了兩個實例 Zones。
通過 Consul 實現(xiàn)服務(wù)注冊發(fā)現(xiàn)
Spring Cloud Consul項目是針對Consul的服務(wù)治理實現(xiàn)挽霉。Consul是一個分布式高可用的系統(tǒng)防嗡,它包含多個組件,但是作為一個整體侠坎,在微服務(wù)架構(gòu)中為我們的基礎(chǔ)設(shè)施提供服務(wù)發(fā)現(xiàn)和服務(wù)配置的工具蚁趁。
此處不詳細介紹,參見 Spring Cloud構(gòu)建微服務(wù)架構(gòu):服務(wù)注冊與發(fā)現(xiàn)(Eureka实胸、Consul)【Dalston版】
引用:
程序猿DD Spring Cloud基礎(chǔ)教程
Spring Cloud構(gòu)建微服務(wù)架構(gòu):服務(wù)注冊與發(fā)現(xiàn)(Eureka他嫡、Consul)【Dalston版】
Spring Cloud Dalston中文文檔