使用Spring Cloud

本文主要面向Spring Cloud微服务架构项目的开发者,介绍如何使用spring-cloud-tencent以帮助开发者在其软件工程项目中快速接入Polaris,以使用其功能,包括服务注册与发现、服务路由与负载均衡、故障节点熔断和服务限流。其中,每个功能对应的子模块可以独立使用。

  • 服务注册与发现:基于 Spring Cloud Common的标准进行微服务的注册与发现
  • 服务路由与负载均衡:基于 Ribbon 的接口标准,提供场景更丰富的动态路由以及负载均衡的能力
  • 故障节点熔断:提供故障节点的熔断剔除以及主/被动探测恢复的能力,保证分布式服务的可靠性
  • 服务限流:支持工作于微服务被调接入层的限流功能,保证后台微服务稳定性,可通过控制台动态配置规则,及查看流量监控数据

环境准备

准备编译运行环境

所有步骤建议执行在JDK/JRE 1.8+的运行环境上。

准备Polaris后端环境

在进行实际的开发前,开发者需要确保Polaris后端服务正常运行,详细操作请参考Polaris使用Spring Cloud - 图1 (opens new window)

快速接入

引入依赖管理

在工程根目录的pom中的<dependencyManagement>添加如下配置,即可在项目中引用需要的spring-cloud-tencent子模块依赖。

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>com.tencent.cloud</groupId>
  5. <artifactId>spring-cloud-tencent-dependencies</artifactId>
  6. <!--版本号需修改成实际依赖的版本号-->
  7. <version>${version}</version>
  8. <type>pom</type>
  9. <scope>import</scope>
  10. </dependency>
  11. </dependencies>
  12. </dependencyManagement>

建议使用最新的release版本,版本说明参考:Spring Cloud Tencent版本说明使用Spring Cloud - 图2 (opens new window)

服务注册与发现

完整样例代码参考:服务注册与发现Example使用Spring Cloud - 图3 (opens new window)

  1. 添加依赖

在项目中加入spring-cloud-starter-tencent-polaris-discovery依赖即可使用Polaris的服务注册与发现功能。如Maven项目中,在pom中添加如下配置:

  1. <dependency>
  2. <groupId>com.tencent.cloud</groupId>
  3. <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
  4. </dependency>
  1. 修改配置

在服务生产者和服务消费者的配置文件(bootstrap.yml、application.yml等)中添加如下配置,即可完成服务注册与发现(在Spring Cloud Edgware之后,无需使用@EnableDiscoveryClient即可进行服务注册与发现):

  1. spring:
  2. application:
  3. name: ${application.name}
  4. cloud:
  5. polaris:
  6. address: ${protocol}://${ip}:${port}

其中,${application.name}为服务名,与Polaris控制台上的服务名保持一致。${protocol}为数据传输协议,目前仅支持grpc。${ip}为Polaris后端ip地址,${port}为Polaris后端暴露的端口号。

  1. 启动并验证

服务注册与发现Example使用Spring Cloud - 图4 (opens new window)为例。

  • Feign调用

执行以下命令发起Feign调用,其逻辑为服务生产者返回value1+value2的和

  1. curl -L -X GET 'http://localhost:48080/discovery/service/caller/feign?value1=1&value2=2'

预期返回值

  1. 3
  • RestTemplate调用

执行以下命令发起RestTemplate调用,其逻辑为服务生产者返回一段字符串

  1. curl -L -X GET 'http://localhost:48080/discovery/service/caller/rest'

预期返回值

  1. Discovery Service Callee

服务路由与负载均衡

  1. 添加依赖

在项目中加入spring-cloud-starter-tencent-polaris-router依赖即可使用服务路由与负载均衡的特性。如Maven项目中,在pom中添加如下配置:

  1. <dependency>
  2. <groupId>com.tencent.cloud</groupId>
  3. <artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>
  4. </dependency>
  1. 添加路由或负载均衡配置

Polaris提供了服务路由配置和元数据配置。其中服务路由配置用于服务路由的判定,元数据配置内的权重配置可以用于负载均衡的权重相关策略使用(目前仅支持权重随机负载均衡策略)。详细操作请参考北极星服务服务路由文档使用Spring Cloud - 图5 (opens new window)

  1. 启动并验证

对服务消费者发生调用时,按照用户配置的路由或者权重对服务生产者实例有不同的路由调用。

故障节点熔断

完整样例代码参考:故障节点熔断Example使用Spring Cloud - 图6 (opens new window)

  1. 添加依赖

在项目中加入spring-cloud-starter-tencent-polaris-circuitbreaker依赖即可使用故障熔断的特性。如Maven项目中,在pom中添加如下配置:

  1. <dependency>
  2. <groupId>com.tencent.cloud</groupId>
  3. <artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
  4. </dependency>
  1. 启动并验证

故障节点熔断Example使用Spring Cloud - 图7 (opens new window)为例。

  • Feign调用

执行以下命令发起Feign调用,其逻辑为服务生产者抛出一个异常

  1. curl -L -X GET 'localhost:48080/example/service/a/getBServiceInfo'

预期返回情况:

在出现

  1. trigger the refuse for service b

时,表示请求到有异常的ServiceB,需要熔断这个实例。后面的所有请求都会得到正常返回。

服务限流

完整样例代码参考:服务限流Example使用Spring Cloud - 图8 (opens new window)

  1. 添加依赖

在项目中加入spring-cloud-starter-tencent-polaris-ratelimit依赖即可使用服务限流的特性。如Maven项目中,在pom中添加如下配置:

  1. <dependency>
  2. <groupId>com.tencent.cloud</groupId>
  3. <artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId>
  4. </dependency>
  1. 启动并验证

通过浏览器访问http://127.0.0.1:48081/business/invoke,可以看到以下输出信息:

  1. hello world for ratelimit service 1
  2. hello world for ratelimit service 2
  3. hello world for ratelimit service 3
  4. ...

北极星提供了三种添加限流配置的方式,包括控制台操作、HTTP接口上传和本地文件配置,具体请参考北极星服务限流使用文档使用Spring Cloud - 图9 (opens new window)

例如调用HTTP接口进行配置。通过以下命令来配置:

  1. curl -X POST -H "Content-Type:application/json" 127.0.0.1:8090/naming/v1/ratelimits -d @rule.json

继续访问http://127.0.0.1:48081/business/invoke,可以看到,10次调用后,就开始被限流:

  1. hello world for ratelimit service 1
  2. hello world for ratelimit service 2
  3. hello world for ratelimit service 3
  4. ...
  5. hello world for ratelimit service 10
  6. request has been limited, service is RateLimitCalleeService, path is /business/invoke, 11

相关链接

Polaris使用Spring Cloud - 图10 (opens new window)

Spring Cloud Tencent使用Spring Cloud - 图11 (opens new window)