前言
在上一篇文章《Soul网关源码学习(4)- Sofa代理的配置和测试》中,我们学习了如何使用 soul 网关做 Sofa 服务代理,同时还通过 Postman 做了一些简单的批量测试。那么这一篇,我们开始学习如何使用 soul 去代理 Spring Cloud 服务。
配置
Soul 网关配置
检查 soul-bootstrap 下的 pom 依赖,这里Spring Cloud 注册中心以 Eureka 为例,如果你使用的是其他的注册中心,换成该注册中心的相关依赖和配置。
<!--soul springCloud plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-springcloud</artifactId>
<version>${last.version}</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-httpclient</artifactId>
<version>${last.version}</version>
</dependency>
<!--soul springCloud plugin end-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<!--Eureka 注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
在 soul-bootstrap 的 yaml 配置文件中,添加 eureka 连接信息:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
Spring Cloud 服务配置
在你的 spring cloud 服务的 pom 中添加以下依赖:
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-client-springcloud</artifactId>
<version>${last.version}</version>
</dependency>
Application.yaml 添加如下配置:
soul:
springcloud:
admin-url: http://localhost:9095
context-path: /springcloud
full: true
# adminUrl: 为你启动的soul-admin 项目的ip + 端口,注意要加http://
# contextPath: 为你的这个mvc项目在soul网关的路由前缀,这个你应该懂意思把? 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
# full: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller
如果你想让你的接口自动注册上 soul-admin,需要在你的Controller 层的接口添加上 @SoulSpringCloudClient 注解,这个和第二篇文章中介绍 Http 代理 是一样的。这样你启动 Spring Cloud 服务之后,就可以直接在 soul-admin 页面看到代理的接口信息了。
你可以把注解加到 Controller 类上面, 里面的path属性则为前缀,如果含有 /** 代表你的整个接口需要被网关代理。
例子1: 接口 /test/payment, /test/findByUserId 都会被网关代理。
```java
@RestController
@RequestMapping("/test")
@SoulSpringCloudClient(path = "/test/**")
public class HttpTestController {
@PostMapping("/payment")
public UserDTO post(@RequestBody final UserDTO userDTO) {
...
}
@GetMapping("/findByUserId")
public UserDTO findByUserId(@RequestParam("userId") final String userId) {
...
}
}
```
例子2:接口 /order/save 会被网关代理,而 /order/findById 则不会。
@RestController
@RequestMapping("/order")
@SoulSpringCloudClient(path = "/order")
public class OrderController {
@PostMapping("/save")
@SoulSpringMvcClient(path = "/save")
public OrderDTO save(@RequestBody final OrderDTO orderDTO) {
...
}
@GetMapping("/findById")
public OrderDTO findById(@RequestParam("id") final String id) {
...
}
}
Spring Cloud 代理测试
启动 soul-admin,登录 Soul Admin 开启 Spring Cloud 插件
启动 Spring Cloud 测试服务器
还是和之前的几篇文章一样,使用源码目录 soul/soul-examples 下的测试服务器。
首先启动 soul-examples-eureka 注册中心,登录 localhost:8761。
接着我们启动 soul-examples-springcloud。不过在启动前,我们先参考上面的配置说明,检查 pom 依赖和 Application.yaml 是否正确,确认无误后再启动服务。然后登录Admin -> PluginList -> SpringCloud,可以看到接口已经注册到控制台。
启动Soul网关
源码 soul 服务模块 soul-bootstrap 的 spring cloud 相关依赖是由可能被注释掉的,所以需要参考上面的配置说明,添加相应的 pom 依赖,同时检查application.yaml,然后重启网关服务。
postman 简单测试
spring cloud 对外提供的本来就是 Http 服务,所以在访问方式上没有任何变化,只是地址由原来的 spring cloud 的服务转移到网关服务,同时 soul 网关需要有一个路由前缀,这个路由前缀就是你接入项目进行配置的 contextPath,这里是"/springcloud"。
测试接口:
@GetMapping("/findById")
@SoulSpringCloudClient(path = "/findById")
public OrderDTO findById(@RequestParam("id") final String id) {
OrderDTO orderDTO = new OrderDTO();
orderDTO.setId(id);
orderDTO.setName("hello world spring cloud findById");
return orderDTO;
}
测试结果:
postman 批量并发 + 断言测试
测试接口:和上面一样使用 findById。
Postman Request:
- url:localhost:9195/springcloud/order/findById?id={ {id}}
- 局部变量:
var id = data.id;
- 断言脚本:
pm.test("Status code is 200", function () { //响应码 pm.response.to.have.status(200); }); pm.test("response id check", function () { var jsonData = pm.response.json(); //返回data.id是否和参数id一致 pm.expect(jsonData.id).to.eql(pm.variables.get("id").toString()); });
同时并发1000个请求:
测试结果:
{
"id": "0fb1c823-d5cf-446c-87b5-5967dda1fa54",
"name": "soul-springcloiud",
"timestamp": "2021-01-19T12:17:07.263Z",
"collection_id": "843047cf-1b72-4261-a54a-7e533d872df4",
"folder_id": 0,
"environment_id": "0",
# 测试全部通过
"totalPass": 2000,
"totalFail": 0,
# 平均响应延迟是5ms左右
"count": 1000,
"totalTime": 5164,
"collection": {
"requests": [
{
"id": "043954f4-137a-409f-9135-a18a51ea345e",
"method": "GET"
}
]
}
}
结语
本篇文章中,我们学习了如何使用 soul 来代理 Spring Cloud 服务,同时通过 soul 源码下的测试用例,对 soul 网关进行了一些简单的压测。到这里 soul 对于几个常用分布式调用协议的代理是如何使用的,就介绍完毕了。后面会继续介绍 soul 的一些企业级功能插件是如何使用的,同时也会包括一些源码方面的分析。