一、ServiceComb的概述
ServiceComb
作爲Apache
開源組織下的一款微服務框架,其前身爲華爲雲的微服務引擎CSE
(Cloud Service Engine
) 雲服務。
解決方案級,多語言、多通信協議、標準服務契約、事務最終一致性開源開放,擁抱SpringBoot
、SpringCloud
、ServiceMesh
等主流生態,低門檻准入,業務侵入度低,架構鬆耦合。
ServiceComb
的優勢:國內政府部門有的會選擇華爲雲,使用ServiceComb
開發可以快速優雅的部署到華爲雲的的pass
平臺上。至於ServiceComb
支持RPC
調用,REST
難道不香嗎?CSE
註冊中心中的服務契約提供的類似swagger
和接口測試功能,倒是挺好用的。
缺點:由於ServiceComb
的特性,遇到bug
的可能百度很難解決。在SpringCloud F
版後,SpringCloud
的功能更強大了,普及度方面SpingCloud
也更多人使用。
二、服務中心CSE的介紹
CSE
的架構和Eureka
很像,經過使用後,可以發現CSE
的自我保護機制比Eureka
還要嚴重。
下載安裝CSE
的安裝包:apache-servicecomb-service-center-1.3.0-windows-amd64.tar.gz,解壓文件目錄如下:
三、Restful方式開發
完整代碼參考:https://github.com/hucheng1997/servicecomb
①添加依賴
父工程依賴:(這裏選擇1.x版本的springboot
,2.x版本會遇到意料之外的錯誤!)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-dependencies</artifactId>
<version>1.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
provider
和consumer
工程的依賴如下:
<!--做參數校驗的-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-boot-starter-provider</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
②添加配置文件
這裏配置文件只能爲microservice.yaml
,這是ServiceComb
的硬性規定:
APPLICATION_ID: hucheng #一般填公司的名稱
service_description:
name: servicecomb-provider #微服務名
version: 0.0.1 #版本
environment: development #環境
properties:
allowCrossApp: false #不允許跨App訪問服務
rest:
address: 0.0.0.0:81 #服務端口
service:
registry:
address: http://127.0.0.1:30100 #註冊中心的位置
③主啓動上添加註解@EnableServiceComb
④Provider代碼及說明
⑤Consumer調用代碼
使用RestTemplate
遠程調用服務
⑥測試
使用註冊中心中的服務契約進行測試
四、RPC方式開發
將@RestSchema
註解換成@RpcSchema
即可
五、ServiceComb服務治理方案
①負載均衡
使用了ServiceComb
框架的微服務即自帶了負載均衡功能,無需任何配置。
②限流策略
添加依賴:
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>handler-flowcontrol-qps</artifactId>
</dependency>
添加配置:
servicecomb:
flowcontrol:
Provider:
qps:
limit:
gateway: 100 #限流每秒QPS次數的限制
③熔斷
添加依賴:
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>handler-bizkeeper</artifactId>
</dependency>
添加配置:
servicecomb:
circuitBreaker:
provider:
shicifang-qa:
requestVolumeThreshold: 1
fallbackpolicy:
provider:
policy: returnnull #容錯策略