-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #458 from dongfangshangren/master
add restful protocol support by integrate jboss resteasy and testcase
- Loading branch information
Showing
33 changed files
with
1,955 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
motan-extension/protocol-extension/motan-protocol-restful/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
~ Copyright 2009-2016 Weibo, Inc. | ||
~ | ||
~ 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 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.weibo</groupId> | ||
<artifactId>protocol-extension</artifactId> | ||
<version>0.3.1-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>motan-protocol-restful</artifactId> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<resteasy.version>3.0.7.Final</resteasy.version> | ||
<junit.version>4.12</junit.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.tomcat.embed</groupId> | ||
<artifactId>tomcat-embed-core</artifactId> | ||
<version>7.0.75</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.tomcat.embed</groupId> | ||
<artifactId>tomcat-embed-logging-juli</artifactId> | ||
<version>7.0.75</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.weibo</groupId> | ||
<artifactId>motan-core</artifactId> | ||
<version>${project.parent.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>javax.ws.rs</groupId> | ||
<artifactId>javax.ws.rs-api</artifactId> | ||
<version>2.0.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>3.1.0</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-netty</artifactId> | ||
<version>${resteasy.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-jaxrs</artifactId> | ||
<version>${resteasy.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>net.jcip</groupId> | ||
<artifactId>jcip-annotations</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>org.jboss.spec.javax.annotation</groupId> | ||
<artifactId>jboss-annotations-api_1.1_spec</artifactId> | ||
</exclusion> | ||
<!-- multipart使用 --> | ||
<exclusion> | ||
<groupId>javax.activation</groupId> | ||
<artifactId>activation</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-client</artifactId> | ||
<version>${resteasy.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-jackson2-provider</artifactId> | ||
<version>${resteasy.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
100 changes: 100 additions & 0 deletions
100
motan-extension/protocol-extension/motan-protocol-restful/restful.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
集成jboss resteasy支持restful协议 | ||
|
||
### 功能支持 | ||
1. 支持rpc单独进程和部署到servlet容器中 | ||
2. 完全支持原有服务治理功能 | ||
3. 支持rpc request/response的attachment机制 | ||
4. 完全支持rpc filter机制 | ||
5. rest服务编程完全按照JAX-RS代码方式编写 | ||
|
||
### 快速入门 | ||
|
||
服务接口 | ||
|
||
```java | ||
@Path("/rest") | ||
public interface HelloResource{ | ||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
List<User> hello(@CookieParam("uid") int uid); | ||
|
||
@GET | ||
@Path("/primitive") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
String testPrimitiveType(); | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
Response add(@FormParam("id") int id, @FormParam("name") String name); | ||
|
||
@GET | ||
@Path("/exception") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
void testException(); | ||
``` | ||
|
||
服务实现 | ||
|
||
```java | ||
public class RestHelloResource implements HelloResource{ | ||
|
||
public List<User> hello(int id){ | ||
return Arrays.asList(new User(id, "de")); | ||
} | ||
|
||
@Override | ||
public String testPrimitiveType(){ | ||
return "helloworld"; | ||
} | ||
|
||
@Override | ||
public Response add(int id, String name){ | ||
return Response.ok().cookie(new NewCookie("ck", String.valueOf(id))).entity(new User(id, name)).build(); | ||
} | ||
|
||
@Override | ||
public void testException(){ | ||
throw new UnsupportedOperationException("unsupport"); | ||
} | ||
|
||
} | ||
``` | ||
|
||
### 配置restserver | ||
|
||
#### 独立rpc进程方式 | ||
|
||
`<motan:protocol name="restful" endpointFactory="netty" />` | ||
|
||
#### 集成到java应用服务器中(如部署到tomcat中) | ||
|
||
此时需要注意contextpath问题 | ||
|
||
`<motan:protocol name="restful" endpointFactory="servlet" />` 服务端还需配置web.xml如下: | ||
|
||
```xml | ||
|
||
<!-- 此filter必须在spring ContextLoaderFilter之前 --> | ||
<listener> | ||
<listener-class>com.weibo.api.motan.protocol.restful.support.servlet.RestfulServletContainerListener</listener-class> | ||
</listener> | ||
|
||
<servlet> | ||
<servlet-name>dispatcher</servlet-name> | ||
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> | ||
<load-on-startup>1</load-on-startup> | ||
<init-param> | ||
<param-name>resteasy.servlet.mapping.prefix</param-name> | ||
<param-value>/servlet</param-value> <!-- 此处实际为servlet-mapping的url-pattern,具体配置见resteasy文档--> | ||
</init-param> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>dispatcher</servlet-name> | ||
<url-pattern>/servlet/*</url-pattern> | ||
</servlet-mapping> | ||
``` | ||
此时如果使用rpc客户端,则需要注意contextpath配置:`<motan:protocol name="restful" contextpath="/serverContextPath/dispatcherServletUrlPattern" />`, | ||
假如服务端部署的ContextPath为`/myserver`,servlet的url-pattern为`/servlet/*`,则客户端的contextpath则应配置为`/myserver/servlet` |
48 changes: 48 additions & 0 deletions
48
...-protocol-restful/src/main/java/com/weibo/api/motan/protocol/restful/EmbedRestServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2009-2016 Weibo, Inc. | ||
* | ||
* 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. | ||
*/ | ||
package com.weibo.api.motan.protocol.restful; | ||
|
||
import org.jboss.resteasy.plugins.server.embedded.EmbeddedJaxrsServer; | ||
import org.jboss.resteasy.spi.ResteasyDeployment; | ||
|
||
/** | ||
* 内嵌resteasy server | ||
* | ||
* @author zhouhaocheng | ||
* | ||
*/ | ||
public class EmbedRestServer implements RestServer { | ||
private EmbeddedJaxrsServer server; | ||
|
||
public EmbedRestServer(EmbeddedJaxrsServer server) { | ||
this.server = server; | ||
} | ||
|
||
@Override | ||
public ResteasyDeployment getDeployment() { | ||
return server.getDeployment(); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
server.start(); | ||
} | ||
|
||
public void stop() { | ||
server.stop(); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...-protocol-restful/src/main/java/com/weibo/api/motan/protocol/restful/EndpointFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2009-2016 Weibo, Inc. | ||
* | ||
* 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. | ||
*/ | ||
package com.weibo.api.motan.protocol.restful; | ||
|
||
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; | ||
|
||
import com.weibo.api.motan.core.extension.Scope; | ||
import com.weibo.api.motan.core.extension.Spi; | ||
import com.weibo.api.motan.rpc.URL; | ||
|
||
@Spi(scope = Scope.SINGLETON) | ||
public interface EndpointFactory { | ||
|
||
RestServer createServer(URL url); | ||
|
||
ResteasyWebTarget createClient(URL url); | ||
|
||
void safeReleaseResource(RestServer server, URL url); | ||
|
||
void safeReleaseResource(ResteasyWebTarget client, URL url); | ||
|
||
} |
Oops, something went wrong.