概述

最近参加了猫大人组织的 soul 网关源码阅读,目的是阅读优质的开源项目,吸收精华,养成习惯。第一天先进行下简单的开箱操作~

任务

  1. 搭建网关环境(github 一键三连 star,watch,fork)
  2. 编译代码,运行soul-admin soul-bootstrap

入门

下载soul源码

https://github.com/dromara/soul

启动 soul-admin

配置数据库:

1
2
3
4
5
datasource:
url: jdbc:mysql://localhost:3306/soul?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver

在 IDEA 中启动 SoulAdminBootstrap,启动成功后访问:http://localhost:9095,账号密码为admin/123456。至此 soul-admin 管理控制台启动成功。

image.png

搭建网关

使用 soul-bootstrap 作为网关,启动 SoulBootstrapApplication,端口为 9195。重点配置是控制台的连接:

1
2
3
4
5
6
7
8
9
10
soul :
file:
enabled: true
corss:
enabled: true
dubbo :
parameter: multi
sync:
websocket :
urls: ws://localhost:9095/websocket

基础 HTTP 连接测试

使用 soul-examples 中的 soul-examples-http 作为样例,启动 SoulTestHttpApplication。重点配置:

1
2
3
4
5
6
7
soul:
http:
adminUrl: http://localhost:9095
port: 8188
contextPath: /http
appName: http
full: false

在业务接口中添加 @SoulSpringMvcClient 注解,将 url 规则注册到网关,样例中添加了 @SoulSpringMvcClient(path = "/test/**"),表示设置了/test/**的规则。

启动工程后,刷新 admin 页面,发现规则自动添加到了 divide 的插件列表中。

image.png

http 转发测试,请求 http://localhost:9195/http/test/findByUserId?userId=1 。测试结果如下,说明可以成功通过网关访问到实际的服务。

image.png

尝试关闭选择器规则,再次进行请求。结果为请求失败,未找到当前的规则。

image.png

尝试关闭 http 测试的服务,admin 错误日志记录,检查到 8188(http服务)挂了。

1
ERROR 1148 --- [upstream-task-3] o.d.s.a.s.impl.UpstreamCheckService      : check the url=192.168.3.3:8188 is fail

此时再次请求,结果仍未失败,未找到当前的规则。

Link