一、前提条件
·在开始开发前,请确保你已经参见 下载 Maven 下载安装了 Java 和 Maven,并且配置了 TSF 私服地址。
注意:说明:
·已参见 微服务网关 文档熟悉了微服务网关的功能。
二、快速上手
使用微服务网关功能前,你需要在pom.xml中添加网关依赖项,同时在代码中使用网关开关注解。你也可以参见 TSF Demo 中 msgw-demo 模块来开发。
三、1.22.0-Series-RELEASE 以及之后版本
1. 向工程中添加依赖,在 pom.xml 文件中添加以下代码。
<dependency> <groupId>com.tencent.tsf</groupId> <artifactId>spring-cloud-tsf-msgw-zuul</artifactId> <version><!– 调整为 SDK 最新版本号 –></version> </dependency>
2. 向 Application 类中添加注解 @EnableZuulProxy 和 SpringBootApplication:
// 下面省略了无关的代码import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@EnableZuulProxy@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
3. 创建分组,导入 API。具体请参见 微服务网关分组管理。
四、1.22.0-Edgware-RELEASE/1.22.0-Finchley-RELEASE 之前的版本
1. 向工程中添加依赖,在 pom.xml 文件中添加以下代码。
<dependency> <groupId>com.tencent.tsf</groupId> <artifactId>spring-cloud-tsf-gateway</artifactId> <version><!– 1.22.0 之前版本 –></version> </dependency>
2. 向 Application 类中添加注解 @EnableTsfGateway。
// 下面省略了无关的代码import com.tencent.tsf.gateway.core.annotation.EnableTsfGateway;@EnableTsfGatewaypublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
3. 创建分组,导入 API。具体请参见 微服务网关分组管理。
五、集成 TSF 其它功能
你在使用微服务网关功能的同时,还可以集成 TSF 其它功能,包括分布式配置、服务监控、服务治理等,你需要在 pom.xml 中添加其对应依赖项,同时在代码中启用注解,具体参见其 功能接入文档。以下示例为集成所有功能:
1. 向工程中增加依赖。在 pom.xml 中按顺序添加以下代码:
<dependency> <groupId>com.tencent.tsf</groupId> <artifactId>spring-cloud-tsf-msgw-zuul</artifactId> <version><!– 调整为 SDK 最新版本号 –></version> </dependency><!–TSF 其它 SDK 依赖,添加到 msgw-zuul 依赖的后面–><dependency> <groupId>com.tencent.tsf</groupId> <artifactId>spring-cloud-tsf-starter</artifactId> <version><!– 调整为 SDK 最新版本号 –></version> </dependency>
2. 向 Application 类中增加注解 @EnableTsf:
// 下面省略了无关的代码import com.tencent.tsf.gateway.core.annotation.EnableTsfGateway;import org.springframework.tsf.annotation.EnableTsf;@EnableZuulProxy@SpringBootApplication@EnableTsfpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
六、自定义网关过滤器
msgw-zuul SDK 提供通过继承TsfGatewayZuulFilter(或原生ZuulFilter)的方式自定义 Filter,同时需要在类上添加@TsfGatewayFilter注解(或其它生成Bean方式)。目前 msgw-zuul 基于 zuul1 实现,其 Filter 功能和原生 ZuulFilter 保持一致。
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;import com.netflix.zuul.exception.ZuulException;import com.tencent.tsf.gateway.core.annotation.TsfGatewayFilter;import com.tencent.tsf.gateway.zuul1.filter.TsfGatewayZuulFilter;@TsfGatewayFilterpublic class TestFilter extends TsfGatewayZuulFilter { @Override public String filterType() { return PRE_TYPE; } @Override public int filterOrder() { return 100; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { System.out.println("hello world"); return null; }}
七、快速上手
使用微服务网关功能前,你需要在pom.xml中添加网关依赖项,同时在代码中使用网关开关注解。或参见 TSF Demo 中 msgw-demo 模块来编写。
1. 向工程中添加依赖。在 pom.xml 中添加以下代码:
<dependency> <groupId>com.tencent.tsf</groupId> <artifactId>spring-cloud-tsf-msgw-scg</artifactId> <version><!– 调整为 SDK 最新版本号 –></version> </dependency>
2. 向 Application 类中添加注解 @SpringBootApplication:
// 下面省略了无关的代码import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
3. 创建分组,导入 API。具体请参见 微服务网关分组管理。
八、集成 TSF 其它功能
你在使用微服务网关功能的同时,还可以集成 TSF 其它功能,包括分布式配置、监控、服务治理等,你需要在pom.xml中添加其对应依赖项,同时在代码中启用注解,具体参见其 功能接入文档。以下示例为集成所有功能:
1. 向工程中增加依赖(scg 在1.26.0才支持服务治理功能,1.26.0以前的 SDK 版本添加 spring-cloud-tsf-starter 会报错)。在 pom.xml 中按顺序添加以下代码:
<dependency> <groupId>com.tencent.tsf</groupId> <artifactId>spring-cloud-tsf-msgw-scg</artifactId> <version><!– 调整为 SDK 最新版本号 –></version></dependency><!–TSF 其它 SDK 依赖,添加到 msgw-scg 依赖的后面–><dependency> <groupId>com.tencent.tsf</groupId> <artifactId>spring-cloud-tsf-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> <exclusion> <groupId>com.tencent.tsf</groupId> <artifactId>spring-cloud-tsf-swagger</artifactId> </exclusion> </exclusions></dependency>
2. 向 Application 类中增加注解 @EnableTsf:
// 下面省略了无关的代码import org.springframework.tsf.annotation.EnableTsf;@SpringBootApplication@EnableTsfpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
九、自定义网关过滤器
msgw-scg SDK 提供通过继承AbstractTsfGlobalFilter的方式,或通过实现原生GlobalFilter 接口的方式自定义 Filter。同时需要在类上添加@TsfGatewayFilter注解(或其它生成 Bean 方式)。AbstractTsfGlobalFilter 提供了和编写 ZuulFilter 类似的体验:
// 下面省略了无关的代码@TsfGatewayFilterpublic class TestFilter extends AbstractTsfGlobalFilter { @Override public int getOrder() { return 100; } @Override public boolean shouldFilter(ServerWebExchange exchange, GatewayFilterChain chain) { return true; } @Override public Mono<Void> doFilter(ServerWebExchange exchange, GatewayFilterChain chain) { System.out.println("hello world"); return null; }}