# 四、TSF与Spring cloud的集成

本章主要为大家介绍了微服务,Spring Cloud,TSF概念以及功能;到目前为止我们已经掌握了Spring Cloud应用的基本开发,在前面的内容中也介绍了TSF是拥抱Spring Cloud微服务框架的,那TSF是如何跟SpirngCloud进行集成的呢?

实际上使用TSF集成SpirngCloud应用是非常简单的,接下来我会一步步为大家介绍TSF跟Spring Cloud集成。

# 学习目标

通过本文的学习,您将可以:

  • 了解YAML格式
  • 掌握本地TSF服务的注册与发现
  • 掌握TSF接入Spring Cloud的应用
  • 掌握TSF接入Dubbo的应用

# 第一章 YAML与FatJar简介

在正式介绍TSF跟Sprig Cloud应用集成之前,先简单介绍YAML和FatJar;在Spring Boot 应用程序中我们已经简单使用过了yaml配置文件,在后面使用TSF都会大量运用到,所以这些必须先掌握。

# 1.1 YAML概述
  • YAML概念:专门用来写配置文件的语言。

  • YAML 的基本语法规则如下:

    • 使用缩进表示层级关系
    • 缩进时不允许使用Tab键,只允许使用空格
    • 缩进的空格数目不重要,只要相同层级的元素左侧对齐即
    • 大小写敏感
  • YAML 支持3种数据结构

    • 对象:键值对的集合,又称为映射(mapping)/哈希(hashes)/字典(dictionary)。
    • 数组:一组按次序排列的值,又称为序列(sequence)/ 列表(list)。
    • 纯量(scalars):单个的、不可再分的值
  • 在YAML之前使用的是xml,properties文件作为项目的配置文件;特别是早期使用Spring (如ssh框架,ssm框架)都大量使用了xml作为配置文件,目前使用Spring Cloud是同时支持properties和yaml配置文件的;

  • YAML和XML 相比,YAML 具有以下的优势,YAML类似于XML的数据描述语言,语法比XML简单很多,YAML试图用一种比XML更敏捷的方式,来完成XML所完成的任务

    • 可读性好
    • 和脚本语言的交互性好
    • 使用实现语言的数据类型
    • 可以基于流来处理
    • 表达能力强,扩展性好
    • XML 解析效率比较低,不过支持自定义的数据类型
  • JSON 的语法其实是YAML 的子集,大部分的JSON 文件都可以被YAML 的剖析器剖析。虽然大部分的数据分层形式也可以使用类似JSON 的格式,不过YAML 并不建议这样使用,除非这样编写能让文件可读性增加,更重要的是,YAML 的许多扩展在JSON 是找不到的,如:进阶资料形态、关系锚点、字串不需要引号、映射资料形态会储存键值的顺序等。

  • 与properties文件对比:

    • yaml在于其拥有天然的树状结构,:
    • 在properties文件中是以”.”进行分割的,在yml中是用”:”进行分割;
    • yml的数据格式和json的格式很像,都是K-V格式,并且通过”:”进行赋值;
    • 注意在yml中缩进一定不能使用TAB,否则会报很奇怪的错误;每个k的冒号后面一定都要加一个空格;
# 1.1.1 YAML文件示例

image-20211119101752539

  • 通过上面的示例可以很清晰的看出yaml文件存储的是键值对的形式,其结构非常清晰,天然的树状结构。

  • 使用yaml千万要注意“:”后面一定要加空格(“ ”)。

image-20211119101908296

  • 如果字符串之中包含空格或特殊字符,需要放在引号之中。str: '内容: 字符串'

  • 单引号和双引号都可以使用,双引号不会对特殊字符转义。

    • s1: '内容\n字符串' # 会对\n 字符转义
    • s2: "内容\n字符串" # 不会对\n 字符转义
  • 多行字符串可以使用| 保留换行符,也可以使用> 折叠换行

    • this: |
    • Foo
    • Bar
    • that: >
    • Foo
    • Bar
# 1.2 FatJar概述

FatJar是一种可执行的Jar 包(Executable Jar),FatJar和普通的Jar 不同在于它包含了依赖的Jar 包。

  • FatJar打包介绍

    1. 在工程的pom.xml 文件中添加插件:

      <build>
          <plugins>
              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
              </plugin>
          </plugins>
      </build>
      
    2. 在工程的主目录下执行maven命令:mvnclean package,在target 目录下找到打包好的FatJar文件。

  • FatJar又称作uber-Jar,是包含所有依赖的Jar 包,是一个包含所有项目类文件和资源以及所有依赖关系的jar包。有不同的方法可以达到这样的效果:

  • 优势1:依赖关系的jar被复制到主jar中,然后使用特殊的类加载器(onejar)加载

  • 优势2:依赖关系的jar被提取到主jar层次结构的顶部(maven-assembly-plugin与它的jar-with-dependencies程序集,maven-shade-plugin和shade目标)

# 第二章 搭建本地/服务器TSF开发环境

# 2.1 安装JDK
  • 确保开发环境已经安装了JDK:

    1. 下载JDK:

      http://www.oracle.com/technetwork/java/javase/downloads/index.html

    2. 设置JAVA_HOME 环境变量,并指向您机器上的Java 安装目录

    3. 验证JDK环境:命令java -version

      image-20211119115407938

  • 这里我们使用的是JDK1.8版本

  • 在命令窗口使用java –version命令后,如果能出现jdk的版本信息则环境正常

  • 具体的JDK按照步骤比较简单,这里就不重复说明了,具体的操作可以参考:https://cloud.tencent.com/document/product/649/20231#1.-.E5.AE.89.E8.A3.85-java。

# 2.2 安装Maven
  • 在执行安装脚本之前,需要确定已经安装了Maven和设置了环境变量。

  • 下载Maven:https://maven.apache.org/download.cgi

  • 解压并设置环境变量:以Windows为例

    新建系统变量MAVEN_HOME 变量值:E:\Maven\apache-maven-3.3.9

    编辑系统变量Path 添加变量值:;%MAVEN_HOME%\bin

  • 验证Maven环境:命令mvn--version

    image-20211119115725016

  • 目前新版的ide开发工具已经集成了maven,eclipse oxygen版已经集成了maven环境

# 2.3 安装TSF SDK

image-20211119115842757

  • 如图,TSF的sdk安装步骤分为4步;前面2个步骤为环境准备,环境的安装步骤在2.1,2.2中有介绍.

  • 步骤一:

    • JDK安装见2.1章节内容
  • 步骤二:

    • Maven安装见2.2章节内容
    • Maven 配置腾讯云TSF 私服地址(使用下载的setting.xml 替换原本的配置文件)
    • setting.xml 样例文件下载地址:

    ​ https://main.qcloudimg.com/raw/0e3c73b64c4ec64ae9b16d1a347db462/settings.xml

    • 可参考官方文档说明:

      https://cloud.tencent.com/document/product/649/20231

    • setting.xml是maven的配置文件,一般在~/.m2/settings.xml 中,可以在maven的安装目录下的conf文件夹中找到,可以用此处下载的setting样例文件替换原本的setting.xml配置文件

    • 配置TSF私服地址主要是为了能够通过maven获取TSF的SDK代码

    • 如果替换原有的setting配置文件,本地注意备份

  • 步骤三:

    • 获取TSF的Demo工程
    • Demo工程的下载地址:

    ​ https://main.qcloudimg.com/raw/082d94a76b7c0a34feb909c130b72179/tsf-demo-simple-1120.zip

    • TSF官网Demo工程如下:

      • |-consumer-demo
      • |-provider-demo
      • |-pom.xml
    • 可参考官方文档说明:

      https://cloud.tencent.com/document/product/649/20261

    • 获取TSF官网提供的Demo工程,如果有版本更新,只需要更新demo项目根目录下的pom.xml文件,修改TSF的依赖版本。更新日志参考https://cloud.tencent.com/document/product/649/20230

    • 步骤四:

      • 在Demo 工程中,pom.xml 所在目录执行mvn clean package 即可下载TSF SDK

      • 执行完成后,显示build success 表示SDK安装成功。

        image-20211120135246895

    • 详情可参考官网的TSF SDK安装:https://cloud.tencent.com/document/product/649/20231

    # 2.3.1 TSF SDK官方文档地址:

    https://cloud.tencent.com/document/product/649/20231

# 第三章 Spring Cloud应用迁移到TSF

在前面我们主要介绍了yaml配置文件,fatjar,以及TSF的sdk安装,到目前为止,TSF开发的基本环境都已经准备好了,接下来我们先看一下如何在本地使用Spring Cloud应用和TSF进行集成测试。

# 3.1 Spring Cloud 集成TSF本地开发测试
  • Spring Cloud 集成TSF本地开发测试步骤:

    image-20211120135516783

  • 我们都知道TSF提供了服务发现与注册,如果要在本地进行SpirngCloud与TSF集成测试,我们需要先在本地安装启动轻量级服务注册中心(consul);对于Spring cloud应用TSF提供consul作为服务注册中心;这也是我们的第一个步骤:创建轻量级服务注册中心

# 3.1.1 创建轻量级服务注册中心

image-20211120135714144

  • TSF中使用Consul作为服务注册中心而不是SpirngCloud中的Eureka;如上是Eureka与Consul对比;Eureka 2.x已停止更新。

  • 创建轻量级服务注册中心:

    • 轻量级服务注册中心给开发者提供在开发、调试、测试过程中的服务发现、注册和查询功能。
    • 本地开发调试时,需要使用轻量级注册中心,轻量级注册中心包含了TSF 服务注册发现服务端的轻量版。
    • 如果不涉及到多个微服务联调场景,可以通过本地机器启动一个Consul 作为轻量服务注册中心。
      • 单机调试支持Windows 和Linux / macOS 操作系统。
      • 确保以下机器端口是空闲的:8300, 8301, 8302, 8500, 8600。
    • Consul下载地址:https://www.consul.io/downloads.html
    • 也可以是用brew下载consul: brew install consul
      • image-20211120140805807
  • consul是google开源的一个使用go语言开发的服务发现、配置管理中心服务。内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。

  • 服务部署简单,只有一个可运行的二进制的包。每个节点都需要运行agent,他有两种运行模式server和client。每个数据中心官方建议需要3或5个server节点以保证数据安全,同时保证server-leader的选举能够正确的进行。

  • 启动Consul,命令:

    • Windows 操作系统:.\consul.exe agent -dev

    • Linux / macOS 操作系统:./consul agent -dev

      • 我是使用brew下载的:所以我得启动命令是/opt/homebrew/opt/consul/bin/consul agent -dev

        image-20211120140926786

  • 通过浏览器查看服务注册中心页面:http://127.0.0.1:8500

    image-20211120140951139

  • 如果访问http://127.0.0.1:8500 能出现如上图结果,表示我们的服务注册中心启动成功

  • 多微服务联调环境的轻量服务注册中心:在多个微服务联调场景下,可以找一台可以被微服务访问的机器来部署轻量服务注册中心。目前本场景下仅支持Linux 系统的Consul

  • 详情参考:https://cloud.tencent.com/document/product/649/16618

# 3.1.2 创建服务提供者(本地开发应用)

创建 tsf-demo 工程,文件结构如下:

|- consumer-demo
|- provider-demo
|- pom.xml

其中pom.xml文件参考 Demo 工程概述 (opens new window) 中的pom.xml内容。

  • 服务提供者创建步骤:

    1. 创建一个Spring Cloud 工程,命名为provider-demo(结构如下)

      image-20211122141111326

    2. 修改pom 依赖,引入TSF依赖(完整的pom依赖)

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <parent>
              <artifactId>tsf-demo</artifactId>
              <groupId>com.keepdive</groupId>
              <version>1.0-SNAPSHOT</version>
          </parent>
          <modelVersion>4.0.0</modelVersion>
      
          <artifactId>provider-demo</artifactId>
          <name>provider-demo</name>
      
          <properties>
              <maven.compiler.source>8</maven.compiler.source>
              <maven.compiler.target>8</maven.compiler.target>
          </properties>
      
          <dependencies>
              <dependency>
                  <groupId>com.tencent.tsf</groupId>
                  <artifactId>spring-cloud-tsf-starter</artifactId>
              </dependency>
      
          </dependencies>
      
      </project>
      
    3. 开启服务注册发现

      @SpringBootApplication
      @EnableFeignClients // 使用Feign微服务调用时请启用
      @EnableTsf
      public class ProviderApplication {
          public static void main(String[] args) {
              SpringApplication.run(ProviderApplication.class, args);
          }
      }
      
    4. 提供echo 服务

      @RestController
      public class EchoController {
          @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
          public String echo(@PathVariable String string) {
              return string;
          }
      }
      
    5. 修改application.yml配置文件

      server:
        port: 18081
      
      spring:
        application:
          name: provider-demo
      
  • 具体操作步骤参考官方文档:

    https://cloud.tencent.com/document/product/649/16617

  • 服务提供者开发操作步骤跟之前演示的Spring Cloud应用程序服务提供者开发步骤一致,这里区别在与引入了TSF相关依赖

  • 这里的yml配置文件就是我们前面讲的YAML配置文件

# 3.1.3 创建服务消费者(本地开发应用)
  • 服务消费者创建步骤:

    1. 创建一个Spring Cloud 工程,命名为consumer-demo

      image-20211122144008373

    2. 修改pom 依赖,引入TSF依赖

         <parent>
              <artifactId>tsf-demo</artifactId>
              <groupId>com.keepdive</groupId>
              <version>1.0-SNAPSHOT</version>
          </parent>
          <modelVersion>4.0.0</modelVersion>
      
          <artifactId>consumer-demo</artifactId>
      
          <properties>
              <maven.compiler.source>8</maven.compiler.source>
              <maven.compiler.target>8</maven.compiler.target>
          </properties>
      
          <dependencies>
              <dependency>
                  <groupId>com.tencent.tsf</groupId>
                  <artifactId>spring-cloud-tsf-starter</artifactId>
              </dependency>
          </dependencies>
      
      
    3. 开启服务注册发现,与服务提供者provider-demo 相比,还需要添加两项配置才能使用RestTemplate、AsyncRestTemplate、FeignClient 这三个客户端

      • 添加@LoadBalanced注解将RestTemplateAsyncRestTemplate与服务发现结合。
      • 使用@EnableFeignClients注解激活FeignClients
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.client.loadbalancer.LoadBalanced;
      import org.springframework.cloud.openfeign.EnableFeignClients;
      import org.springframework.context.annotation.Bean;
      import org.springframework.tsf.annotation.EnableTsf;
      import org.springframework.web.client.AsyncRestTemplate;
      import org.springframework.web.client.RestTemplate;
      
      @SpringBootApplication
      @EnableFeignClients // 使用 Feign 微服务调用时请启用
      @EnableTsf
      public class ConsumerApplication {
          @LoadBalanced
          @Bean
          public RestTemplate restTemplate() {
              return new RestTemplate();
          }
          @LoadBalanced
          @Bean
          public AsyncRestTemplate asyncRestTemplate() {
              return new AsyncRestTemplate();
          }
          public static void main(String[] args) throws InterruptedException {
              SpringApplication.run(ConsumerApplication.class, args);
          }
      }
      
    4. 设置调用信息

      • 在使用EchoServiceFeignClient之前,还需要完善它的配置。配置服务名以及方法对应的 HTTP 请求,服务名为provider-demo工程中配置的服务名provider-demo,代码如下:

        import org.springframework.cloud.openfeign.FeignClient;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        
        @FeignClient(name = "provider-demo")
        public interface EchoService {
            @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
            String echo(@PathVariable("str") String str);
        }
        
    5. 创建Controller

      • 创建一个Controller供调用测试。
      • /echo-rest/*验证通过 RestTemplate 去调用服务提供者。
      • /echo-async-rest/*验证通过 AsyncRestTemplate 去调用服务提供者。
      • /echo-feign/*验证通过 FeignClient 去调用服务提供者。

      具体代码如下:

      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.http.ResponseEntity;
      import org.springframework.util.concurrent.ListenableFuture;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.annotation.RestController;
      import org.springframework.web.client.AsyncRestTemplate;
      import org.springframework.web.client.RestTemplate;
      
      @RestController
      public class Controller {
          @Autowired
          private RestTemplate restTemplate;
          @Autowired
          private AsyncRestTemplate asyncRestTemplate;
          @Autowired
          private  EchoService echoService;
          @RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
          public String rest(@PathVariable String str) {
              return restTemplate.getForObject("http://provider-demo/echo/" + str, String.class);
          }
          
          @RequestMapping(value = "/echo-async-rest/{str}", method = RequestMethod.GET)
          public String asyncRest(@PathVariable String str) throws Exception{
              ListenableFuture<ResponseEntity<String>> future = asyncRestTemplate.
                      getForEntity("http://provider-demo/echo/"+str, String.class);
              return future.get().getBody();
          }
          
          @RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
          public String feign(@PathVariable String str) {
              return echoService.echo(str);
          }
      }
      
    6. 修改application.yml配置文件

      server:
        port: 18083
      
      spring:
        application:
          name: consumer-demo
      
  • 具体操作步骤参考官方文档:

    https://cloud.tencent.com/document/product/649/16617

  • 服务消费者开发操作步骤跟之前演示的Spring Cloud应用程序的消费者开发步骤一致,这里区别在与引入了TSF相关依赖

# 3.1.4 本地开发调试
  • 启动轻量级注册中心,在终端执行以下命令(就是在你安装consul的目录: 执行consul agent -dev命令):

    /opt/homebrew/opt/consul/bin/consul agent -dev

  • 启动应用:本地启动应用的两种方式:

    1. IDE 中启动
      • 通过VM options 配置启动参数-Dtsf_consul_ip=127.0.0.1 -Dtsf_consul_port=8500 ;指明了服务注册中心的ip以及端口;
      • 如果使用了分布式配置功能的模块,还需要设置-Dtsf_application_id=a -Dtsf_group_id=b,取值可为任意值
    2. FatJar启动
      • 打包好fatjar后通过java命令启动:(示例如下)
      • java -Dtsf_consul_ip=127.0.0.1 -Dtsf_consul_port=8500 -jar provider-demo-0.0.1-SNAPSHOT.jar
    • 此处注意在部署到TSF以后是不需要加如上参数,在TSF平台中已经把注册中心的ip以及端口放到了环境变量中
  • 启动后访问:

    http://localhost:8500可以在consul注册中心发现服务提供者和服务消费者

    image-20211122150349238

  • 访问服务消费者,分别进行调用,观察调用结果(这里以本地演示)

    • http://localhost:18083/echo-rest/echo-testimage-20211122150749832

    • http://localhost:18083/echo-async-rest/echo-async-rest

      image-20211122150903689

    • http://localhost:18083/echo-feign/feign

      image-20211122151008702

详细步骤参考官网文档:https://cloud.tencent.com/document/product/649/16617;实际上基本操作,访问步骤跟前面开发的Spring Cloud应用程序一致

# 3.2 Spring Cloud 旧应用迁移到TSF
  • Spring Cloud 旧应用迁移到TSF:

    • Spring Cloud应用如果原本使用的是Eureka 服务注册中心,要迁移到TSF,首先就需要把注册中心从Eureka迁移至consul注册中心;

    • 迁移到TSF步骤如下:

      image-20211123133215861

  • Spring Cloud开发的应用使用的是Eureka作为服务注册中心,那如何将Eureka服务注册中心迁移至TSF呢?实际上从Eureka迁移到TSF是把注册中心从eureka迁移到TSF提供的consul注册中心,所以基本步骤是添加TSF,consul对应的依赖包,然后修改应用启动类中的Eureka启动注解为consul的服务发现注解

  • 在工程根目录的pom.xml 中增加spring-cloud-tsf-dependencies 的parent。

  • 在单个Spring Cloud 应用的pom.xml 中,将spring-cloud-starter-eureka 替换成spring-cloud-starter-consul-discovery。

# 3.2.1 添加parent配置
  • 步骤一:

    • pom.xml中根节点下添加parent配置:

      <parent>
          <groupId>com.tencent.tsf</groupId>
          <artifactId>spring-cloud-tsf-dependencies</artifactId>
          <version>1.23.0-Greenwich-RELEASE</version>
      </parent>
      
    • 这里的项目代码以SpirngCloud内容介绍中的案例为基础。

    • 首先添加TSF的SDK作为parent

# 3.2.2 添加tsf-starte依赖
  • 步骤二:

    • pom.xml中替换eureka的依赖为consul:

      • 替换前:

         <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                    <version>1.5.11.RELEASE</version>
                </dependency>
        
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-config</artifactId>
                </dependency>
        <!--        -->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
                </dependency>
            </dependencies>
        
            <dependencyManagement>
                <dependencies>
                    <!-- springboot 1.5.11对应的springcloud依赖管理 -->
                    <dependency>
                        <groupId>org.springframework.cloud</groupId>
                        <artifactId>spring-cloud-dependencies</artifactId>
                        <version>Dalston.RELEASE</version>
                        <type>pom</type>
                        <scope>import</scope>
                    </dependency>
                </dependencies>
            </dependencyManagement>
        
      • 替换后:

          <dependencies>
                <dependency>
                    <groupId>com.tencent.tsf</groupId>
                    <artifactId>spring-cloud-tsf-starter</artifactId>
                </dependency>
            </dependencies>
        
    • 完整的POM文件

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
      
          <groupId>com.keepdive</groupId>
          <artifactId>eureka-provider</artifactId>
          <version>1.0-SNAPSHOT</version>
      
          <parent>
              <groupId>com.tencent.tsf</groupId>
              <artifactId>spring-cloud-tsf-dependencies</artifactId>
              <version>1.23.0-Greenwich-RELEASE</version>
          </parent>
      
          <properties>
              <maven.compiler.source>8</maven.compiler.source>
              <maven.compiler.target>8</maven.compiler.target>
          </properties>
      
          <dependencies>
              <dependency>
                  <groupId>com.tencent.tsf</groupId>
                  <artifactId>spring-cloud-tsf-starter</artifactId>
              </dependency>
          </dependencies>
      
      </project>
      
# 3.2.3 添加注解
  • 步骤三:

    • 替换eureka启动注解为TSF注解:

    • 以eureka-provider为例,只需要在启动类中替换@EnableEurekaClient注解为@EnableDiscoveryClient注解即可

      image-20211123141925675

  • @EnableDiscoveryClient表明开启consul的服务发现,由于在之前的项目我们已经加上了@EnableDiscoveryClient注解,所以此次在原项目基础上只需要去掉@EnableEurekaClient即可。

  • @EnableDiscoveryClient是由spring Cloud提供的注解,支持eureka,consul,zookkeeper注册中心。

  • 完成这些操作以后,就可以完成Eureka迁移到TSF中。从前面的步骤也可以看出来,整个迁移过程是非常简单的。

  • TSF 中的相关注解:

    • 注解:@EnableTsfAuth
      • TSF 提供了两种类型的鉴权能力,一种根据调用方服务名,一种根据调用方设置的tag。在管控端进行配置鉴权。
      • 其实原理是一样的,服务名作为一种特殊的key的tag。在服务调用时,客户端传输tag,服务端根据鉴权规则进行判断
      • 其中标签将会在后面的内容介绍,这里大家先了解即可
    • 注解:@EnableTsfRoute
      • 启用TSF的路由功能
    • 注解:@EnableTsfRateLimit启动
      • 使用管控端写入到consul配置,服务端拉取配置信息,进行流控(令牌桶)。

3.2.4 启动测试

  • ​ 启动eureka-provider

    image-20211123155315102

3.2.5 异常检查以及部署

  • TSF 中部署应用
    • 将打包好的FatJar程序包上传到TSF 控制台进行部署操作,不需要额外配置。
  • 通过TSF 平台部署改造后的应用,如发现应用无法启动,说明没有正确依赖TSF 相关依赖,可参考Demo 工程中pom.xml 文件。
  • 从前面的步骤来看,Spring Cloud应用迁移至TSF实际上是非常简单的,只需要在原应用的基础上添加TSF相关依赖,注解,然后再通过TSF控制台部署应用即可。
  • 部署步骤暂时不做演示,TSF控制台部署应用会在后续的TSF平台管理能力中介绍,这里大家可以结合前面讲到的TSF的概念关系内容思考一下。

# 第四章 Dubbo应用迁移到TSF

前面在介绍TSF的时候,有讲到TSF时拥抱微服务开源框架的,支持Spring Cloud应用,Dubbo应用,ServcieMesh应用。上一章节中已经介绍了Spring Cloud应用程序迁移至TSF后,接下来看一下Dubbo应用如何迁移到TSF。

# 4.1 什么是Dubbo?

Dubbo是阿里巴巴公司开源的一个高性能,优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring框架无缝集成。

image-20211123175729836

  • 高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:

    1. 面向接口的远程方法调用
    2. 智能容错和负载均衡
    3. 服务自动注册和发现
  • 采用全Spring 配置方式,透明化接入应用,只需用Spring 加载Dubbo的配置即可,可以Spring框架无缝集成。

# 4.2 Dubbo协议应用迁移到TSF
  • Dubbo 应用接入TSF开发步骤:

    image-20211123180046691

  • 如上图是Dubbo迁移到TSF的开发步骤,此处步骤基于Dubbo官方案例Demo。可参考:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

# 4.2.1 从Dubbo迁移到TSF
  • 安装依赖:

    mvn install:install-file -Dfile=dubbo-registry-consul-1.1.4-SNAPSHOT.jar 
    -DpomFile=dubbo-registry-consul-1.1.4-SNAPSHOT.pom
    
  • 依赖包直接下载地址:

    https://main.qcloudimg.com/raw/d075de11daa5d1427346e19880cd5492/dubbo-registry-consul-1.1.4-SNAPSHOT.zip

  • 注册中心配置:

    • 把<dubbo:registry address=”multicast://224.5.6.7:1234”/>替换为<dubbo:registry address=”consul://注册中心地址:端口”/>
  • 添加依赖

    <dependency>
          <groupId>com.tencent.tsf</groupId>
          <artifactId>dubbo-registry-consul</artifactId>
      		<!--修改为对应的版本号-->
      		<version>1.1.4-SNAPSHOT</version>
    </dependency>
    
  • 打包Fatjar

    • 可以通过maven-shade-plugin 来构建一个包含所有依赖的jar 包(FatJar)。执行命令mvn clean package
  • Dubbo应用接入TSF官网文档:

    https://cloud.tencent.com/document/product/649/13947