可定制DispatcherServlet

This commit is contained in:
redkale
2024-01-04 18:34:02 +08:00
parent 0435cc4e48
commit 6348aa884f
4 changed files with 24 additions and 34 deletions

View File

@@ -5,20 +5,7 @@
文件说明:
${APP_HOME} 指当前程序的根目录APP_HOME
没注明唯一的节点可多个存在
required 被声明required的属性值不能为空
group
/ / \ \
/ / \ \
/ / \ \
node1 node2 node3 node4
/ \
/ \
/ \
/ \
serviceid1 serviceid2
/ \ / \
serviceid1_name1 serviceid1_name2 serviceid2_name1 serviceid2_name2
required 被声明required的属性值不能为空
-->
<!--
nodeid: int 进程的节点ID用于分布式环境一个系统中节点ID必须全局唯一使用cluster时框架会进行唯一性校验
@@ -28,23 +15,23 @@
lib: 加上额外的lib路径,多个路径用分号;隔开; 默认为空。 例如: ${APP_HOME}/lib/a.jar;${APP_HOME}/lib2/b.jar;
-->
<application nodeid="1000" port="6560" lib="">
<!--
【节点全局唯一】
【节点全局唯一】 @since 2.3.0
全局Serivce执行的线程池 Application.workExecutor, 没配置该节点将自动创建一个。
threads 线程数为0表示不启用workExecutor只用IO线程。默认: CPU核数, 核数=1的情况下默认值为2
-->
<executor threads="4"/>
<!--
【节点全局唯一】
【节点全局唯一】 @since 2.8.0
全局Serivce的定时任务设置没配置该节点将自动创建一个。
enabled 是否开启缓存功能。默认: true
-->
<schedule enabled="true"/>
<!--
【节点全局唯一】
【节点全局唯一】 @since 2.8.0
全局Serivce的缓存设置没配置该节点将自动创建一个。
enabled 是否开启缓存功能。默认: true
source: 远程CacheSource的资源名
@@ -162,6 +149,7 @@
aliveTimeoutSeconds: KeepAlive读操作超时秒数 默认30 0表示永久不超时; -1表示禁止KeepAlive
readTimeoutSeconds: 读操作超时秒数, 默认0 0表示永久不超时
writeTimeoutSeconds: 写操作超时秒数, 默认0 0表示永久不超时
dispatcher: DispatcherServlet的自定义实现类若是HttpServer则实现类必须是HttpDispatcherServlet的子类
interceptor: 启动/关闭NodeServer时被调用的拦截器实现类必须是org.redkale.boot.NodeInterceptor的子类默认为null
-->
<server protocol="HTTP" host="127.0.0.1" port="6060" root="root" lib="">
@@ -365,6 +353,7 @@
</server>
</application>
```
# source.properties 配置:

View File

@@ -26,14 +26,7 @@
lib: 加上额外的lib路径,多个路径用分号;隔开; 默认为空。 例如: ${APP_HOME}/lib/a.jar;${APP_HOME}/lib2/b.jar;
-->
<application nodeid="1000" port="6560" lib="">
<!--
【已废弃,不再需要此节点】
所有服务所需的资源
-->
<resources>
</resources>
<!--
【节点全局唯一】 @since 2.3.0
全局Serivce执行的线程池 Application.workExecutor, 没配置该节点将自动创建一个。
@@ -159,7 +152,6 @@
lib: server额外的class目录 默认为${APP_HOME}/libs/*;
charset: 文本编码, 默认: UTF-8
backlog: 默认10K
threads【已废弃】 线程数, 默认: CPU核数*2最小8个【已废弃 @since 2.3.0】
maxconns 最大连接数, 小于1表示无限制 默认: 0
maxbody: request.body最大值 默认: 256K
bufferCapacity: ByteBuffer的初始化大小 TCP默认: 32K; (HTTP 2.0、WebSocket必须要16k以上); UDP默认: 8K
@@ -168,6 +160,7 @@
aliveTimeoutSeconds: KeepAlive读操作超时秒数 默认30 0表示永久不超时; -1表示禁止KeepAlive
readTimeoutSeconds: 读操作超时秒数, 默认0 0表示永久不超时
writeTimeoutSeconds: 写操作超时秒数, 默认0 0表示永久不超时
dispatcher: DispatcherServlet的自定义实现类若是HttpServer则实现类必须是HttpDispatcherServlet的子类
interceptor: 启动/关闭NodeServer时被调用的拦截器实现类必须是org.redkale.boot.NodeInterceptor的子类默认为null
-->
<server protocol="HTTP" host="127.0.0.1" port="6060" root="root" lib="">

View File

@@ -57,7 +57,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
protected final ResourceFactory resourceFactory;
//服务的根Servlet
protected final DispatcherServlet<K, C, R, P, S> dispatcher;
protected DispatcherServlet<K, C, R, P, S> dispatcher;
//ClassLoader
protected RedkaleClassLoader serverClassLoader;
@@ -138,6 +138,14 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
if (!this.name.matches("^[a-zA-Z][\\w_-]{1,64}$")) {
throw new RedkaleException("server.name (" + this.name + ") is illegal");
}
String dispatcherImpl = config.getValue("dispatcher");
if (Utility.isNotBlank(dispatcherImpl)) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class dispatcherImplClass = classLoader.loadClass(dispatcherImpl);
RedkaleClassLoader.putReflectionDeclaredConstructors(dispatcherImplClass, dispatcherImplClass.getName());
this.dispatcher = (DispatcherServlet) dispatcherImplClass.getConstructors()[0].newInstance();
this.dispatcher.application = application;
}
AnyValue sslConf = config.getAnyValue("ssl");
if (sslConf != null) {
String builderClass = sslConf.getValue("builder", SSLBuilder.class.getName());

View File

@@ -113,14 +113,14 @@ public class HttpDispatcherServlet extends DispatcherServlet<String, HttpContext
}
public HttpServlet removeHttpServlet(final HttpServlet servlet) {
Predicate<MappingEntry> predicateEntry = (t) -> t.servlet == servlet;
Predicate<Map.Entry<String, WebSocketServlet>> predicateFilter = (t) -> t.getValue() == servlet;
Predicate<MappingEntry> predicateEntry = t -> t.servlet == servlet;
Predicate<Map.Entry<String, WebSocketServlet>> predicateFilter = t -> t.getValue() == servlet;
removeHttpServlet(predicateEntry, predicateFilter);
return servlet;
}
public <T extends HttpServlet> HttpServlet removeHttpServlet(Service service) {
Predicate<MappingEntry> predicateEntry = (t) -> {
public HttpServlet removeHttpServlet(Service service) {
Predicate<MappingEntry> predicateEntry = t -> {
if (!Rest.isRestDyn(t.servlet)) {
return false;
}
@@ -161,7 +161,7 @@ public class HttpDispatcherServlet extends DispatcherServlet<String, HttpContext
@SuppressWarnings("unchecked")
public <T extends WebSocket> HttpServlet removeHttpServlet(Class<T> websocketOrServletType) {
Predicate<MappingEntry> predicateEntry = (t) -> {
Predicate<MappingEntry> predicateEntry = t -> {
Class type = t.servlet.getClass();
if (type == websocketOrServletType) {
return true;
@@ -169,7 +169,7 @@ public class HttpDispatcherServlet extends DispatcherServlet<String, HttpContext
RestDynSourceType rdt = (RestDynSourceType) type.getAnnotation(RestDynSourceType.class);
return (rdt != null && rdt.value() == websocketOrServletType);
};
Predicate<Map.Entry<String, WebSocketServlet>> predicateFilter = (t) -> {
Predicate<Map.Entry<String, WebSocketServlet>> predicateFilter = t -> {
Class type = t.getValue().getClass();
if (type == websocketOrServletType) {
return true;