操作场景
本文介绍在本地开发 Java 应用,通过 Spring Cloud的方式接入 TSE 治理中心(北极星网格),并实现服务路由。
前提条件
1. 在开发前,请确保您已经参见 下载 Maven下载安装了 Java 和 Maven。
2. 已创建 TSE 治理中心(北极星网格)实例,操作步骤详见 引擎管理。
操作步骤
步骤1:引入依赖
1.1 引入 spring cloud tencent 依赖
修改应用根目录下的 pom.xml,添加
dependencyManagement
:<dependencyManagement><dependencies><dependency><groupId>com.tencent.cloud</groupId><artifactId>spring-cloud-tencent-dependencies</artifactId><version>${version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
注意:
请根据项目 Spring Boot 和 Spring Framework 的版本,选择合适的 Spring Cloud Tencent 版本。
服务路由在 1.5.0版本之后,提供了完善的服务路由能力。所以版本号需大于1.5.0。
1.2 引入 spring cloud tencent starter
方式一:同时引入
spring-cloud-starter-tencent-polaris-discovery
和 spring-cloud-starter-tencent-polaris-router
。<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-router</artifactId></dependency>
方式二:通过
spring-cloud-starter-tencent-all
引入 sct 所有 starters。<dependency><groupId>com.tencent.cloud</groupId><artifactId>spring-cloud-starter-tencent-all</artifactId><!-- 注意需要指定 type=pom--><type>pom</type></dependency>
如果使用 Spring Cloud 2020 及以后版本,还需要添加如下依赖:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency>
步骤2:添加北极星配置文件
1. 在项目的 main/resources 目录下创建 bootstrap.yml 文件。
2. 在 bootstrap.yml 文件中配置应用名、polaris(北极星)服务端地址等信息。服务端地址查看详见 引擎管理 的客户端访问地址部分。
spring:application:name: ${application.name}cloud:polaris:address: grpc://${修改为 Polaris 服务地址}:8091namespace: default
步骤3:路由规则下发