Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
touzizhuo03
Dubbo
提交
96ab99c9
D
Dubbo
项目概览
touzizhuo03
/
Dubbo
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Dubbo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
96ab99c9
编写于
4月 10, 2018
作者:
Y
YunaiV
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dubbo rpc rest
上级
23049f4c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
52 addition
and
9 deletion
+52
-9
dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/DubboHttpServer.java
.../com/alibaba/dubbo/rpc/protocol/rest/DubboHttpServer.java
+37
-2
dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/NettyServer.java
...java/com/alibaba/dubbo/rpc/protocol/rest/NettyServer.java
+13
-6
dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/RestServerFactory.java
...om/alibaba/dubbo/rpc/protocol/rest/RestServerFactory.java
+2
-1
未找到文件。
dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/DubboHttpServer.java
浏览文件 @
96ab99c9
...
...
@@ -36,11 +36,26 @@ import javax.servlet.http.HttpServletResponse;
import
java.io.IOException
;
import
java.util.Enumeration
;
/**
* 基于 `dubbo-remoting-http` 的 HTTP 服务器实现类
*/
public
class
DubboHttpServer
extends
BaseRestServer
{
/**
* Resteasy HttpServletDispatcher
*/
private
final
HttpServletDispatcher
dispatcher
=
new
HttpServletDispatcher
();
/**
* Resteasy ResteasyDeployment
*/
private
final
ResteasyDeployment
deployment
=
new
ResteasyDeployment
();
/**
* Dubbo HttpBinder$Adaptive
*/
private
HttpBinder
httpBinder
;
/**
* HttpServer 对象
*/
private
HttpServer
httpServer
;
// private boolean isExternalServer;
...
...
@@ -48,10 +63,13 @@ public class DubboHttpServer extends BaseRestServer {
this
.
httpBinder
=
httpBinder
;
}
@Override
protected
void
doStart
(
URL
url
)
{
// 创建 HttpServer 对象,使用 RestHandler 作为处理器。
// TODO jetty will by default enable keepAlive so the xml config has no effect now
httpServer
=
httpBinder
.
bind
(
url
,
new
RestHandler
());
// 获得 ServletContext 对象
ServletContext
servletContext
=
ServletManager
.
getInstance
().
getServletContext
(
url
.
getPort
());
if
(
servletContext
==
null
)
{
servletContext
=
ServletManager
.
getInstance
().
getServletContext
(
ServletManager
.
EXTERNAL_SERVER_PORT
);
...
...
@@ -60,9 +78,10 @@ public class DubboHttpServer extends BaseRestServer {
throw
new
RpcException
(
"No servlet context found. If you are using server='servlet', "
+
"make sure that you've configured "
+
BootstrapListener
.
class
.
getName
()
+
" in web.xml"
);
}
// 设置 ResteasyDeployment
servletContext
.
setAttribute
(
ResteasyDeployment
.
class
.
getName
(),
deployment
);
// https://github.com/resteasy/Resteasy/blob/master/server-adapters/resteasy-undertow/src/main/java/org/jboss/resteasy/plugins/server/undertow/UndertowJaxrsServer.java#L74
servletContext
.
setAttribute
(
ResteasyDeployment
.
class
.
getName
(),
deployment
);
// 初始化 Resteasy HttpServletDispatcher
try
{
dispatcher
.
init
(
new
SimpleServletConfig
(
servletContext
));
}
catch
(
ServletException
e
)
{
...
...
@@ -70,20 +89,26 @@ public class DubboHttpServer extends BaseRestServer {
}
}
@Override
public
void
stop
()
{
httpServer
.
close
();
}
@Override
protected
ResteasyDeployment
getDeployment
()
{
return
deployment
;
}
private
class
RestHandler
implements
HttpHandler
{
@Override
public
void
handle
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
,
ServletException
{
// 设置
RpcContext
.
getContext
().
setRemoteAddress
(
request
.
getRemoteAddr
(),
request
.
getRemotePort
());
// 调度请求
dispatcher
.
service
(
request
,
response
);
}
}
private
static
class
SimpleServletConfig
implements
ServletConfig
{
...
...
@@ -94,28 +119,38 @@ public class DubboHttpServer extends BaseRestServer {
this
.
servletContext
=
servletContext
;
}
@Override
public
String
getServletName
()
{
return
"DispatcherServlet"
;
}
@Override
public
ServletContext
getServletContext
()
{
return
servletContext
;
}
@Override
public
String
getInitParameter
(
String
s
)
{
return
null
;
}
@Override
public
Enumeration
getInitParameterNames
()
{
return
new
Enumeration
()
{
@Override
public
boolean
hasMoreElements
()
{
return
false
;
}
@Override
public
Object
nextElement
()
{
return
null
;
}
};
}
}
}
dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/NettyServer.java
浏览文件 @
96ab99c9
...
...
@@ -30,31 +30,38 @@ import java.util.Map;
/**
* Netty server can't support @Context injection of servlet objects since it's not a servlet container
*
* 基于 Netty 的 HTTP 服务器实现类
*/
public
class
NettyServer
extends
BaseRestServer
{
private
final
NettyJaxrsServer
server
=
new
NettyJaxrsServer
();
@Override
protected
void
doStart
(
URL
url
)
{
// 设置 NettyJaxrsServer 的属性
String
bindIp
=
url
.
getParameter
(
Constants
.
BIND_IP_KEY
,
url
.
getHost
());
if
(!
url
.
isAnyHost
()
&&
NetUtils
.
isValidLocalHost
(
bindIp
))
{
server
.
setHostname
(
bindIp
);
server
.
setHostname
(
bindIp
);
// Hostname
}
server
.
setPort
(
url
.
getParameter
(
Constants
.
BIND_PORT_KEY
,
url
.
getPort
()));
server
.
setPort
(
url
.
getParameter
(
Constants
.
BIND_PORT_KEY
,
url
.
getPort
()));
// Port
Map
<
ChannelOption
,
Object
>
channelOption
=
new
HashMap
<
ChannelOption
,
Object
>();
channelOption
.
put
(
ChannelOption
.
SO_KEEPALIVE
,
url
.
getParameter
(
Constants
.
KEEP_ALIVE_KEY
,
Constants
.
DEFAULT_KEEP_ALIVE
));
channelOption
.
put
(
ChannelOption
.
SO_KEEPALIVE
,
url
.
getParameter
(
Constants
.
KEEP_ALIVE_KEY
,
Constants
.
DEFAULT_KEEP_ALIVE
));
// Keep-Alive
server
.
setChildChannelOptions
(
channelOption
);
server
.
setExecutorThreadCount
(
url
.
getParameter
(
Constants
.
THREADS_KEY
,
Constants
.
DEFAULT_THREADS
));
server
.
setIoWorkerCount
(
url
.
getParameter
(
Constants
.
IO_THREADS_KEY
,
Constants
.
DEFAULT_IO_THREADS
));
server
.
setMaxRequestSize
(
url
.
getParameter
(
Constants
.
PAYLOAD_KEY
,
Constants
.
DEFAULT_PAYLOAD
));
server
.
setExecutorThreadCount
(
url
.
getParameter
(
Constants
.
THREADS_KEY
,
Constants
.
DEFAULT_THREADS
));
// 执行线程数
server
.
setIoWorkerCount
(
url
.
getParameter
(
Constants
.
IO_THREADS_KEY
,
Constants
.
DEFAULT_IO_THREADS
));
// IO 线程数
server
.
setMaxRequestSize
(
url
.
getParameter
(
Constants
.
PAYLOAD_KEY
,
Constants
.
DEFAULT_PAYLOAD
));
// 请求最大长度
// 启动 NettyJaxrsServer
server
.
start
();
}
@Override
public
void
stop
()
{
server
.
stop
();
}
@Override
protected
ResteasyDeployment
getDeployment
()
{
return
server
.
getDeployment
();
}
}
dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/RestServerFactory.java
浏览文件 @
96ab99c9
...
...
@@ -33,7 +33,7 @@ public class RestServerFactory {
public
RestServer
createServer
(
String
name
)
{
// TODO move names to Constants
if
(
"servlet"
.
equalsIgnoreCase
(
name
)
||
"jetty"
.
equalsIgnoreCase
(
name
)
||
"tomcat"
.
equalsIgnoreCase
(
name
))
{
if
(
"servlet"
.
equalsIgnoreCase
(
name
)
||
"jetty"
.
equalsIgnoreCase
(
name
)
||
"tomcat"
.
equalsIgnoreCase
(
name
))
{
// `dubbo-remoting-http`
return
new
DubboHttpServer
(
httpBinder
);
}
else
if
(
"netty"
.
equalsIgnoreCase
(
name
))
{
return
new
NettyServer
();
...
...
@@ -41,4 +41,5 @@ public class RestServerFactory {
throw
new
IllegalArgumentException
(
"Unrecognized server name: "
+
name
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录