數(shù)字化轉(zhuǎn)型之微服務(wù)之 –Spring Cloud

背景:隨著公司業(yè)務(wù)量的飛速發(fā)展,平臺面臨的挑戰(zhàn)已經(jīng)遠(yuǎn)遠(yuǎn)大于業(yè)務(wù),需求量不斷增加,技術(shù)人員數(shù)量增加,面臨的復(fù)雜度也大大增加。在這個背景下,平臺的技術(shù)架構(gòu)也完成了從傳統(tǒng)的單體應(yīng)用到微服務(wù)化的演進(jìn)。


目前大部分公司用的微服務(wù)架構(gòu)比較多的是Spring-Cloud

中文文檔:https://springcloud.cc/

官方文檔:http://projects.spring.io/spring-cloud/#quick-start



Spring Cloud provides tools for developersto quickly build some of the common patterns in distributed systems (e.g.configuration management, service discovery, circuit breakers, intelligentrouting, micro-proxy, control bus, one-time tokens, global locks, leadershipelection, distributed sessions, cluster state). Coordination of distributedsystems leads to boiler plate patterns, and using Spring Cloud developers canquickly stand up services and applications that implement those patterns. Theywill work well in any distributed environment, including the developer's ownlaptop, bare metal data centres, and managed platforms such as Cloud Foundry.

https://spring.io/guides


?

Spring Cloud 基于 Spring Boot,為微服務(wù)體系開發(fā)中的架構(gòu)問題,提供了一整套的解決方案——服務(wù)注冊與發(fā)現(xiàn),服務(wù)消費(fèi),服務(wù)保護(hù)與熔斷,網(wǎng)關(guān),分布式調(diào)用追蹤,分布式配置管理等。


Spring Boot 是 Spring 的一套快速配置腳手架,使用默認(rèn)大于配置的理念,用于快速開發(fā)單個微服務(wù)



那么Spring Boot和和Spring Cloud 功能區(qū)別是什么呢?

[if !supportLists]1.?? [endif]Spring Boot 它內(nèi)嵌Web服務(wù)器(tomcat/jetty)的可執(zhí)行程序的框架。你開發(fā)的web應(yīng)用不需要作為war包部署到web服務(wù)器中[例如:把war包扔到tomcat app下],Spring Boot 直接作為一個可啟動程序,直接把Web啟動起來加載起來。

[if !supportLists]2.?? [endif]Spring Cloud是一套微服務(wù)開發(fā)和治理框架,來自Netflex的OSS,包含了微服務(wù)運(yùn)行的功能,可以通過Spring-boot 項目集成很多功能比如 RabbitMQ,ES,Ribbon,負(fù)載均衡,限流等。



Spring Cloud 核心功能


核心功能:

[if !supportLists]·????????[endif]分布式/版本化配置

[if !supportLists]·????????[endif]服務(wù)注冊和發(fā)現(xiàn)

[if !supportLists]·????????[endif]路由

[if !supportLists]·????????[endif]服務(wù)和服務(wù)之間的調(diào)用

[if !supportLists]·????????[endif]負(fù)載均衡

[if !supportLists]·????????[endif]斷路器

[if !supportLists]·????????[endif]分布式消息傳遞


使用 Spring Cloud作為微服務(wù)開發(fā)框架,Spring Boot 擁有嵌入式 Tomcat,可直接運(yùn)行一個 jar 包來發(fā)布微服務(wù),此外它還提供了一系列“開箱即用”的插件,例如:配置中心,服務(wù)注冊與發(fā)現(xiàn),熔斷器,路由,代理,控制總線,一次性令牌,全局鎖,leader選舉,分布式 會話,集群狀態(tài)等,可大量提高我們的開發(fā)效率。功能

Spring Cloud

路由與負(fù)載均衡

Ribbon

注冊中心

Eureka

網(wǎng)關(guān)

Zuul

斷路器

Hystrix

分布式配置

Config

服務(wù)調(diào)用跟蹤

sleuth

日志輸出

elk

認(rèn)證集成

oauth2

消息總線

Bus

批量任務(wù)

Task


[if !supportLists]·????????[endif]?

Spring Cloud?基本項目搭建步驟

Spring Cloud 服務(wù)注冊發(fā)現(xiàn)實現(xiàn)原理 如圖?





spring中配置文件官方文檔http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/




流程:

[if !supportLists]·????????[endif]請求統(tǒng)一通過 API 網(wǎng)關(guān)(Zuul)來訪問內(nèi)部服務(wù)。

[if !supportLists]·????????[endif]網(wǎng)關(guān)接收到請求后,從注冊中心(Eureka)獲取可用服務(wù)。

[if !supportLists]·????????[endif]由 Ribbon 進(jìn)行均衡負(fù)載后,分發(fā)到后端具體實例。

[if !supportLists]·????????[endif]微服務(wù)之間通過 Feign 進(jìn)行通信處理業(yè)務(wù)。

[if !supportLists]·????????[endif]Hystrix 負(fù)責(zé)處理服務(wù)超時熔斷。

[if !supportLists]·????????[endif]Turbine 監(jiān)控服務(wù)間的調(diào)用和熔斷相關(guān)指標(biāo)。

Spring Cloud工具框架

[if !supportLists]·????????[endif]Spring Cloud Config 配置中心,利用 Git 集中管理程序的配置。

[if !supportLists]·????????[endif]Spring Cloud Netflix 集成眾多Netflix的開源軟件。

[if !supportLists]·????????[endif]Spring Cloud Netflix Eureka 服務(wù)中心(類似于管家的概念,需要什么直接從這里取,就可以了),一個基于 REST 的服務(wù),用于定位服務(wù),以實現(xiàn)云端中間層服務(wù)發(fā)現(xiàn)和故障轉(zhuǎn)移。

[if !supportLists]·????????[endif]Spring Cloud Netflix Hystrix 熔斷器,容錯管理工具,旨在通過熔斷機(jī)制控制服務(wù)和第三方庫的節(jié)點(diǎn),從而對延遲和故障提供更強(qiáng)大的容錯能力。

[if !supportLists]·????????[endif]Spring Cloud Netflix Zuul 網(wǎng)關(guān),是在云平臺上提供動態(tài)路由,監(jiān)控,彈性,安全等邊緣服務(wù)的框架。Web 網(wǎng)站后端所有請求的前門。

[if !supportLists]·????????[endif]Spring Cloud Netflix Archaius 配置管理 API,包含一系列配置管理API,提供動態(tài)類型化屬性、線程安全配置操作、輪詢框架、回調(diào)機(jī)制等功能。

[if !supportLists]·????????[endif]Spring Cloud Netflix Ribbon 負(fù)載均衡

[if !supportLists]·????????[endif]Spring Cloud Netflix Fegin REST客戶端

[if !supportLists]·????????[endif]Spring Cloud Bus 消息總線,利用分布式消息將服務(wù)和服務(wù)實例連接在一起,用于在一個集群中傳播狀態(tài)的變化。

[if !supportLists]·????????[endif]Spring Cloud for Cloud Foundry 利用 Pivotal Cloudfoundry 集成你的應(yīng)用程序。

[if !supportLists]·????????[endif]Spring Cloud Cloud Foundry Service Broker 為建立管理云托管服務(wù)的服務(wù)代理提供了一個起點(diǎn)。

[if !supportLists]·????????[endif]Spring Cloud Cluster 集群工具,基于 Zookeeper, Redis, Hazelcast, Consul 實現(xiàn)的領(lǐng)導(dǎo)選舉和平民狀態(tài)模式的抽象和實現(xiàn)。

[if !supportLists]·????????[endif]Spring Cloud Consul 基于 Hashicorp Consul 實現(xiàn)的服務(wù)發(fā)現(xiàn)和配置管理。

[if !supportLists]·????????[endif]Spring Cloud Security 安全控制,在 Zuul 代理中為 OAuth2 REST 客戶端和認(rèn)證頭轉(zhuǎn)發(fā)提供負(fù)載均衡。

[if !supportLists]·????????[endif]Spring Cloud Sleuth 分布式鏈路監(jiān)控,SpringCloud 應(yīng)用的分布式追蹤系統(tǒng),和 Zipkin,HTrace,ELK 兼容。

[if !supportLists]·????????[endif]Spring Cloud Data Flow 一個云本地程序和操作模型,組成數(shù)據(jù)微服務(wù)在一個結(jié)構(gòu)化的平臺上。

[if !supportLists]·????????[endif]Spring Cloud Stream 消息組件,基于 Redis,Rabbit,Kafka 實現(xiàn)的消息微服務(wù),簡單聲明模型用以在 Spring Cloud 應(yīng)用中收發(fā)消息。

[if !supportLists]·????????[endif]Spring Cloud Stream App Starters 基于 Spring Boot 為外部系統(tǒng)提供 Spring 的集成。

[if !supportLists]·????????[endif]Spring Cloud Task 短生命周期的微服務(wù),為 Spring Booot 應(yīng)用簡單聲明添加功能和非功能特性。

[if !supportLists]·????????[endif]Spring Cloud Task App Starters。

[if !supportLists]·????????[endif]Spring Cloud Zookeeper 服務(wù)發(fā)現(xiàn)和配置管理基于 Apache Zookeeper。

[if !supportLists]·????????[endif]Spring Cloud for Amazon Web Services 快速和亞馬遜網(wǎng)絡(luò)服務(wù)集成。

[if !supportLists]·????????[endif]Spring Cloud Connectors 便于PaaS應(yīng)用在各種平臺上連接到后端像數(shù)據(jù)庫和消息經(jīng)紀(jì)服務(wù)。

[if !supportLists]·????????[endif]Spring Cloud Starters (項目已經(jīng)終止并且在 Angel.SR2 后的版本和其他項目合并)

[if !supportLists]·????????[endif]Spring Cloud CLI 命令行工具,插件用 Groovy 快速的創(chuàng)建 Spring Cloud 組件應(yīng)用。


Spring Cloud 的一些優(yōu)點(diǎn)

[if !supportLists]·????????[endif]有強(qiáng)大的 Spring 社區(qū)、Netflix 等公司支持,并且開源社區(qū)貢獻(xiàn)非常活躍

[if !supportLists]·????????[endif]標(biāo)準(zhǔn)化的將微服務(wù)的成熟產(chǎn)品和框架結(jié)合一起,Spring Cloud 提供整套的微服務(wù)解決方案,開發(fā)成本較低,且風(fēng)險較小

[if !supportLists]·????????[endif]基于 Spring Boot,具有簡單配置、快速開發(fā)、輕松部署、方便測試的特點(diǎn)。

[if !supportLists]·????????[endif]支持 REST 服務(wù)調(diào)用,相比于 RPC,更加輕量化和靈活(服務(wù)之間只依賴一紙契約,不存在代碼級別的強(qiáng)依賴),有利于跨語言服務(wù)的實現(xiàn),以及服務(wù)的發(fā)布部署。另外,結(jié)合 Swagger,也使得服務(wù)的文檔一體化

[if !supportLists]·????????[endif]提供了 Docker 及 Kubernetes 微服務(wù)編排支持。

[if !supportLists]·????????[endif]國內(nèi)外企業(yè)應(yīng)用非常多,經(jīng)受了大公司的應(yīng)用考驗(比如 Netfilx 公司),以及強(qiáng)大的開源社區(qū)支持。

Spring Cloud 的一些問題

[if !supportLists]·????????[endif]支持 REST 服務(wù)調(diào)用,可能因為接口定義過輕,導(dǎo)致定義文檔與實際實現(xiàn)不一致導(dǎo)致服務(wù)集成時的問題(可以使用統(tǒng)一文檔和版本管理解決,比如 Swagger)。

[if !supportLists]·????????[endif]另外,REST 服務(wù)調(diào)用性能會比 RPC 低一些(但也不是強(qiáng)綁定)

[if !supportLists]·????????[endif]Spring Cloud 整合了大量組件,相關(guān)文檔比較復(fù)雜,需要針對性的進(jìn)行閱讀。


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容