-
Notifications
You must be signed in to change notification settings - Fork 26.5k
Add JSON-RPC protocol #3786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add JSON-RPC protocol #3786
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,61 @@ | ||
| <!-- | ||
| Licensed to the Apache Software Foundation (ASF) under one or more | ||
| contributor license agreements. See the NOTICE file distributed with | ||
| this work for additional information regarding copyright ownership. | ||
| The ASF licenses this file to You 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/xsd/maven-4.0.0.xsd"> | ||
| <parent> | ||
| <artifactId>dubbo-rpc</artifactId> | ||
| <groupId>org.apache.dubbo</groupId> | ||
| <version>2.7.2-SNAPSHOT</version> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <artifactId>dubbo-rpc-jsonrpc</artifactId> | ||
|
|
||
| <description>The JSON-RPC module of dubbo project</description> | ||
|
|
||
| <properties> | ||
| <skip_maven_deploy>false</skip_maven_deploy> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.dubbo</groupId> | ||
| <artifactId>dubbo-rpc-api</artifactId> | ||
| <version>${project.parent.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.dubbo</groupId> | ||
| <artifactId>dubbo-remoting-http</artifactId> | ||
| <version>${project.parent.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework</groupId> | ||
| <artifactId>spring-context</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.github.briandilley.jsonrpc4j</groupId> | ||
| <artifactId>jsonrpc4j</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>javax.portlet</groupId> | ||
| <artifactId>portlet-api</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
|
|
||
| </project> | ||
163 changes: 163 additions & 0 deletions
163
...ubbo-rpc-jsonrpc/src/main/java/org/apache/dubbo/rpc/protocol/jsonrpc/JsonRpcProtocol.java
This file contains hidden or 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,163 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.dubbo.rpc.protocol.jsonrpc; | ||
|
|
||
| import org.apache.dubbo.common.URL; | ||
| import org.apache.dubbo.remoting.http.HttpBinder; | ||
| import org.apache.dubbo.remoting.http.HttpHandler; | ||
| import org.apache.dubbo.remoting.http.HttpServer; | ||
| import org.apache.dubbo.rpc.RpcContext; | ||
| import org.apache.dubbo.rpc.RpcException; | ||
| import org.apache.dubbo.rpc.protocol.AbstractProxyProtocol; | ||
|
|
||
| import com.googlecode.jsonrpc4j.HttpException; | ||
| import com.googlecode.jsonrpc4j.JsonRpcClientException; | ||
| import com.googlecode.jsonrpc4j.JsonRpcServer; | ||
| import com.googlecode.jsonrpc4j.spring.JsonProxyFactoryBean; | ||
| import org.springframework.remoting.RemoteAccessException; | ||
|
|
||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import java.io.IOException; | ||
| import java.net.SocketTimeoutException; | ||
| import java.util.ArrayList; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| public class JsonRpcProtocol extends AbstractProxyProtocol { | ||
|
|
||
| public static final String ACCESS_CONTROL_ALLOW_ORIGIN_HEADER = "Access-Control-Allow-Origin"; | ||
| public static final String ACCESS_CONTROL_ALLOW_METHODS_HEADER = "Access-Control-Allow-Methods"; | ||
| public static final String ACCESS_CONTROL_ALLOW_HEADERS_HEADER = "Access-Control-Allow-Headers"; | ||
|
|
||
| private final Map<String, HttpServer> serverMap = new ConcurrentHashMap<>(); | ||
|
|
||
| private final Map<String, JsonRpcServer> skeletonMap = new ConcurrentHashMap<>(); | ||
|
|
||
| private HttpBinder httpBinder; | ||
|
|
||
| public JsonRpcProtocol() { | ||
| super(HttpException.class, JsonRpcClientException.class); | ||
| } | ||
|
|
||
| public void setHttpBinder(HttpBinder httpBinder) { | ||
| this.httpBinder = httpBinder; | ||
| } | ||
|
|
||
| @Override | ||
| public int getDefaultPort() { | ||
| return 80; | ||
| } | ||
|
|
||
| private class InternalHandler implements HttpHandler { | ||
|
|
||
| private boolean cors; | ||
|
|
||
| public InternalHandler(boolean cors) { | ||
| this.cors = cors; | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException { | ||
| String uri = request.getRequestURI(); | ||
| JsonRpcServer skeleton = skeletonMap.get(uri); | ||
| if (cors) { | ||
| response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*"); | ||
| response.setHeader(ACCESS_CONTROL_ALLOW_METHODS_HEADER, "POST"); | ||
| response.setHeader(ACCESS_CONTROL_ALLOW_HEADERS_HEADER, "*"); | ||
| } | ||
| if (request.getMethod().equalsIgnoreCase("OPTIONS")) { | ||
| response.setStatus(200); | ||
| } else if (request.getMethod().equalsIgnoreCase("POST")) { | ||
|
|
||
| RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort()); | ||
| try { | ||
| skeleton.handle(request.getInputStream(), response.getOutputStream()); | ||
| } catch (Throwable e) { | ||
| throw new ServletException(e); | ||
| } | ||
| } else { | ||
| response.setStatus(500); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException { | ||
| String addr = url.getIp() + ":" + url.getPort(); | ||
| HttpServer server = serverMap.get(addr); | ||
| if (server == null) { | ||
| server = httpBinder.bind(url, new InternalHandler(url.getParameter("cors", false))); | ||
| serverMap.put(addr, server); | ||
| } | ||
| final String path = url.getAbsolutePath(); | ||
| JsonRpcServer skeleton = new JsonRpcServer(impl, type); | ||
| skeletonMap.put(path, skeleton); | ||
| return () -> skeletonMap.remove(path); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| protected <T> T doRefer(final Class<T> serviceType, URL url) throws RpcException { | ||
| JsonProxyFactoryBean jsonProxyFactoryBean = new JsonProxyFactoryBean(); | ||
| jsonProxyFactoryBean.setServiceUrl(url.setProtocol("http").toIdentityString()); | ||
| jsonProxyFactoryBean.setServiceInterface(serviceType); | ||
|
|
||
| jsonProxyFactoryBean.afterPropertiesSet(); | ||
| return (T) jsonProxyFactoryBean.getObject(); | ||
| } | ||
|
|
||
| @Override | ||
| protected int getErrorCode(Throwable e) { | ||
| if (e instanceof RemoteAccessException) { | ||
| e = e.getCause(); | ||
| } | ||
| if (e != null) { | ||
| Class<?> cls = e.getClass(); | ||
| if (SocketTimeoutException.class.equals(cls)) { | ||
| return RpcException.TIMEOUT_EXCEPTION; | ||
| } else if (IOException.class.isAssignableFrom(cls)) { | ||
| return RpcException.NETWORK_EXCEPTION; | ||
| } else if (ClassNotFoundException.class.isAssignableFrom(cls)) { | ||
| return RpcException.SERIALIZATION_EXCEPTION; | ||
| } | ||
| } | ||
| return super.getErrorCode(e); | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() { | ||
| super.destroy(); | ||
| for (String key : new ArrayList<>(serverMap.keySet())) { | ||
| HttpServer server = serverMap.remove(key); | ||
| if (server != null) { | ||
| try { | ||
| if (logger.isInfoEnabled()) { | ||
| logger.info("Close jsonrpc server " + server.getUrl()); | ||
| } | ||
| server.close(); | ||
| } catch (Throwable t) { | ||
| logger.warn(t.getMessage(), t); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
1 change: 1 addition & 0 deletions
1
...bbo-rpc-jsonrpc/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.rpc.Protocol
This file contains hidden or 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 @@ | ||
| jsonrpc=org.apache.dubbo.rpc.protocol.jsonrpc.JsonRpcProtocol |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.