soul源码学习(二)-http示例

目标

  1. 运行官方示例soul-examples-http
  2. 了解http方式访问网关大致流程

运行soul-examples-http示例

配置

该示例工程是以常见的springmvc方式访问,其中要想被soul网关代理访问,只需要在普通的controller上加入soul相关注解,如图所示:

  1. 类方式
    在这里插入图片描述
  2. 方法上加soul注解
    在这里插入图片描述
  3. pom文件引入soul客户端的依赖包
    在这里插入图片描述

运行

  1. 首先启动soul-admin和soul-bootstrap项目,启动过程详见soul源码学习(一)-项目搭建
  2. 启动soul-examples-http下的SoulTestHttpApplication,注意项目配置文件application.yml中的
soul:
  http:
    adminUrl: http://localhost:9095
    port: 8188
    contextPath: /http
    appName: http
    full: false

应对应soul-admin中相应配置端口,项目启动后的输出:

2021-01-15 22:34:58.818  INFO 72112 --- [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":"127.0.0.1","port":8188,"ruleName":"/http/test/**","enabled":true,"registerMetaData":false} 
2021-01-15 22:34:58.865  INFO 72112 --- [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":"127.0.0.1","port":8188,"ruleName":"/http/order/findById","enabled":true,"registerMetaData":false} 
2021-01-15 22:34:58.892  INFO 72112 --- [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":"127.0.0.1","port":8188,"ruleName":"/http/order/path/**","enabled":true,"registerMetaData":false} 
2021-01-15 22:34:58.927  INFO 72112 --- [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":"127.0.0.1","port":8188,"ruleName":"/http/order/path/**/name","enabled":true,"registerMetaData":false} 
2021-01-15 22:34:58.970  INFO 72112 --- [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":"127.0.0.1","port":8188,"ruleName":"/http/order/save","enabled":true,"registerMetaData":false} 

可以看到相关controller的url信息被RegisterUtils类进行了注册,该类的注册过程随后分析
3. 访问soul-admin项目,可看到相关信息已在web界面展示出来

在这里插入图片描述
在这里插入图片描述
与步骤2的日志信息相对应
4. 数据库中对应的注册信息,如下图(截取关键信息):
rule
在这里插入图片描述
rule-condition
在这里插入图片描述

selector
在这里插入图片描述
其中handler字段值为:

[{"upstreamHost":"localhost","protocol":"http://","upstreamUrl":"127.0.0.1:8188","weight":50,"status":true,"timestamp":0,"warmup":0}]

由于目前本机只启动了一个soul-examples-http,端口为8188,因此该字段只存了一台服务器地址

selector-condition
在这里插入图片描述
综上可知,web界面中的数据,来源于(rule和rule-condition
表),保存的是url路由映射信息
在这里插入图片描述

在这里插入图片描述
上图数据来源于selector和selector-condition表,保存的是路由的url对应的均衡负载信息

访问测试

使用postman访问soul-bootstrap项目:

在这里插入图片描述
测试成功,soul-bootstrap控制台输出的日志为:

2021-01-15 22:54:33.162  INFO 70166 --- [-work-threads-1] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http
2021-01-15 22:54:33.169  INFO 70166 --- [-work-threads-1] o.d.soul.plugin.base.AbstractSoulPlugin  : divide selector success match , selector name :/http/test/**
2021-01-15 22:54:33.189  INFO 70166 --- [-work-threads-1] o.d.s.plugin.httpclient.WebClientPlugin  : The request urlPath is http://127.0.0.1:8188/test/findByUserId?userId=1, retryTimes is 0
2021-01-15 22:54:33.382  WARN 70166 --- [-work-threads-1] io.netty.bootstrap.Bootstrap             : Unknown channel option 'SO_TIMEOUT' for channel '[id: 0x1e0b5983]'

总结

通过以上分析,可知soul的大致访问流程为:

  1. soul-admin为客户端(soul-examples-http)的注册中心,当客户端启动时,会向soul-admin注册相关通过soul注解标注的url元数据,同时也会向数据库保存相关路由信息
  2. soul-admin与soul-bootstrap通过配置websocket方式(默认),进行路由信息的同步,路由信息保存在soul-bootstrap的内存中
  3. 用户通过浏览器方位soul-bootstrap,soul-bootstrp通过访问的url进行路由匹配信息,从而找到相应后端服务器进行访问

猜你喜欢

转载自blog.csdn.net/yilongzhetian/article/details/112689650