Soul 提供了一系列方便易用的插件用于支持不同的協(xié)議苫拍,如 http、dubblo 等。我們可以在項目源碼下找到 soul-examples 測試這些插件的使用绘迁,本文我們關注用于支持 http 的 divide 插件。
divide 插件是進行 http 正向代理的插件卒密,所有 http 類型的請求缀台,都是由該插件進行負載均衡的調用。
下面我們先使用一下 divide 插件哮奇,從應用層看下它是如何工作的膛腐。
啟動測試環(huán)境
這里我們要分別啟動 admin、bootstrap 和 soul-examples-http屏镊,admin 和 bootstrap 的啟動可以參考前文:源碼編譯和簡單使用
找到 soul-examples-http 項目依疼,我們可以使用 IDEA 這種集成化開發(fā)環(huán)境運行,也可以使用 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 注冊了一些規(guī)則误辑,下面是這些 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 組規(guī)則用于 http 請求轉發(fā)。這些規(guī)則我們同樣可以在 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 發(fā)起注冊請求,我們通過配置文件可以看到 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 網關注冊哪些規(guī)則赚导,注解可以注冊到 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;
}
}