Soul网关概述
Soul是一个异步的(基于WebFlux ),高性能的(使用Java内存),跨语言的(支持http,dubbo,springcloud协议。),响应式(基于响应式编程)的API网关。它有如下特点:
- 支持各种语言(http协议),支持 dubbo,springcloud协议。
- 插件化设计思想,插件热插拔,易扩展。
- 灵活的流量筛选,能满足各种流量控制。
- 内置丰富的插件支持,鉴权,限流,熔断,防火墙等等。
- 流量配置动态化,性能极高,网关消耗在 1~2ms。
- 支持集群部署,支持 A/B Test, 蓝绿发布。
详细的介绍可以参考官方文档
https://dromara.org/zh-cn/docs/soul/soul.html
环境搭建
前提要求
- JDK1.8
- Maven 3.2.x
- Git
- MySQL (可选,也可以使用H2)
源码获取
- GitHub地址:https://github.com/dromara/soul
- Gitee地址:https://gitee.com/shuaiqiyu/soul
- 一键三连 star,watch,fork
- Fork到个人仓库后,Clone到本地
- 在本地目录下设置upstream
git remote add upstream https://github.com/dromara/soul.git
- 拉取最新的代码
git checkout master git pull upstream master
完成之后我们可以看到整个项目的的结构是这样的
它包含如下的模块:
- soul-admin : 插件和其他信息配置的管理后台
- soul-bootstrap : 用于启动项目,用户可以参考
- soul-client : 用户可以使用 Spring MVC,Dubbo,Spring Cloud 快速访问
- soul-common : 框架的通用类
- soul-dist : 构建项目
- soul-metrics : prometheus(普罗米修斯)实现的 metrics
- soul-plugin : Soul 支持的插件集合
- soul-spi : 定义 Soul spi
- soul-spring-boot-starter : 支持 spring boot starter
- soul-sync-data-center : 提供 ZooKeeper,HTTP,WebSocket,Nacos 的方式同步数据
- soul-examples : RPC 示例项目
- soul-web : 包括插件、请求路由和转发等的核心处理包
启动soul-admin
第一次启动的话可以使用如下命令编译,跳过了一些不必要的编译过程,会比较快一些
mvn clean package install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Drat.skip=true -Dcheckstyle.skip=true
启动项目之前,先看一下soul-admin的配置文件application.yml。
这里面配置了需要的数据库,所以先启动MySql数据库
server:
port: 9095
address: 0.0.0.0
spring:
#profiles:
# active: h2
thymeleaf:
cache: true
encoding: utf-8
enabled: true
prefix: classpath:/static/
suffix: .html
datasource:
url: jdbc:mysql://localhost:3306/soul?useUnicode=true&characterEncoding=utf-8
username: root
password:
driver-class-name: com.mysql.jdbc.Driver
mybatis:
config-location: classpath:/mybatis/mybatis-config.xml
mapper-locations: classpath:/mappers/*.xml
soul:
database:
dialect: mysql
init_script: "META-INF/schema.sql"
sync:
websocket:
enabled: true
# zookeeper:
# url: localhost:2181
# sessionTimeout: 5000
# connectionTimeout: 2000
# http:
# enabled: true
# nacos:
# url: localhost:8848
# namespace: 1c10d748-af86-43b9-8265-75f487d20c6c
# acm:
# enabled: false
# endpoint: acm.aliyun.com
# namespace:
# accessKey:
# secretKey:
aes:
secret:
key: 2095132720951327
logging:
level:
root: info
org.springframework.boot: info
org.apache.ibatis: info
org.dromara.soul.bonuspoint: info
org.dromara.soul.lottery: info
org.dromara.soul: info
swagger:
enable: true
之后直接启动SoulAdminBootstrap下的main方法。
启动成功之后可以访问本地的9095端口,出现如下登录画面即启动成功。
使用内置的admin用户(密码123456)登录进去,可以看到整个Soul网关的后台管理页面,主要分为插件管理和系统管理。
启动soul-bootstrap
同样,先看一下soul-bootstrap的配置文件application-local.yml。这个配置比较简单,不过有一个soul.sync.websocket.urls配置了我们刚才启动的soul-admin项目的地址。
server:
port: 9195
address: 0.0.0.0
spring:
main:
allow-bean-definition-overriding: true
application:
name: soul-bootstrap
# cloud:
# nacos:
# discovery:
# server-addr: 127.0.0.1:8848
management:
health:
defaults:
enabled: false
soul :
file:
enabled: true
corss:
enabled: true
dubbo :
parameter: multi
sync:
websocket :
urls: ws://localhost:9095/websocket
直接运行SoulBootstrapApplication的main方法启动,出现如下输出,则表明启动成功,并且和我们的soul-admin项目连接成功。
2021-01-15 00:38:06.147 INFO 17876 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 9195
2021-01-15 00:38:06.150 INFO 17876 --- [ main] o.d.s.b.SoulBootstrapApplication : Started SoulBootstrapApplication in 4.481 seconds (JVM running for 5.738)
2021-01-15 00:38:15.025 INFO 17876 --- [ocket-connect-1] o.d.s.p.s.d.w.WebsocketSyncDataService : websocket reconnect is successful.....
到此Soul网关的基础搭建算是完成了,后续就可以接入我们实际的业务项目进行使用。
后面的学习分享中将进行各种协议的项目的演示和操作。。。
踩过的坑
- 项目成功编译完成了,没有报错,但是启动的时候报“xxx包找不到”,去网上找办法,看到说是要将IDEA 的 build/run 动作如果委托给了 maven,需要如下的配置。
配置之后是好了,但是还是不清楚为什么,为了不在其他问题上浪费时间,就没有细究,如果有大佬知道,希望解答一下,什么时候需要勾选这个选项?什么时候不用勾选?