Soul 網(wǎng)關(guān)使用 - divide 插件

Soul 提供了一系列方便易用的插件用于支持不同的協(xié)議,如 http、dubblo 等。我們可以在項(xiàng)目源碼下找到 soul-examples 測(cè)試這些插件的使用,本文我們關(guān)注用于支持 http 的 divide 插件。

divide 插件是進(jìn)行 http 正向代理的插件,所有 http 類(lèi)型的請(qǐng)求,都是由該插件進(jìn)行負(fù)載均衡的調(diào)用。
下面我們先使用一下 divide 插件,從應(yīng)用層看下它是如何工作的。

啟動(dòng)測(cè)試環(huán)境

這里我們要分別啟動(dòng) admin、bootstrap 和 soul-examples-http,admin 和 bootstrap 的啟動(dòng)可以參考前文:源碼編譯和簡(jiǎn)單使用
找到 soul-examples-http 項(xiàng)目,我們可以使用 IDEA 這種集成化開(kāi)發(fā)環(huán)境運(yùn)行,也可以使用 maven 打包后運(yùn)行。因?yàn)檫@里我暫時(shí)不調(diào)試代碼,所以直接使用 maven 打包使用。

cd $SOUL/soul-exampls/soul-examples-http/
# 打包 soul-examples-http
mvn package -Dmaven.javadoc.skip=true -Dmaven.test.skip=true
# 啟動(dòng) soul-examples-http 服務(wù)
java -jar target/soul-examples-http.jar

通過(guò) log 和 admin 管理平臺(tái)看做了哪些事情

soul-examples-http 服務(wù)啟動(dòng)時(shí),我們可以通過(guò) log 看到它以 http client 的形式向 admin 注冊(cè)了一些規(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}

通過(guò)日志我們可以看到 soul-examples-http 服務(wù)啟動(dòng)時(shí)向 Soul 網(wǎng)關(guān)一共注冊(cè)了 5 組規(guī)則用于 http 請(qǐng)求轉(zhuǎn)發(fā)。這些規(guī)則我們同樣可以在 admin 的管理控制臺(tái)看到。


Soul 網(wǎng)關(guān)管理控制臺(tái)界面

通過(guò)網(wǎng)關(guān)訪問(wèn) soul-examples-http 的接口

通過(guò)查看 soul-examples-http 源碼中的 controller 代碼,我們可以看到 soul-examples-http 提供了哪些接口,接下來(lái)我們通過(guò)網(wǎng)關(guān)訪問(wèn)這些接口。
Soul 網(wǎng)關(guān)的訪問(wèn)地址是: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,當(dāng)服務(wù)啟動(dòng)時(shí),soul-examples-http 以客戶端的形式向 Soul 網(wǎng)關(guān)的 admin 發(fā)起注冊(cè)請(qǐng)求,我們通過(guò)配置文件可以看到 Soul 網(wǎng)關(guān) 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 網(wǎng)關(guān)注冊(cè)哪些規(guī)則,注解可以注冊(cè)到 controller 類(lèi)上面,用于匹配 controller 類(lèi)下面的所有接口:

@RestController
@RequestMapping("/test")
@SoulSpringMvcClient(path = "/test/**")
public class HttpTestController {
  ...
}

也可以注冊(cè)到接口的方法上面,用于注冊(cè)特定的某一個(gè)接口:

@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;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容