Soul-源码解析2-接入http应用

在上一节对Soul做了一个概述,安装部署了一个简单的单机版,
补充上一节目录结构:

  • soul-admin: soul 插件元数据配置管理服务,内部包含了soul-dashboard交互页面
  • soul-bootstrap: soul 网关启动程序入口

今天计划:

  • 运行examples下面的 http服务
  • 结合divde插件,发起http请求soul网关,体验http代理

准备工作

  1. 启动soul-admin
    登录 Admin 后台 http://localhost:9095 进入「系统管理 -> 插件管理」菜单,看到 divide 插件,默认是开启的。
  2. 启动 soul-bootstrap 应用
    通过上1节我们知道看到这个日志知道已经和soul-admin建立好websocket连接
2021-01-15 19:20:56.200  INFO 13568 --- [0.0-9095-exec-2] o.d.s.a.l.websocket.WebsocketCollector   : websocket on open successful....

运行examples下面的 http服务

  1. 启动 soul-examples-http 应用,该应用演示了 Soul 如何快速接入 HTTP 应用
    在这里插入图片描述
    http服务注册成功,启动成功日志:
2021-01-15 19:27:17.119  INFO 8028 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils  : http client register success: {"appName":"http","context":"/http","path":"/http/test/**","pathDesc":"","rpcType":"http","host":"192.168.101.146","port":8188,"ruleName":"/http/test/**","enabled":true,"registerMetaData":false} 
2021-01-15 19:27:17.165  INFO 8028 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils  : http client register success: {"appName":"http","context":"/http","path":"/http/order/save","pathDesc":"Save order","rpcType":"http","host":"192.168.101.146","port":8188,"ruleName":"/http/order/save","enabled":true,"registerMetaData":false} 
2021-01-15 19:27:17.207  INFO 8028 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils  : http client register success: {"appName":"http","context":"/http","path":"/http/order/findById","pathDesc":"Find by id","rpcType":"http","host":"192.168.101.146","port":8188,"ruleName":"/http/order/findById","enabled":true,"registerMetaData":false} 
2021-01-15 19:27:17.245  INFO 8028 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils  : http client register success: {"appName":"http","context":"/http","path":"/http/order/path/**","pathDesc":"","rpcType":"http","host":"192.168.101.146","port":8188,"ruleName":"/http/order/path/**","enabled":true,"registerMetaData":false} 
2021-01-15 19:27:17.287  INFO 8028 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils  : http client register success: {"appName":"http","context":"/http","path":"/http/order/path/**/name","pathDesc":"","rpcType":"http","host":"192.168.101.146","port":8188,"ruleName":"/http/order/path/**/name","enabled":true,"registerMetaData":false} 
2021-01-15 19:27:19.388  INFO 8028 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8188
  1. 之后,打开 Admin 后台,会看到如下配置信息
    在这里插入图片描述
  • 至此,所有的服务均已经启动完成,我们看下http-example的的ordercontroller
    在这里插入图片描述

验证通过网关请求接口数据是否一致

  • 请求http://127.0.0.1:8188/order/findById?id=15,得到返回数据
{
    "id": "15",
    "name": "hello world findById"
}
  • 请求http://127.0.0.1:9195/http/order/findById?id=15, 得到返回数据
    在这里插入图片描述

结合divde插件,再次启动一个 soul-examples-http 应用。查看负载均衡
在这里插入图片描述

修改权重
在这里插入图片描述

访问多次http://127.0.0.1:9195/http/order/findById?id=15 查看soul-bootstrap的日志输出

2021-01-15 20:04:17.980  INFO 13884 --- [-work-threads-1] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http
2021-01-15 20:04:17.980  INFO 13884 --- [-work-threads-1] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http/order/findById
2021-01-15 20:04:17.981  INFO 13884 --- [-work-threads-1] o.d.s.plugin.httpclient.WebClientPlugin  : The request urlPath is http://192.168.101.146:8190/order/findById?id=15, retryTimes is 0
2021-01-15 20:04:18.620  INFO 13884 --- [-work-threads-3] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http
2021-01-15 20:04:18.620  INFO 13884 --- [-work-threads-3] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http/order/findById
2021-01-15 20:04:18.620  INFO 13884 --- [-work-threads-3] o.d.s.plugin.httpclient.WebClientPlugin  : The request urlPath is http://192.168.101.146:8190/order/findById?id=15, retryTimes is 0
2021-01-15 20:04:19.309  INFO 13884 --- [-work-threads-5] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http
2021-01-15 20:04:19.310  INFO 13884 --- [-work-threads-5] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http/order/findById
2021-01-15 20:04:19.310  INFO 13884 --- [-work-threads-5] o.d.s.plugin.httpclient.WebClientPlugin  : The request urlPath is http://192.168.101.146:8190/order/findById?id=15, retryTimes is 0
2021-01-15 20:04:20.018  INFO 13884 --- [-work-threads-7] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http
2021-01-15 20:04:20.019  INFO 13884 --- [-work-threads-7] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http/order/findById
2021-01-15 20:04:20.019  INFO 13884 --- [-work-threads-7] o.d.s.plugin.httpclient.WebClientPlugin  : The request urlPath is http://192.168.101.146:8190/order/findById?id=15, retryTimes is 0
2021-01-15 20:04:20.796  INFO 13884 --- [-work-threads-9] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http
2021-01-15 20:04:20.796  INFO 13884 --- [-work-threads-9] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http/order/findById
2021-01-15 20:04:20.796  INFO 13884 --- [-work-threads-9] o.d.s.plugin.httpclient.WebClientPlugin  : The request urlPath is http://192.168.101.146:8190/order/findById?id=15, retryTimes is 0
2021-01-15 20:04:41.850  INFO 13884 --- [work-threads-10] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http
2021-01-15 20:04:41.850  INFO 13884 --- [work-threads-10] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http/order/findById
2021-01-15 20:04:41.851  INFO 13884 --- [work-threads-10] o.d.s.plugin.httpclient.WebClientPlugin  : The request urlPath is http://192.168.101.146:8190/order/findById?id=15, retryTimes is 0

权重改为各一半后查看
在这里插入图片描述

2021-01-15 20:08:56.591  INFO 13884 --- [work-threads-18] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http
2021-01-15 20:08:56.591  INFO 13884 --- [work-threads-18] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http/order/findById
2021-01-15 20:08:56.591  INFO 13884 --- [work-threads-18] o.d.s.plugin.httpclient.WebClientPlugin  : The request urlPath is http://192.168.101.146:8190/order/findById?id=15, retryTimes is 0
2021-01-15 20:08:57.392  INFO 13884 --- [work-threads-19] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http
2021-01-15 20:08:57.393  INFO 13884 --- [work-threads-19] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http/order/findById
2021-01-15 20:08:57.393  INFO 13884 --- [work-threads-19] o.d.s.plugin.httpclient.WebClientPlugin  : The request urlPath is http://192.168.101.146:8188/order/findById?id=15, retryTimes is 0

小结:符合 /http/** 的请求会随机分流到 8188 和 8190 端口

再次启动一个 soul-examples-http 应用8189端口,contextPath: /httpb。
访问:http://127.0.0.1:9195/httpb/order/findById?id=22

2021-01-15 20:12:58.997  INFO 13884 --- [-work-threads-3] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/httpb
2021-01-15 20:12:58.998  INFO 13884 --- [-work-threads-3] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/httpb/order/findById
2021-01-15 20:12:58.998  INFO 13884 --- [-work-threads-3] o.d.s.plugin.httpclient.WebClientPlugin  : The request urlPath is http://192.168.101.146:8189/order/findById?id=22, retryTimes is 0

由此可见,请求符合 /httpb/** 的请求则会分流到 8189 端口。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值