任务
- 运行examples下面的 http服务
- 结合divde插件,发起http请求soul网关,体验http代理
- 其实前一篇文章已经写了通过soul转发成功了,所以探讨一下soul的接口注册流程。
数据库设计
soul的数据库设计图1所示,该图为官网的图
然后我们分别查询了 selector,selector_condition,rule,rule_condition表,如图2所示。可以看得到不同的selector和其数据。规则就是
-
一个插件对应多个选择器,一个选择器对应多个规则。
-
一个选择器对应多个匹配条件,一个规则对应多个匹配条件。
-
每个规则在对应插件下,不同的处理表现为handle字段,handle字段就是一个json字符串。
接口注册过程
SoulSpringMvcClient注解
SpringMvcClientBeanPostProcessor这个类实现了BeanPostProcessor的方法,使用了单线程的线程池进行处理。通过使用Spring的AnnotationUtils工具类找出含有SoulSpringMvcClient的注解(如图4所示)。
逻辑如图3所示(又学到了一个小知识,哈哈哈)
- 找到所有 controller、restController、requestMapping 注解的 bean 。
- 在类上找是否有注解 @SoulSpringMvcClient,如果有,则查找是否含有 * ,将其注册到控制台。
- 在方法上找是否有注解 @SoulSpringMvcClient,如果有,则注册到控制台。
RegisterUtils
RegisterUtils这个类很简单(如图5显示),其实就是使用okhttp将格式化的json(如下所示)post到SoulClientController的registSpringMvc接口,如图6显示。然后如图8通过mybatis持久化到数据库。注册的json数据如可以看得出与我们admin后台的RulesList很像(如图9所示 )。
{"appName":"http","context":"/http","path":"/http/test/**","pathDesc":"","rpcType":"http","host":"192.168.0.103","port":8189,"ruleName":"/http/test/**","enabled":true,"registerMetaData":false}
{"appName":"http","context":"/http","path":"/http/order/findById","pathDesc":"Find by id","rpcType":"http","host":"192.168.0.103","port":8189,"ruleName":"/http/order/findById","enabled":true,"registerMetaData":false}
{"appName":"http","context":"/http","path":"/http/order/path/**","pathDesc":"","rpcType":"http","host":"192.168.0.103","port":8189,"ruleName":"/http/order/path/**","enabled":true,"registerMetaData":false}
{"appName":"http","context":"/http","path":"/http/order/path/**/name","pathDesc":"","rpcType":"http","host":"192.168.0.103","port":8189,"ruleName":"/http/order/path/**/name","enabled":true,"registerMetaData":false}
{"appName":"http","context":"/http","path":"/http/order/save","pathDesc":"Save order","rpcType":"http","host":"192.168.0.103","port":8189,"ruleName":"/http/order/save","enabled":true,"registerMetaData":false}