按照计划,今天要从源码的角度分析soul是如何处理浏览器请求并找到真正要访问的后台服务,但计划赶不上变化,作为一名超级奶爸,只有深夜的时间真正属于自己,无法按计划完成今天任务了,但是还是坚持每天有所产出,那么今晚分析soul源码得到的结果是终于弄清楚了接入soul的springBoot应用启动时是如何将url的拦截规则信息注册到soul-admin管理控制台的。
首先将今天分析源码带着的问题先抛出来,我们在上一篇中就以使用者的角度体验了http请求通过soul成功转发到了后台,在后台工程soul-examples-http启动时,是会有以下日志信息的:
2021-01-16 23:52:16.861 INFO 49500 --- [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.137.1","port":8188,"ruleName":"/http/test/**","enabled":true,"registerMetaData":false}
2021-01-16 23:52:31.083 INFO 49500 --- [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.137.1","port":8188,"ruleName":"/http/order/save","enabled":true,"registerMetaData":false}
2021-01-16 23:52:39.780 INFO 49500 --- [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.137.1","port":8188,"ruleName":"/http/order/findById","enabled":true,"registerMetaData":false}
2021-01-16 23:52:50.768 INFO 49500 --- [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.137.1","port":8188,"ruleName":"/http/order/path/**","enabled":true,"registerMetaData":false}
2021-01-16 23:52:59.528 INFO 49500 --- [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.137.1","port":8188,"ruleName":"/http/order/path/**/name","enabled":true,"registerMetaData":false}
这里打印出来的日志信息,实际上是后台工程在启动时,将提供服务的url的相对路径的拦截规则注册到了soul-admin中,此举的目的是为了让请求通过soul处理时,能灵活的根据soul-admin中对单个接口或者所有接口的请求做灵活处理,包括限制访问,在集群环境下通过加权以及指定ip访问等方式控制此请求是否能到达对应的后台服务或者指定到达某个后台服务,可以看到,上述日志信息和在soul-admin首页的divide插件看到的选择器规则是一致的,如下图所示:
那么带着这个问题,我Debug了一波源码,得出了以下结论,我画了一个流程图描述,如下所示: