# layne-cloud-tsf
**Repository Path**: dht5867/layne-cloud-tsf
## Basic Information
- **Project Name**: layne-cloud-tsf
- **Description**: 腾讯云微服务平台tsf简单使用
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 2
- **Forks**: 1
- **Created**: 2020-11-06
- **Last Updated**: 2025-02-21
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 腾讯微服务平台-TSF #
## 产品概述 ##
腾讯微服务平台 (Tencent Service Framework,TSF) 是一个围绕着 应用 和微服务的 PaaS 平台,提供应用全生命周期管理、数据化运营、立体化监控和服务治理等功能。TSF 拥抱 Spring Cloud 、Service Mesh 微服务框架,帮助企业客户解决传统集中式架构转型的困难,打造大规模高可用的分布式系统架构,实现业务、产品的快速落地。
TSF 以腾讯云中间件团队多款成熟的分布式产品为核心基础组件,提供秒级推送的分布式配置服务、链路追踪等高可用稳定性组件。此外,TSF 与腾讯云 API 网关和消息队列打通,让企业轻松构建大型分布式系统。
## 产品功能 ##
- 服务注册与发现
- 细粒度的服务治理
- 全面的应用生命周期管理
- 高可靠的配置中心
- 可视化应用运维
- 分布式事务
- 分布式任务调度
## 产品优势 ##
| 优势项 | TSF 服务治理平台 | 自建服务治理平台 |
| :----- | :----- | :----- |
| 服务注册中心 | 平台提供高可用的注册中心集群,金融级别容灾 | 自行搭建和维护 |
| 调用链能力 | 符合国人习惯的交互方式,与日志服务联动 | 使用开源组件,英文界面 |
| 应用部署 | 成熟、灵活的部署方案,支持灰度发布等高级能力 | 自行开发部署模块 |
| 日志服务 | 提供采集、呈现、解析、检索的一站式能力,帮助开发者快速定位目标日志 | 基于 ELK 等开源组件搭建 |
| 配置管理 | 分布式配置,支持从环境、版本、应用三个维度进行管理,配置动态推送和历史推送回溯能力 | 自行搭建和维护 |
| 服务治理 | 支持细粒度的服务路由、限流、鉴权功能,控制台进行可视化配置 | 自行搭建和维护 |
| 分布式事务 | 提供分布式事务解决方案,经腾讯内部多产品实践验证 | 使用开源或自行开发 |
| 分布式任务调度 | 提供分布式任务调度的解决方案,通过控制台即可配置、管理定时调度任务 | 自行搭建和维护 |
| 微服务 API 网关 | 微服务 API 网关提供微服务网关能力,支持配置鉴权、限流等策略 | 基于 Zuul 等组件开发 |
| 与腾讯云服务整合 | 与腾讯云服务深度整合 | 基于云 API 开发 |
| 售后服务 | 提供稳定及时的售后服务,强大的技术、运维团队支持 | 企业运维团队支持 |
## Spring Cloud应用接入 ##
### Spring Cloud概述 ###
Spring Cloud是基于Spring Boot实现微服务的框架,包含多个子项目的整体方案。
| 组件 | 用途 |
| :----- | :----- |
| Eureka/Consul | 服务注册中心 |
| Ribbon | 客户端负载均衡,特性有区域亲和、重试机制 |
| Feign | 声明式服务调用,本质上就是Ribbon+Hystrix |
| Hystrix | 客户端容错保护,特性有服务降级、服务熔断、请求缓存、请求合并、依赖隔离。 |
| Config | 分布式配置中心,支持本地仓库,SVN、GIT、JAR包内配置等模式 |
| Zuul/Gateway | API服务网关,功能有路由分发和过滤 |
| Bus | 消息总线 |
### 快速入门 ###
#### 开发准备 ####
1. 安装了Java和Maven
2. 配置了TSF私服地址
Maven配置TSF私服地址:
nexus
central
http://repo1.maven.org/maven2
true
true
central
http://repo1.maven.org/maven2
true
true
qcloud-repo
qcloud-central
qcloud mirror central
http://mirrors.cloud.tencent.com/nexus/repository/maven-public/
true
true
qcloud-plugin-central
http://mirrors.cloud.tencent.com/nexus/repository/maven-public/
true
true
nexus
qcloud-repo
#### 获取Demo ####
[Demo下载地址](https://github.com/tencentyun/tsf-simple-demo)
Demo工程目录:
|- consumer-demo:服务消费者
|- provider-demo:服务提供者
|- scg-demo:基于开源 Spring Cloud Gateway 的微服务网关示例(不适用于 Edgware 版本 )
|- zuul-demo:基于开源 Zuul 的微服务网关示例
|- cmq-producer-demo:CMQ 消息队列消息生产者示例
|- cmq-consumer-demo:CMQ 消息队列消息消费者示例
|- kafka-demo:Kafka 消息队列示例,包含了消息消费者和生产者
|- mongodb-demo:调用 MongoDB 的微服务示例
|- mysql-demo:调用 MySQL 的微服务示例
|- redis-demo:调用 Redis 的微服务示例
|- msgw-demo:基于 TSF SDK 的微服务网关示例
|- pom.xml
#### 搭建示例Demo ####
**新建一个demo maven 工程**
新建一个maven工程,在pom.xml文件中定义工程需要的依赖包
4.0.0
com.tencent.tsf
spring-cloud-tsf-dependencies
1.23.0-Greenwich-RELEASE
top.layne.tsf
layne-cloud-tsf
1.0-SNAPSHOT
pom
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
UTF-8
1.8
1.8
**新建一个提供者模块**
1. 新建一个provider模块,pom.xml 配置:
layne-cloud-tsf
top.layne.tsf
1.0-SNAPSHOT
4.0.0
layne-cloud-tsf-provider
jar
com.tencent.tsf
spring-cloud-tsf-starter
2. 新建启动类
package top.layne.cloud.tsf.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.tsf.annotation.EnableTsf;
/**
* @author : layne
* @Date: 2020/7/24
* @Time: 14:39
* Description:
*/
@SpringBootApplication
@EnableFeignClients //使用feign微服务调用时启用
@EnableTsf
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
3. 新建一个echo服务
package top.layne.cloud.tsf.provider.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @author : layne
* @Date: 2020/7/24
* @Time: 14:41
* Description:
*/
@RestController
public class EchoController {
@GetMapping("/echo/{message}")
public String echo(@PathVariable String message) {
return message;
}
}
4. 新建配置文件application.yml
server:
port: 18081
spring:
application:
name: layne-cloud-tsf-provider
**新建一个消费者模块**
1. 添加consumer模块,pom.xml引入配置
layne-cloud-tsf
top.layne.tsf
1.0-SNAPSHOT
4.0.0
layne-cloud-tsf-consumer
jar
com.tencent.tsf
spring-cloud-tsf-starter
2. 添加启动类
package top.layne.cloud.tsf.consumer;
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;
/**
* @author : layne
* @Date: 2020/7/24
* @Time: 14:54
* Description:
*/
@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) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
3. 添加EchoService服务
package top.layne.cloud.tsf.consumer.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author : layne
* @Date: 2020/7/24
* @Time: 14:59
* Description:
*/
@FeignClient("layne-cloud-tsf-provider")
public interface EchoService {
@GetMapping("/echo/{message}")
String echo(@PathVariable("message") String message);
}
4. 添加controller
package top.layne.cloud.tsf.consumer.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import top.layne.cloud.tsf.consumer.service.EchoService;
/**
* @author : layne
* @Date: 2020/7/24
* @Time: 15:02
* Description:
*/
@RestController
public class EchoController {
private static final Logger LOGGER = LoggerFactory.getLogger(EchoController.class);
@Autowired
private EchoService echoService;
@Autowired
private RestTemplate restTemplate;
@GetMapping("/echo-feign/{message}")
public String feign(@PathVariable("message") String message) {
LOGGER.info(">>>>>echo-feign :{}", message);
return echoService.echo(message);
}
@GetMapping("/echo-rest/{message}")
public String rest(@PathVariable("message") String message) {
LOGGER.info(">>>>>echo-rest :{}", message);
return restTemplate.getForObject("http://layne-cloud-tsf-provider/echo/" + message, String.class);
}
}
5. 添加配置文件application.yml
server:
port: 18083
spring:
application:
name: layne-cloud-tsf-consumer
#### 搭建轻量级服务注册中心 ####
1. 下载Consul作为轻量级微服务注册中心,[下载地址](https://www.consul.io/downloads.html)
确保机器以下的端口是空闲的:8300, 8301, 8302, 8500, 8600。
用户可通过执行如下命令查看端口占用信息 netstat -apn|grep LISTEN
2. 启动轻量级微服务注册中心
Windows操作系统:
.\consul.exe agent -dev
Linux / macOS 操作系统:
./consul agent -dev
3. 验证服务注册中心启动
通过浏览器查看服务注册中心页面(http://127.0.0.1:8500)。
#### 启动工程 ####
1. 启动服务注册中心
2. 启动提供者服务
3. 启动消费者服务
4. 浏览器输入地址访问