升级jetty到9 6/head
authorcuikexiang <cuikexiang@quancheng-ec.com>
Sun, 14 Feb 2016 09:00:14 +0000 (17:00 +0800)
committersdcuike <sdcuike@aol.com>
Thu, 18 Feb 2016 05:54:23 +0000 (13:54 +0800)
update doc

升级jetty到jetty9.跟新配置文档

与原作者版本号一致,还原老版本jetty支持,还原老版本jetty测试用例。push

README.md
pom.xml
src/main/java/com/doctor/dubbo/remoting/http/jetty/JettyHttpBinder.java [new file with mode: 0644]
src/main/java/com/doctor/dubbo/remoting/http/jetty/JettyHttpServer.java [new file with mode: 0644]
src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.remoting.http.HttpBinder [new file with mode: 0644]
src/test/java/com/ofpay/dubbo/rpc/protocol/jsonrpc/JsonRpcProtocolTest.java

index 879b773..7f0c5c1 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,6 +1,7 @@
 [![Build Status](https://travis-ci.org/ofpay/dubbo-rpc-jsonrpc.svg)](https://travis-ci.org/ofpay/dubbo-rpc-jsonrpc)
 
 
+
 ## Why HTTP
 在互联网快速迭代的大潮下,越来越多的公司选择nodejs、django、rails这样的快速脚本框架来开发web端应用
 而后端的服务用Java又是最合适的,这就产生了大量的跨语言的调用需求。  
@@ -65,6 +66,10 @@ Multi protocol:
 Jetty Server: (default)
 ```xml
 <dubbo:protocol ... server="jetty" />
+
+或jetty的最新版:
+<dubbo:protocol ... server="jetty9" />
+
 ```
 Maven:
 ```xml
@@ -78,6 +83,7 @@ Maven:
 Servlet Bridge Server: (recommend)
 ```xml
 <dubbo:protocol ... server="servlet" />
+
 ```
 
 web.xml:
diff --git a/pom.xml b/pom.xml
index 413e643..ca73c80 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -1,20 +1,20 @@
 <!--
- - Copyright 1999-2011 Alibaba Group.
- -  
- - Licensed under the Apache License, Version 2.0 (the "License");
- - you may not use this file except in compliance with the License.
- - You may obtain a copy of the License at
- -  
-      http://www.apache.org/licenses/LICENSE-2.0
- -  
- - Unless required by applicable law or agreed to in writing, software
- - distributed under the License is distributed on an "AS IS" BASIS,
- - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- - See the License for the specific language governing permissions and
- - limitations under the License.
   - Copyright 1999-2011 Alibaba Group.
+    -
   - Licensed under the Apache License, Version 2.0 (the "License");
   - you may not use this file except in compliance with the License.
   - You may obtain a copy of the License at
+    -
   - http://www.apache.org/licenses/LICENSE-2.0
+    -
   - Unless required by applicable law or agreed to in writing, software
   - distributed under the License is distributed on an "AS IS" BASIS,
   - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   - See the License for the specific language governing permissions and
   - limitations under the License.
 -->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-       <modelVersion>4.0.0</modelVersion>
+    <modelVersion>4.0.0</modelVersion>
 
     <parent>
         <groupId>org.sonatype.oss</groupId>
     </parent>
 
     <groupId>com.ofpay</groupId>
-       <artifactId>dubbo-rpc-jsonrpc</artifactId>
+    <artifactId>dubbo-rpc-jsonrpc</artifactId>
     <version>1.0.2-SNAPSHOT</version>
-       <packaging>jar</packaging>
-       <name>${project.artifactId}</name>
-       <description>The Json rpc module of dubbo project</description>
+    <packaging>jar</packaging>
+    <name>${project.artifactId}</name>
+    <description>The Json rpc module of dubbo project</description>
 
     <url>https://github.com/ofpay/dubbo-rpc-jsonrpc</url>
 
 
     <properties>
         <dubbo_version>2.4.10</dubbo_version>
-        <jsonrpc_version>1.1</jsonrpc_version>
-        <jetty_version>6.1.26</jetty_version>
-        <jackson.version>2.0.2</jackson.version>
+        <jsonrpc_version>1.2.0</jsonrpc_version>
+        <jetty_version>9.3.7.v20160115</jetty_version>
+        <mortbay_jetty_version>6.1.26</mortbay_jetty_version>
+        <jackson.version>2.7.1</jackson.version>
         <maven.compiler.source>1.7</maven.compiler.source>
         <maven.compiler.target>1.7</maven.compiler.target>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     </properties>
 
-       <dependencies>
+    <dependencies>
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>dubbo</artifactId>
@@ -91,8 +92,8 @@
 
         <dependency>
             <groupId>javax.servlet</groupId>
-            <artifactId>servlet-api</artifactId>
-            <version>2.5</version>
+            <artifactId>javax.servlet-api</artifactId>
+            <version>3.1.0</version>
             <scope>provided</scope>
         </dependency>
 
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
-            <version>4.11</version>
+            <version>4.12</version>
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>log4j</groupId>
-            <artifactId>log4j</artifactId>
-            <version>1.2.17</version>
-            <scope>test</scope>
+            <groupId>org.eclipse.jetty</groupId>
+            <artifactId>jetty-server</artifactId>
+            <version>${jetty_version}</version>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.eclipse.jetty</groupId>
+            <artifactId>jetty-servlet</artifactId>
+            <version>${jetty_version}</version>
+            <optional>true</optional>
         </dependency>
         <dependency>
             <groupId>org.mortbay.jetty</groupId>
             <artifactId>jetty</artifactId>
-            <version>${jetty_version}</version>
+            <version>${mortbay_jetty_version}</version>
             <optional>true</optional>
         </dependency>
-       </dependencies>
+    </dependencies>
 
     <build>
         <plugins>
diff --git a/src/main/java/com/doctor/dubbo/remoting/http/jetty/JettyHttpBinder.java b/src/main/java/com/doctor/dubbo/remoting/http/jetty/JettyHttpBinder.java
new file mode 100644 (file)
index 0000000..d768080
--- /dev/null
@@ -0,0 +1,20 @@
+package com.doctor.dubbo.remoting.http.jetty;
+
+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;
+
+/**
+ * @author sdcuike
+ *
+ *         time 2016年2月14日 下午3:06:54
+ */
+public class JettyHttpBinder implements HttpBinder {
+
+    @Override
+    public HttpServer bind(URL url, HttpHandler handler) {
+        return new JettyHttpServer(url, handler);
+    }
+
+}
diff --git a/src/main/java/com/doctor/dubbo/remoting/http/jetty/JettyHttpServer.java b/src/main/java/com/doctor/dubbo/remoting/http/jetty/JettyHttpServer.java
new file mode 100644 (file)
index 0000000..27c69cd
--- /dev/null
@@ -0,0 +1,73 @@
+package com.doctor.dubbo.remoting.http.jetty;
+
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.servlet.ServletHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
+import org.eclipse.jetty.util.thread.QueuedThreadPool;
+
+import com.alibaba.dubbo.common.Constants;
+import com.alibaba.dubbo.common.URL;
+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;
+
+/**
+ * @author sdcuike
+ *
+ *         time 2016年2月14日 下午3:07:34
+ */
+public class JettyHttpServer extends AbstractHttpServer {
+    private static final Logger logger = LoggerFactory.getLogger(JettyHttpServer.class);
+
+    private Server server;
+
+    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);
+        threadPool.setMaxThreads(threads);
+        threadPool.setMinThreads(threads);
+
+        server = new Server(threadPool);
+
+        // HTTP connector
+        ServerConnector connector = new ServerConnector(server);
+        if (!url.isAnyHost() && NetUtils.isValidLocalHost(url.getHost())) {
+            connector.setHost(url.getHost());
+        }
+        connector.setPort(url.getPort());
+        // connector.setIdleTimeout(30000);
+        server.addConnector(connector);
+
+        ServletHandler servletHandler = new ServletHandler();
+        ServletHolder servletHolder = servletHandler.addServletWithMapping(DispatcherServlet.class, "/*");
+        servletHolder.setInitOrder(2);
+
+        server.insertHandler(servletHandler);
+
+        try {
+            server.start();
+        } catch (Exception e) {
+            throw new IllegalStateException("Failed to start jetty server on " + url.getAddress() + ", cause: "
+                    + e.getMessage(), e);
+        }
+    }
+
+    public void close() {
+        super.close();
+        if (server != null) {
+            try {
+                server.stop();
+            } catch (Exception e) {
+                logger.warn(e.getMessage(), e);
+            }
+        }
+    }
+}
diff --git a/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.remoting.http.HttpBinder b/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.remoting.http.HttpBinder
new file mode 100644 (file)
index 0000000..8b2b2a5
--- /dev/null
@@ -0,0 +1 @@
+jetty9=com.doctor.dubbo.remoting.http.jetty.JettyHttpBinder
\ No newline at end of file
index 8a8b1a1..6fb5932 100644 (file)
@@ -1,5 +1,9 @@
 package com.ofpay.dubbo.rpc.protocol.jsonrpc;
 
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+
 import com.alibaba.dubbo.common.URL;
 import com.alibaba.dubbo.common.extension.ExtensionLoader;
 import com.alibaba.dubbo.rpc.Exporter;
@@ -7,12 +11,9 @@ import com.alibaba.dubbo.rpc.Invoker;
 import com.alibaba.dubbo.rpc.Protocol;
 import com.alibaba.dubbo.rpc.ProxyFactory;
 import com.ofpay.dubbo.rpc.protocol.jsonrpc.JsonRpcServiceImpl.MyException;
-import org.junit.Assert;
-import org.junit.Test;
 
 /**
- * JsonRpcProtocolTest
- * Created by wuwen on 15/4/1.
+ * JsonRpcProtocolTest Created by wuwen on 15/4/1.
  */
 public class JsonRpcProtocolTest {
 
@@ -33,14 +34,33 @@ public class JsonRpcProtocolTest {
         exporter.unexport();
     }
 
-    /**
-     *TODO 暂不支持自定义异常
     @Test
+    public void testJsonrpcProtocolForServerJetty9() {
+        JsonRpcServiceImpl server = new JsonRpcServiceImpl();
+        Assert.assertFalse(server.isCalled());
+        ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
+        Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
+        URL url = URL.valueOf("jsonrpc://127.0.0.1:5342/" + JsonRpcService.class.getName() + "?version=1.0.0&server=jetty9");
+        Exporter<JsonRpcService> exporter = protocol.export(proxyFactory.getInvoker(server, JsonRpcService.class, url));
+        Invoker<JsonRpcService> invoker = protocol.refer(JsonRpcService.class, url);
+        JsonRpcService client = proxyFactory.getProxy(invoker);
+        String result = client.sayHello("haha");
+        Assert.assertTrue(server.isCalled());
+        Assert.assertEquals("Hello, haha", result);
+        invoker.destroy();
+        exporter.unexport();
+    }
+
+    // TODO 暂不支持自定义异常
+
+    @Test
+    @Ignore
     public void testCustomException() {
         JsonRpcServiceImpl server = new JsonRpcServiceImpl();
         ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
         Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
-        URL url = URL.valueOf("jsonrpc://127.0.0.1:5342/" + JsonRpcService.class.getName() + "?version=1.0.0");
+        URL url = URL.valueOf("jsonrpc://127.0.0.1:5342/" +
+                JsonRpcService.class.getName() + "?version=1.0.0&server=jetty9");
         Exporter<JsonRpcService> exporter = protocol.export(proxyFactory.getInvoker(server, JsonRpcService.class, url));
         Invoker<JsonRpcService> invoker = protocol.refer(JsonRpcService.class, url);
         JsonRpcService client = proxyFactory.getProxy(invoker);
@@ -51,6 +71,6 @@ public class JsonRpcProtocolTest {
         }
         invoker.destroy();
         exporter.unexport();
-    }***/
+    }
 
 }
\ No newline at end of file