diff --git a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpServer.java b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpServer.java index cb30752fff0a573c754170b71c022e4a71b2844a..7a77c8b9314f6fe27b6e6d82776b76c9021e74f1 100644 --- a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpServer.java +++ b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpServer.java @@ -15,15 +15,10 @@ */ package com.alibaba.dubbo.remoting.http.jetty; -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.mortbay.jetty.Server; -import org.mortbay.jetty.handler.AbstractHandler; import org.mortbay.jetty.nio.SelectChannelConnector; +import org.mortbay.jetty.servlet.ServletHandler; +import org.mortbay.jetty.servlet.ServletHolder; import org.mortbay.thread.QueuedThreadPool; import com.alibaba.dubbo.common.Constants; @@ -32,6 +27,7 @@ import com.alibaba.dubbo.common.logger.Logger; import com.alibaba.dubbo.common.logger.LoggerFactory; import com.alibaba.dubbo.common.utils.NetUtils; import com.alibaba.dubbo.remoting.http.HttpHandler; +import com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet; import com.alibaba.dubbo.remoting.http.support.AbstractHttpServer; public class JettyHttpServer extends AbstractHttpServer { @@ -42,7 +38,8 @@ public class JettyHttpServer extends AbstractHttpServer { public JettyHttpServer(URL url, final HttpHandler handler){ super(url, handler); - + DispatcherServlet.addHttpHandler(url.getPort(), handler); + int threads = url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS); QueuedThreadPool threadPool = new QueuedThreadPool(); threadPool.setDaemon(true); @@ -58,13 +55,12 @@ public class JettyHttpServer extends AbstractHttpServer { server = new Server(); server.setThreadPool(threadPool); server.addConnector(connector); - server.addHandler(new AbstractHandler() { - public void handle(String target, HttpServletRequest request, - HttpServletResponse response, int dispatch) throws IOException, - ServletException { - handler.handle(request, response); - } - }); + + ServletHandler servletHandler = new ServletHandler(); + ServletHolder servletHolder = servletHandler.addServletWithMapping(DispatcherServlet.class, "/*"); + servletHolder.setInitOrder(2); + + server.addHandler(servletHandler); try { server.start(); diff --git a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/DispatcherServlet.java b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/DispatcherServlet.java index 29e7a3455a575fa0b7d4f4582fa31265e40f2719..ae3c7210bfef06c4548657d8793b5436a0d8a8ba 100644 --- a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/DispatcherServlet.java +++ b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/DispatcherServlet.java @@ -33,16 +33,26 @@ import com.alibaba.dubbo.remoting.http.HttpHandler; */ public class DispatcherServlet extends HttpServlet { - private static final long serialVersionUID = 5766349180380479888L; + private static final long serialVersionUID = 5766349180380479888L; + + private static DispatcherServlet INSTANCE; private static final Map handlers = new ConcurrentHashMap(); - static void addHttpInvoker(int port, HttpHandler processor) { + public static void addHttpHandler(int port, HttpHandler processor) { handlers.put(port, processor); } - static void removeHttpInvoker(int port) { + public static void removeHttpHandler(int port) { handlers.remove(port); + } + + public static DispatcherServlet getInstance() { + return INSTANCE; + } + + public DispatcherServlet() { + DispatcherServlet.INSTANCE = this; } protected void service(HttpServletRequest request, HttpServletResponse response) diff --git a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/ServletHttpServer.java b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/ServletHttpServer.java index 0b9433a85e32948fdac226f0e356c038b10a88b1..fa913556c51286748d984e6900210087384b6244 100644 --- a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/ServletHttpServer.java +++ b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/ServletHttpServer.java @@ -23,7 +23,7 @@ public class ServletHttpServer extends AbstractHttpServer { public ServletHttpServer(URL url, HttpHandler handler){ super(url, handler); - DispatcherServlet.addHttpInvoker(url.getPort(), handler); + DispatcherServlet.addHttpHandler(url.getPort(), handler); } } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-webservice/pom.xml b/dubbo-rpc/dubbo-rpc-webservice/pom.xml index c3e8246757716b7cfafdf6ec5a010b53f0662546..dfd417abe6818207f93fd75e670f207182586607 100644 --- a/dubbo-rpc/dubbo-rpc-webservice/pom.xml +++ b/dubbo-rpc/dubbo-rpc-webservice/pom.xml @@ -34,9 +34,14 @@ dubbo-rpc-api ${project.parent.version} + + com.alibaba + dubbo-remoting-http + ${project.parent.version} + org.apache.cxf - cxf-rt-frontend-jaxws + cxf-rt-frontend-simple org.springframework diff --git a/dubbo-rpc/dubbo-rpc-webservice/src/main/java/com/alibaba/dubbo/rpc/protocol/webservice/WebServiceProtocol.java b/dubbo-rpc/dubbo-rpc-webservice/src/main/java/com/alibaba/dubbo/rpc/protocol/webservice/WebServiceProtocol.java index b1eb312595a38abc6b6bf872c3a85115bd05e2f9..2654b028efd08586223b9b4221e8c5b4ee0c5df3 100644 --- a/dubbo-rpc/dubbo-rpc-webservice/src/main/java/com/alibaba/dubbo/rpc/protocol/webservice/WebServiceProtocol.java +++ b/dubbo-rpc/dubbo-rpc-webservice/src/main/java/com/alibaba/dubbo/rpc/protocol/webservice/WebServiceProtocol.java @@ -17,13 +17,28 @@ package com.alibaba.dubbo.rpc.protocol.webservice; import java.io.IOException; import java.net.SocketTimeoutException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; -import org.apache.cxf.interceptor.LoggingInInterceptor; -import org.apache.cxf.interceptor.LoggingOutInterceptor; -import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; -import org.apache.cxf.jaxws.JaxWsServerFactoryBean; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.cxf.bus.extension.ExtensionManagerBus; +import org.apache.cxf.frontend.ClientProxyFactoryBean; +import org.apache.cxf.frontend.ServerFactoryBean; +import org.apache.cxf.transport.http.HTTPTransportFactory; +import org.apache.cxf.transport.http.HttpDestinationFactory; +import org.apache.cxf.transport.servlet.ServletController; +import org.apache.cxf.transport.servlet.ServletDestinationFactory; import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.remoting.http.HttpBinder; +import com.alibaba.dubbo.remoting.http.HttpHandler; +import com.alibaba.dubbo.remoting.http.HttpServer; +import com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet; +import com.alibaba.dubbo.rpc.RpcContext; import com.alibaba.dubbo.rpc.RpcException; import com.alibaba.dubbo.rpc.protocol.AbstractProxyProtocol; @@ -36,31 +51,78 @@ public class WebServiceProtocol extends AbstractProxyProtocol { public static final int DEFAULT_PORT = 80; + private final Map serverMap = new ConcurrentHashMap(); + + private final ExtensionManagerBus bus = new ExtensionManagerBus(); + + private final HTTPTransportFactory transportFactory = new HTTPTransportFactory(bus); + + private HttpBinder httpBinder; + + public WebServiceProtocol() { + super(IOException.class); + bus.setExtension(new ServletDestinationFactory(), HttpDestinationFactory.class); + } + + public void setHttpBinder(HttpBinder httpBinder) { + this.httpBinder = httpBinder; + } + public int getDefaultPort() { return DEFAULT_PORT; } + private class WebServiceHandler implements HttpHandler { + + private volatile ServletController servletController; + + public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + if (servletController == null) { + HttpServlet httpServlet = DispatcherServlet.getInstance(); + if (httpServlet == null) { + response.sendError(500, "No such DispatcherServlet instance."); + return; + } + synchronized (this) { + if (servletController == null) { + servletController = new ServletController(transportFactory.getRegistry(), httpServlet.getServletConfig(), httpServlet); + } + } + } + RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort()); + servletController.invoke(request, response); + } + + } + protected Runnable doExport(T impl, Class type, URL url) throws RpcException { - final JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean(); - jaxWsServerFactoryBean.setServiceClass(type); - jaxWsServerFactoryBean.setAddress(url.setProtocol("http").toString()); - jaxWsServerFactoryBean.setServiceBean(impl); - jaxWsServerFactoryBean.create(); + String addr = url.getIp() + ":" + url.getPort(); + HttpServer httpServer = serverMap.get(addr); + if (httpServer == null) { + httpServer = httpBinder.bind(url, new WebServiceHandler()); + serverMap.put(addr, httpServer); + } + final ServerFactoryBean serverFactoryBean = new ServerFactoryBean(); + serverFactoryBean.setServiceClass(type); + serverFactoryBean.setAddress(url.getAbsolutePath()); + serverFactoryBean.setServiceBean(impl); + serverFactoryBean.setDestinationFactory(transportFactory); + serverFactoryBean.setBus(bus); + serverFactoryBean.create(); return new Runnable() { public void run() { - jaxWsServerFactoryBean.destroy(); + serverFactoryBean.destroy(); } }; } @SuppressWarnings("unchecked") protected T doRefer(final Class serviceType, final URL url) throws RpcException { - JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); - jaxWsProxyFactoryBean.getInInterceptors().add(new LoggingInInterceptor()); - jaxWsProxyFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); - jaxWsProxyFactoryBean.setServiceClass(serviceType); - jaxWsProxyFactoryBean.setAddress(url.setProtocol("http").toString()); - return (T) jaxWsProxyFactoryBean.create(); + ClientProxyFactoryBean proxyFactoryBean = new ClientProxyFactoryBean(); + proxyFactoryBean.setServiceClass(serviceType); + proxyFactoryBean.setAddress(url.setProtocol("http").toIdentityString()); + proxyFactoryBean.setBus(bus); + return (T) proxyFactoryBean.create(); } protected int getErrorCode(Throwable e) { diff --git a/dubbo/pom.xml b/dubbo/pom.xml index ca6c55a654c7a0b3408f5d4c16c918ef5ae4efb7..e3df1e1cea62c387f26e16551e74dddbe41c7454 100644 --- a/dubbo/pom.xml +++ b/dubbo/pom.xml @@ -140,7 +140,7 @@ org.apache.cxf - cxf-rt-frontend-jaxws + cxf-rt-frontend-simple diff --git a/pom.xml b/pom.xml index fc1b1a65206dbe8505c70584fb14cc9e544725ab..5ddc8702025c429187ed5201bfc69adfc8ddf3fc 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,7 @@ 1.1.10 2.0.0 1.3.6 - 2.6.0 + 2.6.1 0.8.0 1.0.13 4.0.7 @@ -183,7 +183,7 @@ org.apache.cxf - cxf-rt-frontend-jaxws + cxf-rt-frontend-simple ${cxf_version}