提交 96ab99c9 编写于 作者: Y YunaiV

dubbo rpc rest

上级 23049f4c
......@@ -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;
}
};
}
}
}
......@@ -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();
}
}
......@@ -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.
先完成此消息的编辑!
想要评论请 注册