Soul-bootStrap
网关的核心启动入口
Maven 依赖
从Maven 依赖中我们可以看见,所以 Soul 网关需要的各个插件模块都尽在眼底,所有的组件模块都可以自定义配置,其中有:
- soul-spring-boot-starter-gateway:其中在
spring.provides
包含 soul-web 模块。 - soul-spring-boot-starter-plugin-divide:对于分流插件
- soul-spring-boot-starter-plugin-httpclient:HTTPClient
- soul-spring-boot-starter-plugin-alibaba-dubbo:对于 alibaba-dubbo 支持
- soul-spring-boot-starter-plugin-ratelimiter:限流组件
- soul-spring-boot-starter-plugin-hystrix:熔断限流组件
- soul-spring-boot-starter-plugin-waf:waf 组件
- soul-spring-boot-starter-plugin-monitor:监控组件
- soul-spring-boot-starter-sync-data-websocket:websocket 支持
- soul-spring-boot-starter-sync-data-http:异步 http 同步
- soul-spring-boot-starter-plugin-sign:默认签名插件
- soul-spring-boot-starter-plugin-resilience4j:另一种限流插件
- 等等自己需要可以配置
Netty 自定义配置
@Configuration
public class SoulNettyWebServerFactory {
/**
* Netty reactive web server factory netty reactive web server factory.
*
* @return the netty reactive web server factory
*/
@Bean
public NettyReactiveWebServerFactory nettyReactiveWebServerFactory() {
NettyReactiveWebServerFactory webServerFactory = new NettyReactiveWebServerFactory();
webServerFactory.addServerCustomizers(new EventLoopNettyCustomizer());
return webServerFactory;
}
private static class EventLoopNettyCustomizer implements NettyServerCustomizer {
@Override
public HttpServer apply(final HttpServer httpServer) {
return httpServer
.tcpConfiguration(tcpServer -> tcpServer
.runOn(LoopResources.create("soul-netty", 1, LoopResources.DEFAULT_IO_WORKER_COUNT, true), false)
.selectorOption(ChannelOption.SO_REUSEADDR, true)
.selectorOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT));
}
}
}
健康检查
配置文件里面默认关闭健康检查,但是工程里面自己实现健康返回。
management:
health:
defaults:
enabled: false
HealthFilter
@Component
@Order(-99)
public final class HealthFilter implements WebFilter {
private static final String[] FILTER_TAG = {"/actuator/health", "/health_check"};
@Override
public Mono<Void> filter(@Nullable final ServerWebExchange exchange, @Nullable final WebFilterChain chain) {
ServerHttpRequest request = Objects.requireNonNull(exchange).getRequest();
String urlPath = request.getURI().getPath();
for (String check : FILTER_TAG) {
if (check.equals(urlPath)) {
String result = JsonUtils.toJson(new Health.Builder().up().build());
DataBuffer dataBuffer = exchange.getResponse().bufferFactory().wrap(result.getBytes());
return exchange.getResponse().writeWith(Mono.just(dataBuffer));
}
}
return Objects.requireNonNull(chain).filter(exchange);
}
}
项目启动
因为 SpringBoot Webflux 版本,启动很简单,直接 run Application 即可。
从启动日志里面我们可以清晰看见加载的插件有那些,访问本地 URL 返回结果:
> curl http://localhost:9195/actuator/health
{"status":"UP"}
Socket 同步数据
项目中默认使用 Socket 同步数据,如果此时还没有启动 Soul-admin,会在日志中有警告,不用过多担心。
Soul-admin
MySQL 环境搭建
本地搭建使用 docker 搭建 MySQL 环境,对应的 docker-compose.yml
文件如下:
version: '3'
services:
mysql57:
image: mysql:5.7
ports:
- 0.0.0.0:3306:3306
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
volumes:
- ./db:/var/lib/mysql
deploy:
placement:
constraints:
- node.hostname==server1
restart_policy:
condition: on-failure
执行启动命令如下:
由于 Soul-admin 需要有持久化的操作,所以我们需要启动数据库,之后把 META-INF 文件夹下的 SQL 文件执行,之后创建 Soul 数据库。
启动工程
我们对应数据库表建立好之后,我们就可以启动工程了,项目因为是 SpringBoot 工程,直接启动即可。
ok 直接启动截图如下,默认端口为9095。
访问界面
由于项目是有前端界面,所以我们可以直接打开,对应地址:http://localhost:9095/#/home
最后对于 Soul-admin 工程的目录结构,留到我们下次再分析吧。