<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          重磅!騰訊正式開源SpringCloud解決方案

          共 6969字,需瀏覽 14分鐘

           ·

          2022-06-24 19:42

          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 提供的能力包括但不限于:

          架構(gòu)圖

          一、安裝北極星

          北極星是騰訊開源的服務(wù)發(fā)現(xiàn)和治理中心,致力于解決分布式或者微服務(wù)架構(gòu)中的服務(wù)可見、故障容錯(cuò)、流量控制和安全問(wèn)題。雖然,業(yè)界已經(jīng)有些組件可以解決其中一部分問(wèn)題,但是缺少一個(gè)標(biāo)準(zhǔn)的、多語(yǔ)言的、框架無(wú)關(guān)的實(shí)現(xiàn)。

          騰訊具有海量的分布式服務(wù),加上業(yè)務(wù)線和技術(shù)棧的多樣性,沉淀了大大小小數(shù)十個(gè)相關(guān)組件。從 2019 年開始,我們通過(guò)北極星對(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)行即可[1]

          北極星界面展示

          二、服務(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 上下文里。通過(guò) Spring Boot 標(biāo)準(zhǔn)的 @Value,@ConfigurationProperties 注解即可獲取配置內(nèi)容。動(dòng)態(tài)配置刷新能力,則通過(guò) 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

          全文配套源碼: https://github.com/lltx/spring-cloud-tencent-demo[2]

          參考資料

          [1]

          北極星下載: https://github.com/polarismesh/polaris/releases/tag/v1.9.0

          [2]

          spring-cloud-tencent-demo: https://github.com/lltx/spring-cloud-tencent-demo

          加群聊聊 SCT


          瀏覽 80
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  大战老熟女丝袜高跟 | 黄色操逼动漫 | 黄页网站免费在线观看 | 国产一级片免费观看 | 激情小说综合网 |