tsf 简介
TSF 全称是腾讯微服务平台 (Tencent Service Framework), 它在我的理解中就是整合了微服务组件的各个功能的一站式部署与管理平台, 这个平台本身也是免费的, 你需要付费的是云服务器的费用. 如果你的应用想上云, 而且是微服务, 不妨考虑 TSF, 不仅可以减少你的代码开发量而且提供了运维界面.
TSF 平台官方文档
tsf 入门
TSF 我将它简单的分为两部分, 一部分是用于开发的 SDK, 一部分是提供运维界面的腾讯云 TSF 平台. 接下里我会就我个人的使用经验来介绍这两大块.
环境准备
首先我们的开发流程是这样的, 先建一个本地的 springboot maven 应用, 引入 tsf 依赖, 然后配置运行环境, 开发业务功能, 最后部署到 TSF 云平台上.
TSF 的微服务注册中心是依赖于 consul 的, 因此我们要先下载一个 consul, 后面还会进行容器化部署, 最好还安装一个 docker. 相对 Mac 而言, Windows 对开发者而言确实不是很友好, 在这里我分享一些 Windows 环境开发的一些小技巧. 如果你的 Windows 系统是 win10 专业版或企业版, 是可以直接安装 docker 的, 具体安装方式百度就能搜到, 我就不在这里水了. 我介绍一下如何在 Windows 环境下不使用 docker 的情况下, 比较方便的启动 ZK,Redis,consul 这些中间件.
以 consul 为例, 下载并解压后, 编写 consul.bat 脚本, 内容如下:
C:\exe\consul.exe agent -dev
根据自己 consul 的文件路径自行修改,
然后将 consul.bat 的所在文件夹路径配置到环境变量中, 后续如果再安装 Redis 等中间件都可以将启动脚本放到该文件夹中; 快捷键 win+r 输入脚本名就可以直接调用该脚本:
当然, 也可以直接使用 docker, 但是 docker 麻烦的地方就是改配置比较麻烦, 所以我比较习惯本地启动, 看个人喜好吧.
编程及源码分析
下载好 consul 后我们就可以进行编程了.
我们知道 spring cloud 与 springboot 是有版本对应的关系的, 各个版本之间存在兼容问题, TSF 本身又要兼容 spring cloud 那么必然存在一个版本对应关系, 官方提供的版本对应关系:
TSF 私有化平台版本 | Edgware | Finchley | Greenwich |
---|---|---|---|
1.23.x | 1.21.4-Edgware-RELEASE | 1.23.4-Finchley-RELEASE | 1.23.5-Greenwich-RELEASE |
1.21.x | 1.21.4-Edgware-RELEASE | 1.21.6-Finchley-RELEASE | 1.21.4-Greenwich-RELEASE |
1.18.x | 1.18.1-Edgware-RELEASE | 1.18.5-Finchley-RELEASE | 1.18.1-Greenwich-RELEASE |
1.12.x | 1.12.5-Edgware-RELEASE | 1.12.5-Finchley-RELEASE | - |
我这里使用的是 F 版, 小伙伴们也可以使用比较新的 G 版. 先创建一个 springboot 应用, 然后再 pom 文件中加入仓库信息 (TSF 的依赖并未在 apach 的仓库中, 而是在腾讯的仓库中), 依赖, 插件等:
- <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.6.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example</groupId>
- <artifactId>tsf-provider</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>tsf-provider</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>com.tencent.tsf</groupId>
- <artifactId>spring-cloud-tsf-dependencies</artifactId>
- <version>1.18.1-Finchley-RELEASE</version>
- <scope>import</scope>
- <type>pom</type>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <!-- TSF 启动器 -->
- <dependency>
- <groupId>com.tencent.tsf</groupId>
- <artifactId>spring-cloud-tsf-starter</artifactId>
- </dependency>
- <!-- 服务发现 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-consul-discovery</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
- </dependencies>
- <repositories>
- <repository>
- <id>public</id>
- <name>TSF REP</name>
- <url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
- <layout>default</layout>
- <snapshots>
- <updatePolicy>always</updatePolicy>
- <checksumPolicy>warn</checksumPolicy>
- <enabled>true</enabled>
- </snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>qcloud-plugin-central</id>
- <url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- <releases>
- <enabled>true</enabled>
- </releases>
- </pluginRepository>
- </pluginRepositories>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- <!--docker 插件 -->
- <plugin>
- <groupId>com.spotify</groupId>
- <artifactId>dockerfile-maven-plugin</artifactId>
- <version>1.3.6</version>
- <configuration>
- <repository>ccr.ccs.tencentyun.com/< 账号 ID>/docker-${project.artifactId}</repository>
- <buildArgs>
- <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
- </buildArgs>
- <tag>${project.version}</tag>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
docker 的 maven 插件中的 账号 ID 获取方式, 点击右上角账号信息就能看到自己的账号 id, 然后替换掉 maven 中的 < 账号 ID> 字样:
image.PNG
然后在启动类上添加 @EnableTsf 注解, 我们不妨来看看这个注解:
- @Target({
- ElementType.TYPE
- })
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- @EnableAutoConfiguration
- @EnableDiscoveryClient
- @EnableConfigurationProperties
- @EnableTsfAuth
- @EnableTsfRoute
- @EnableTsfRateLimit
- @EnableTsfSleuth
- @EnableTsfMonitor
- public @interface EnableTsf {
- }
不难看出这个注解是利用注解的派生性来实现各个模块的自动装配能力; 实际上这个 stater 包源码里只有这一个注解, 再没有其他类了, 由此不难猜测出其自动装配类实际是在其所依赖的模块中. 后续如果有机会在探索其他诸如日志链路追踪相关的代码, 这里就不做太多分析了.
在项目根目录下添加 Dockerfile 文件和 run.sh 文件, dockerfile 和 run.sh 是为了后续容器化部署准备的.
- dockerfile:
- FROM CentOS:7
- RUN yum update -y && yum install -y java-1.8.0-openjdk
- ARG JAR_FILE
- COPY ${JAR_FILE} /data/tsf/App.jar
- COPY run.sh /data/tsf/run.sh
- # GMT+8 for CentOS
- RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
- RUN echo "Asia/Shanghai"> /etc/timezone
- # run.sh
- CMD ["sh", "-c", "cd /data/tsf; sh run.sh app.jar /data/tsf"]
- run.sh:
- #!/bin/bash
- default_log_path="/data/tsf_default"
- stout_log_path="/data/tsf_std/stdout/logs"
- stout_log="$stout_log_path/sys_log.log"
- echo "para1 is"$1
- echo "para2 is"$2
- echo $stout_log_path
- echo $stout_log
- mkdir -p $stout_log_path
- if [ ! -n "$2" ] ;then
- echo "you have not input logpath!"
- else
- mkdir -p $default_log_path
- cd $2
- cp $1 $default_log_path
- sleep 5
- cd $default_log_path
- fi
- java ${JAVA_OPTS} -jar $1> $stout_log 2>&1
最后随便写一个接口, 启动 consul, 启动应用, 然后访问 consul 控制台, 默认是 8500 端口, 观察是否注册成功; 效果如图:
然后以同样的方式创建一个消费者应用, 通过 openfeign 去调用生产者, 前文分析过 @EnableTsf 注解, 并未包含 @EnableFeignClients 因此我们需要自己在启动类加上这个注解
部署到腾讯云
现在我已经开发出两个应用, 接下来就要把它们部署到腾讯云上; 登录腾讯云, 点击右上角控制台后在云产品中搜索 TSF:
image.PNG
进入 TSF 控制台, 单击左侧导航栏中的集群, 单击集群列表上方的新建集群. 新建集群的类型选择容器集群.
image.PNG
接下来我们需要买一个云主机, 这个单纯做测试的话, 十块钱都能玩很久, 所以不用担心浪费钱. 点击云产品 - 云服务器, 新建一个实例:
- image.PNG
- image.PNG
然后选择按量计费, 其他的随便选选 (用完记得注销, 不然要扣钱的):
image.PNG
我这里才几毛钱一个小时:
image.PNG
在安全组策略配置里面, 配置你要开放的端口, 你自己的应用端口肯定是要对外开放的, 不然没办法访问, 我这里是 8081:
- image.PNG
- image.PNG
安全策略配置参考资料:
其他的随便选选, 不懂就点击使用指引瞅瞅:
image.PNG
创建实例完成后, 导入实例到集群:
image.PNG
导入完成, 我们新建一个应用:
image.PNG
这个应用名称是要和你 maven 插件中的 <repository> 标签对应起来的, 具体对应方式: 点击你创建的应用:
image.PNG
点击镜像 - 使用指引:
image.PNG
它这里会告诉你, 你的镜像怎么推送到哪个仓库去, 懂 docker 插件的人应该不难理解这里的配置原理.
比如我创建的应用名称是 docker-tsf-provider
image.PNG
而我自己创建项目的应用名称是 tsf-provider, 所以我的 docker 配置是 /docker-${project.artifactId}
- <plugin>
- <groupId>com.spotify</groupId>
- <artifactId>dockerfile-maven-plugin</artifactId>
- <version>1.3.6</version>
- <configuration>
- <repository>ccr.ccs.tencentyun.com/tsf_< 账号 ID>/docker-${project.artifactId}</repository>
- <buildArgs>
- <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
- </buildArgs>
- <tag>${project.version}</tag>
- </configuration>
- </plugin>
然后我们要推送这个镜像, 我尝试过用配置 DOCKER_HOST 的方式推送, 没有成功, 所以这里我用的手动档推送:
在项目下执行指令:
mvn package org.springframework.boot:spring-boot-maven-plugin:2.2.5.RELEASE:build-info dockerfile:build
在命令行窗口查看镜像, 可看到对应的镜像:
docker images
登录远程 docker 仓库, 并将对应的镜像推送到远程
docker login --username=< 账号 ID> ccr.ccs.tencentyun.com docker push ccr.ccs.tencentyun.com/tsf_< 账号 ID>/< 项目名 >:tag
推送后就能在自己的控制台查看了.
然后配置命名空间:
image.PNG
配置日志配置, 这个日志的路径是之前 run.sh 脚本中写的相关信息:
image.PNG
接下来我们需要建一个部署组:
- image.PNG
- image.PNG
接下来是对需要部署的应用的配置:
image.PNG
还记得我们前面说过我们要对外把端口暴露出来供外部访问吗? 这里也要配置好:
image.PNG
最后点击提交然后部署应用. 通过 更多 - 查看日志可以查看你应用的运行日志:
image.PNG
通过负载均衡的 ip 可以访问到你的容器服务:
image.PNG
比如我这里输入负载均衡 ip:8081 就返回了 springboot 的默认 404 页面:
image.PNG
实际开发中, 我们这样应用的端口不应该暴露在外部, 只能通过网关去转发, TSF 也是完全支持的, 这里只是为了实验才这么操作.
结语
TSF 的使用到这就介绍完了, 小伙伴们做完实验记得注销 云主机的实例哦:
image.PNG
TSF 第一次使用配置会多一点, 后续的话就会很方便, 而且 TSF 平台提供了以下服务:
TSF 调用链查询
TSF 弹性伸缩
TSF 配置管理
TSF 分布式事务
服务鉴权
服务限流
服务路由及灰度部署
由于篇幅问题, 这里就不做太多介绍了, 感兴趣的小伙伴可以上官方文档中去探索.
来源: https://www.qcloud.com/developer/article/1809505