Skip to content

Commit

Permalink
Merge pull request #458 from dongfangshangren/master
Browse files Browse the repository at this point in the history
add restful protocol support by integrate jboss resteasy and testcase
  • Loading branch information
rayzhang0603 authored Jul 10, 2017
2 parents 314f1d2 + 60e0e7a commit 1801fc6
Show file tree
Hide file tree
Showing 33 changed files with 1,955 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import com.weibo.api.motan.rpc.URL;

/**
*
*
* Decorate the protocol, to add more features.
*
* @author fishermen
Expand Down Expand Up @@ -135,7 +135,7 @@ public URL getServiceUrl() {
return lastRef;
}

private <T> Provider<T> decorateWithFilter(Provider<T> provider, URL url) {
private <T> Provider<T> decorateWithFilter(final Provider<T> provider, URL url) {
List<Filter> filters = getFilters(url, MotanConstants.NODE_TYPE_SERVICE);
if (filters == null || filters.size() == 0) {
return provider;
Expand Down Expand Up @@ -179,6 +179,11 @@ public void init() {
public boolean isAvailable() {
return lp.isAvailable();
}

@Override
public T getImpl() {
return provider.getImpl();
}
};
}
return lastProvider;
Expand All @@ -191,7 +196,7 @@ public boolean isAvailable() {
* 2)根据filter配置获取新的filters,并和默认的filter列表合并;
* 3)再根据一些其他配置判断是否需要增加其他filter,如根据accessLog进行判断,是否需要增加accesslog
* </pre>
*
*
* @param url
* @param key
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* @author maijunsheng
* @version 创建时间:2013-5-23
*
*
*/
@SpiMeta(name = "motan")
public class DefaultProvider<T> extends AbstractProvider<T> {
Expand All @@ -38,6 +38,11 @@ public DefaultProvider(T proxyImpl, URL url, Class<T> clz) {
this.proxyImpl = proxyImpl;
}

@Override
public T getImpl(){
return proxyImpl;
}

@Override
public Response invoke(Request request) {
DefaultResponse response = new DefaultResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
import com.weibo.api.motan.core.extension.Spi;

/**
*
*
* Service provider.
*
*
* @author fishermen
* @version V1.0 created at: 2013-5-16
*/
@Spi(scope = Scope.PROTOTYPE)
public interface Provider<T> extends Caller<T> {

Class<T> getInterface();

T getImpl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public void testProtocol() {
}

defaultRpcProtocol.export(new Provider<IHello>() {
private IHello hello = new Hello();
@Override
public Response call(Request request) {
IHello hello = new Hello();
hello.hello();
return new DefaultResponse("hello");
}
Expand Down Expand Up @@ -98,6 +98,11 @@ public Class<IHello> getInterface() {
return IHello.class;
}

@Override
public IHello getImpl() {
return hello;
}

}, url);

Referer<IHello> referer = defaultRpcProtocol.refer(IHello.class, url);
Expand Down
107 changes: 107 additions & 0 deletions motan-extension/protocol-extension/motan-protocol-restful/pom.xml
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 motan-extension/protocol-extension/motan-protocol-restful/restful.md
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`
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();
}

}
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);

}
Loading

0 comments on commit 1801fc6

Please sign in to comment.