Dubbo 的 HTTP 服务器,在 dubbo-remoting-http 模块中实现,使用在 http://、 rest://、hessian://、webservice://协议上。
实现 Resetable 接口,HTTP 服务器接口。方法如下:
/**
* HTTP 服务器接口
*/
public interface HttpServer extends Resetable {
/**
* get http handler.
*
* @return http handler.
*/
HttpHandler getHttpHandler();
/**
* get url.
*
* @return url
*/
URL getUrl();
/**
* get local address.
*
* @return local address.
*/
InetSocketAddress getLocalAddress();
/**
* close the channel.
*/
void close();
/**
* Graceful close the channel.
*/
void close(int timeout);
/**
* is bound.
*
* @return bound
*/
boolean isBound();
/**
* is closed.
*
* @return closed
*/
boolean isClosed();
}
实现 HttpServer 接口,HTTP 服务器抽象类。代码如下:
/**
* AbstractHttpServer
*
* HTTP 服务器抽象类
*/
public abstract class AbstractHttpServer implements HttpServer {
/**
* URL 对象
*/
private final URL url;
/**
* 处理器
*/
private final HttpHandler handler;
/**
* 是否关闭
*/
private volatile boolean closed;
public AbstractHttpServer(URL url, HttpHandler handler) {
if (url == null) {
throw new IllegalArgumentException("url == null");
}
if (handler == null) {
throw new IllegalArgumentException("handler == null");
}
this.url = url;
this.handler = handler;
}
@Override
public HttpHandler getHttpHandler() {
return handler;
}
@Override
public URL getUrl() {
return url;
}
@Override
public void reset(URL url) {
}
@Override
public boolean isBound() {
return true;
}
@Override
public InetSocketAddress getLocalAddress() {
return url.toInetSocketAddress();
}
@Override
public void close() {
closed = true;
}
@Override
public void close(int timeout) {
close();
}
@Override
public boolean isClosed() {
return closed;
}
}
HTTP 处理器接口。方法如下:
/**
* http invocation handler.
*
* HTTP 处理器接口
*/
public interface HttpHandler {
/**
* invoke.
*
* 处理器请求
*
* @param request request. 请求
* @param response response. 响应
* @throws IOException 当 IO 发生异常
* @throws ServletException 当 Servlet 发生异常
*/
void handle(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException;
}
HTTP 绑定器接口。方法如下:
/**
* HttpBinder
*
* HTTP 绑定起接口
*/
@SPI("jetty")
public interface HttpBinder {
/**
* bind the server.
*
* @param url server url.
* @return server.
*/
@Adaptive({Constants.SERVER_KEY})
HttpServer bind(URL url, HttpHandler handler);
}
实现 javax.servlet.http.HttpServlet 接口,服务请求调度 Servlet。代码如下:
/**
* Service dispatcher Servlet.
*
* 服务调度 Servlet
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 5766349180380479888L;
/**
* 处理器集合
*
* key:服务器端口
*/
private static final Map handlers = new ConcurrentHashMap();
/**
* 单例
*/
private static DispatcherServlet INSTANCE;
public DispatcherServlet() {
DispatcherServlet.INSTANCE = this;
}
public static DispatcherServlet getInstance() {
return INSTANCE;
}
/**
* 添加处理器
*
* @param port 服务器端口
* @param processor 处理器
*/
public static void addHttpHandler(int port, HttpHandler processor) {
handlers.put(port, processor);
}
/**
* 移除处理器
*
* @param port 服务器端口
*/
public static void removeHttpHandler(int port) {
handlers.remove(port);
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获得处理器
HttpHandler handler = handlers.get(request.getLocalPort());
// 处理器不存在,报错
if (handler == null) {// service not found.
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Service not found.");
// 处理请求
} else {
handler.handle(request, response);
}
}
}
Servlet 管理器,负责管理 ServletContext ,目前仅有 dubbo-rpc-rest 模块,需要使用到这个类。代码如下:
public class ServletManager {
/**
* 外部服务器端口,用于 `servlet` 的服务器端口
*/
public static final int EXTERNAL_SERVER_PORT = -1234;
/**
* 单例
*/
private static final ServletManager instance = new ServletManager();
/**
* ServletContext 集合
*/
private final Map contextMap = new ConcurrentHashMap();
public static ServletManager getInstance() {
return instance;
}
/**
* 添加 ServletContext 对象
*
* @param port 服务器端口
* @param servletContext ServletContext 对象
*/
public void addServletContext(int port, ServletContext servletContext) {
contextMap.put(port, servletContext);
}
/**
* 移除 ServletContext 对象
*
* @param port 服务器端口
*/
public void removeServletContext(int port) {
contextMap.remove(port);
}
/**
* 获得 ServletContext 对象
*
* @param port 服务器端口
* @return ServletContext 对象
*/
public ServletContext getServletContext(int port) {
return contextMap.get(port);
}
}
实现 AbstractHttpServer 抽象类,基于 Tomcat 的 HTTP 服务器实现类。
/**
* 基于 Tomcat 的 HTTP 服务器实现类
*/
public class TomcatHttpServer extends AbstractHttpServer {
private static final Logger logger = LoggerFactory.getLogger(TomcatHttpServer.class);
/**
* 内嵌的 Tomcat 对象
*/
private final Tomcat tomcat;
/**
* URL 对象
*/
private final URL url;
public TomcatHttpServer(URL url, final HttpHandler handler) {
super(url, handler);
this.url = url;
// 注册 HttpHandler 到 DispatcherServlet 中
DispatcherServlet.addHttpHandler(url.getPort(), handler);
// 创建内嵌的 Tomcat 对象
String baseDir = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
tomcat = new Tomcat();
tomcat.setBaseDir(baseDir);
tomcat.setPort(url.getPort());
tomcat.getConnector().setProperty("maxThreads", String.valueOf(url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS))); // 最大线程数
// tomcat.getConnector().setProperty(
// "minSpareThreads", String.valueOf(url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS)));
tomcat.getConnector().setProperty("maxConnections", String.valueOf(url.getParameter(Constants.ACCEPTS_KEY, -1))); // 最大连接池
tomcat.getConnector().setProperty("URIEncoding", "UTF-8"); // 编码为 UTF-8
tomcat.getConnector().setProperty("connectionTimeout", "60000"); // 连接超时,60 秒
tomcat.getConnector().setProperty("maxKeepAliveRequests", "-1");
tomcat.getConnector().setProtocol("org.apache.coyote.http11.Http11NioProtocol");
// 添加 DispatcherServlet 到 Tomcat 中
Context context = tomcat.addContext("/", baseDir);
Tomcat.addServlet(context, "dispatcher", new DispatcherServlet());
context.addServletMapping("/*", "dispatcher");
// 添加 ServletContext 对象,到 ServletManager 中
ServletManager.getInstance().addServletContext(url.getPort(), context.getServletContext());
// 启动 Tomcat
try {
tomcat.start();
} catch (LifecycleException e) {
throw new IllegalStateException("Failed to start tomcat server at " + url.getAddress(), e);
}
}
}