Soul 提供了一系列方便易用的插件用于支持不同的协议,如 http、dubblo 等。我们可以在项目源码下找到 soul-examples 测试这些插件的使用,本文我们关注用于支持 http 的 divide 插件。
divide 插件是进行 http 正向代理的插件,所有 http 类型的请求,都是由该插件进行负载均衡的调用。
下面我们先使用一下 divide 插件,从应用层看下它是如何工作的。
启动测试环境
这里我们要分别启动 admin、bootstrap 和 soul-examples-http,admin 和 bootstrap 的启动可以参考前文:源码编译和简单使用
找到 soul-examples-http 项目,我们可以使用 IDEA 这种集成化开发环境运行,也可以使用 maven 打包后运行。因为这里我暂时不调试代码,所以直接使用 maven 打包使用。
cd $SOUL/soul-exampls/soul-examples-http/
# 打包 soul-examples-http
mvn package -Dmaven.javadoc.skip=true -Dmaven.test.skip=true
# 启动 soul-examples-http 服务
java -jar target/soul-examples-http.jar
通过 log 和 admin 管理平台看做了哪些事情
soul-examples-http 服务启动时,我们可以通过 log 看到它以 http client 的形式向 admin 注册了一些规则,下面是这些 log:
...
http client register success: {"appName":"http","context":"/http","path":"/http/test/**","pathDesc":"","rpcType":"http","host":"192.168.0.103","port":8188,"ruleName":"/http/test/**","enabled":true,"registerMetaData":false}
http client register success: {"appName":"http","context":"/http","path":"/http/order/save","pathDesc":"Save order","rpcType":"http","host":"192.168.0.103","port":8188,"ruleName":"/http/order/save","enabled":true,"registerMetaData":false}
http client register success: {"appName":"http","context":"/http","path":"/http/order/path/**","pathDesc":"","rpcType":"http","host":"192.168.0.103","port":8188,"ruleName":"/http/order/path/**","enabled":true,"registerMetaData":false}
http client register success: {"appName":"http","context":"/http","path":"/http/order/path/**/name","pathDesc":"","rpcType":"http","host":"192.168.0.103","port":8188,"ruleName":"/http/order/path/**/name","enabled":true,"registerMetaData":false}
http client register success: {"appName":"http","context":"/http","path":"/http/order/findById","pathDesc":"Find by id","rpcType":"http","host":"192.168.0.103","port":8188,"ruleName":"/http/order/findById","enabled":true,"registerMetaData":false}
通过日志我们可以看到 soul-examples-http 服务启动时向 Soul 网关一共注册了 5 组规则用于 http 请求转发。这些规则我们同样可以在 admin 的管理控制台看到。
通过网关访问 soul-examples-http 的接口
通过查看 soul-examples-http 源码中的 controller 代码,我们可以看到 soul-examples-http 提供了哪些接口,接下来我们通过网关访问这些接口。
Soul 网关的访问地址是:http://127.0.0.1:9195
curl -X GET \
http://127.0.0.1:9195/http/test/path/abc\?name\=yiwenlong
# result:
# {"userId":"abc","userName":"hello world"}
curl -X POST -H "Content-Type:application/json" -d '{"userId":"aaa","userName":"bbb"}' \
http://127.0.0.1:9195/http/test/payment
# result:
# {"userId":"aaa","userName":"bbb"}
curl -X PUT -H "Content-Type:application/json" -d '{"userId":"aaa","userName":"bbb"}' \
http://127.0.0.1:9195/http/test/putPathBody/aaa
# reuslut:
# {"userId":"aaa","userName":"hello world"}
soul-example-http 是如何配置的
soul-examples-http 依赖了 soul-spring-boot-starter-client-springmvc
,当服务启动时,soul-examples-http 以客户端的形式向 Soul 网关的 admin 发起注册请求,我们通过配置文件可以看到 Soul 网关 admin 的配置:
# soul-examples-http: application.yml
soul:
http:
adminUrl: http://localhost:9095
port: 8188
contextPath: /http
appName: http
full: false
@SoulSpringMvcClient
注解用于告诉 soul-spring-boot-starter-client-springmvc
需要向 Soul 网关注册哪些规则,注解可以注册到 controller 类上面,用于匹配 controller 类下面的所有接口:
@RestController
@RequestMapping("/test")
@SoulSpringMvcClient(path = "/test/**")
public class HttpTestController {
...
}
也可以注册到接口的方法上面,用于注册特定的某一个接口:
@RestController
@RequestMapping("/order")
@SoulSpringMvcClient(path = "/order")
public class OrderController {
/**
* Save order dto.
*
* @param orderDTO the order dto
* @return the order dto
*/
@PostMapping("/save")
@SoulSpringMvcClient(path = "/save" , desc = "Save order")
public OrderDTO save(@RequestBody final OrderDTO orderDTO) {
orderDTO.setName("hello world save order");
return orderDTO;
}
}