SpringCloud?Tencent?全套解決方案源碼分析
Spring Cloud Tencent 是什么?
Spring Cloud Tencent 是騰訊開源的一站式微服務(wù)解決方案。Spring Cloud Tencent 實(shí)現(xiàn)了 Spring Cloud 標(biāo)準(zhǔn)微服務(wù) SPI,開發(fā)者可以基于 Spring Cloud Tencent 快速開發(fā) Spring Cloud 微服務(wù)架構(gòu)應(yīng)用。Spring Cloud Tencent 的核心依托騰訊開源的一站式服務(wù)發(fā)現(xiàn)與治理平臺(tái) Polarismesh ,實(shí)現(xiàn)各種分布式微服務(wù)場(chǎng)景。
Spring Cloud Tencent 提供的能力包括但不限于:
項(xiàng)目地址:https://github.com/Tencent/spring-cloud-tencent
項(xiàng)目源碼地址
https://github.com/lltx/spring-cloud-tencent-demo
一、安裝北極星
北極星是騰訊開源的服務(wù)發(fā)現(xiàn)和治理中心,致力于解決分布式或者微服務(wù)架構(gòu)中的服務(wù)可見、故障容錯(cuò)、流量控制和安全問題。雖然,業(yè)界已經(jīng)有些組件可以解決其中一部分問題,但是缺少一個(gè)標(biāo)準(zhǔn)的、多語(yǔ)言的、框架無(wú)關(guān)的實(shí)現(xiàn)。
騰訊具有海量的分布式服務(wù),加上業(yè)務(wù)線和技術(shù)棧的多樣性,沉淀了大大小小數(shù)十個(gè)相關(guān)組件。從 2019 年開始,我們通過北極星對(duì)這些組件進(jìn)行抽象和整合,打造公司統(tǒng)一的服務(wù)發(fā)現(xiàn)和治理方案,幫助業(yè)務(wù)提升研發(fā)效率和運(yùn)營(yíng)質(zhì)量。
北極星安裝非常的簡(jiǎn)單下載響應(yīng)平臺(tái)的 zip 直接運(yùn)行即可,下載地址:https://github.com/polarismesh/polaris/releases/tag/v1.9.0
二、服務(wù)注冊(cè)與發(fā)現(xiàn)
服務(wù)增加 polaris-discovery 依賴
<dependency> <groupId>com.tencent.cloud</groupId> <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId> </dependency>
application.yaml 接入 polaris server
spring: cloud: polaris: address: grpc://127.0.0.1:8091
啟動(dòng)服務(wù)觀察 polaris console
服務(wù)調(diào)用示例
@Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } @Autowired private RestTemplate restTemplate; @GetMapping("/consumer") public String consumer() { return restTemplate.getForObject("http://lengleng-tencent-discovery-provider/provider/lengleng", String.class); }
三、配置管理
在應(yīng)用啟動(dòng) Bootstrap 階段,Spring Cloud 會(huì)調(diào)用 PolarisConfigFileLocator 從 Polaris 服務(wù)端獲取配置文件并加載到 Spring 上下文里。通過 Spring Boot 標(biāo)準(zhǔn)的 @Value,@ConfigurationProperties 注解即可獲取配置內(nèi)容。動(dòng)態(tài)配置刷新能力,則通過 Spring Cloud 標(biāo)準(zhǔn)的 @RefreshScope 機(jī)制實(shí)現(xiàn)。
服務(wù)增加 polaris-config 依賴
<dependency> <groupId>com.tencent.cloud</groupId> <artifactId>spring-cloud-starter-tencent-polaris-config</artifactId> </dependency>
bootstrap.yaml 接入 polaris-config
spring: cloud: polaris: address: grpc://127.0.0.1:8081 config: groups: - name: ${spring.application.name} files: "application"
特別注意: 這里需要配置在 bootstrap, spring-cloud-tencent 未適配 spring boot 最新的文件加載機(jī)制
北極星控制臺(tái)增加配置
代碼使用配置
@Value("${name:}") private String name;
四、服務(wù)限流
服務(wù)限流是最常見的一種服務(wù)自我保護(hù)措施之一,防止流量洪峰打垮服務(wù)。Spring Cloud Tencent Rate Limit 模塊內(nèi)置了針對(duì) Spring Web 和 Spring WebFlux 場(chǎng)景的限流 Filter,結(jié)合 Polaris 的限流功能幫忙業(yè)務(wù)快速接入限流能力。
服務(wù)增加 polaris-ratelimit 依賴,使用限流組件時(shí)添加 discovery ,方便在服務(wù)列表編輯
<dependency> <groupId>com.tencent.cloud</groupId> <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId> </dependency> <dependency> <groupId>com.tencent.cloud</groupId> <artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId> </dependency>
服務(wù)接入 polaris-ratelimit
spring: cloud: polaris: address: grpc://127.0.0.1:8091 namespace: default ratelimit: reject-http-code: 403 reject-request-tips: "lengleng test rate limit"
北極星控制臺(tái)增加限流規(guī)則
五、服務(wù)路由
polaris 能夠?qū)崿F(xiàn)的路由形式較多元數(shù)據(jù)路由、就近路由、規(guī)則路由、自定義路由等形式,本文以元數(shù)據(jù)路由演示,如下圖只會(huì)路由至相同元數(shù)據(jù)信息的服務(wù)
服務(wù)增加 polaris-router 依賴
<dependency> <groupId>com.tencent.cloud</groupId> <artifactId>spring-cloud-starter-tencent-polaris-router</artifactId> </dependency>
服務(wù)標(biāo)記元數(shù)據(jù)
spring: cloud: polaris: address: grpc://127.0.0.1:8091 tencent: metadata: content: version: local
六、限流熔斷
故障實(shí)例熔斷是常見的一種容錯(cuò)保護(hù)機(jī)制。故障實(shí)例熔斷能實(shí)現(xiàn)主調(diào)方迅速自動(dòng)屏蔽錯(cuò)誤率高或故障的服務(wù)實(shí)例,并啟動(dòng)定時(shí)任務(wù)對(duì)熔斷實(shí)例進(jìn)行探活。在達(dá)到恢復(fù)條件后對(duì)其進(jìn)行半開恢復(fù)。在半開恢復(fù)后,釋放少量請(qǐng)求去進(jìn)行真實(shí)業(yè)務(wù)探測(cè)。并根據(jù)真實(shí)業(yè)務(wù)探測(cè)結(jié)果去判斷是否完全恢復(fù)正常。
添加限流熔斷相關(guān)的依賴 polaris-circuitbreaker
<dependency> <groupId>com.tencent.cloud</groupId> <artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> <dependency> <groupId>com.tencent.cloud</groupId> <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-circuitbreaker-spring-retry</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
提供 Feign 服務(wù)調(diào)用實(shí)現(xiàn)
spring-cloud-tencent 當(dāng)前版本僅支持 feign 熔斷
@FeignClient(contextId = "demoFeign", value = "lengleng-circuitbreaker-tencent-circuitbreaker-provider", fallback = DemoFeignFallback.class) public interface DemoFeign { @GetMapping("/provider") String get(@RequestParam String name); }
服務(wù)接入 polaris-circuitbreaker
spring: cloud: polaris: address: grpc://127.0.0.1:8091 #開啟斷路器 feign: circuitbreaker: enabled: true
編寫熔斷規(guī)則 polaris.yml
consumer: circuitBreaker: checkPeriod: 100ms chain: - errorCount - errorRate plugin: errorCount: continuousErrorThreshold: 1 metricNumBuckets: 1 errorRate: errorRateThreshold: 100 metricStatTimeWindow: 1s requestVolumeThreshold: 1
到此這篇關(guān)于SpringCloud Tencent 全套解決方案的文章就介紹到這了,更多相關(guān)SpringCloud Tencent內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis分頁(yè)插件PageHelper手寫實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Mybatis分頁(yè)插件PageHelper手寫實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢
這篇文章主要介紹了使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法
這篇文章主要介紹了Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03基于springboot 長(zhǎng)輪詢的實(shí)現(xiàn)操作
這篇文章主要介紹了基于springboot 長(zhǎng)輪詢的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-01-01Spring事件監(jiān)聽機(jī)制ApplicationEvent方式
這篇文章主要介紹了Spring事件監(jiān)聽機(jī)制ApplicationEvent方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09