Skip to content
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

[Enhancement] JsonRpcProtocol support Generalization #4595 #4596

Merged
merged 7 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
import com.googlecode.jsonrpc4j.JsonRpcClientException;
import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.googlecode.jsonrpc4j.spring.JsonProxyFactoryBean;
import org.apache.dubbo.rpc.service.GenericService;
import org.apache.dubbo.rpc.support.ProtocolUtils;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.remoting.support.RemoteInvocation;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -39,8 +42,9 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HttpProtocol extends AbstractProxyProtocol {
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;

public class HttpProtocol 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";
Expand Down Expand Up @@ -82,9 +86,9 @@ public void handle(HttpServletRequest request, HttpServletResponse response)
response.setHeader(ACCESS_CONTROL_ALLOW_METHODS_HEADER, "POST");
response.setHeader(ACCESS_CONTROL_ALLOW_HEADERS_HEADER, "*");
}
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
response.setStatus(200);
} else if ("POST".equalsIgnoreCase(request.getMethod())) {
} else if (request.getMethod().equalsIgnoreCase("POST")) {

RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort());
try {
Expand All @@ -108,17 +112,38 @@ protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcExcept
serverMap.put(addr, server);
}
final String path = url.getAbsolutePath();
final String genericPath = path + "/" + GENERIC_KEY;
JsonRpcServer skeleton = new JsonRpcServer(impl, type);
JsonRpcServer genericServer = new JsonRpcServer(impl, GenericService.class);
skeletonMap.put(path, skeleton);
return () -> skeletonMap.remove(path);
skeletonMap.put(genericPath, genericServer);
return () -> {
skeletonMap.remove(path);
skeletonMap.remove(genericPath);
};
}

@SuppressWarnings("unchecked")
@Override
protected <T> T doRefer(final Class<T> serviceType, URL url) throws RpcException {
final String generic = url.getParameter(GENERIC_KEY);
final boolean isGeneric = ProtocolUtils.isGeneric(generic) || serviceType.equals(GenericService.class);
JsonProxyFactoryBean jsonProxyFactoryBean = new JsonProxyFactoryBean();
jsonProxyFactoryBean.setServiceUrl(url.setProtocol("http").toIdentityString());
jsonProxyFactoryBean.setServiceInterface(serviceType);
JsonRpcProxyFactoryBean jsonRpcProxyFactoryBean = new JsonRpcProxyFactoryBean(jsonProxyFactoryBean);
jsonRpcProxyFactoryBean.setRemoteInvocationFactory((methodInvocation) -> {
RemoteInvocation invocation = new JsonRemoteInvocation(methodInvocation);
if (isGeneric) {
invocation.addAttribute(GENERIC_KEY, generic);
}
return invocation;
});
String key = url.setProtocol("http").toIdentityString();
if (isGeneric) {
key = key + "/" + GENERIC_KEY;
}

jsonRpcProxyFactoryBean.setServiceUrl(key);
jsonRpcProxyFactoryBean.setServiceInterface(serviceType);

jsonProxyFactoryBean.afterPropertiesSet();
return (T) jsonProxyFactoryBean.getObject();
Expand Down Expand Up @@ -160,4 +185,5 @@ public void destroy() {
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.http;

import org.aopalliance.intercept.MethodInvocation;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.remoting.support.RemoteInvocation;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;

/**
* JsonRemoteInvocation
*/
public class JsonRemoteInvocation extends RemoteInvocation {
private static final long serialVersionUID = 1L;
private static final String DUBBO_ATTACHMENTS_ATTR_NAME = "dubbo.attachments";

public JsonRemoteInvocation(MethodInvocation methodInvocation) {
super(methodInvocation);
addAttribute(DUBBO_ATTACHMENTS_ATTR_NAME, new HashMap<>(RpcContext.getContext().getAttachments()));
}

@Override
public Object invoke(Object targetObject) throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
RpcContext context = RpcContext.getContext();
context.setAttachments((Map<String, String>) getAttribute(DUBBO_ATTACHMENTS_ATTR_NAME));

String generic = (String) getAttribute(GENERIC_KEY);
if (StringUtils.isNotEmpty(generic)) {
context.setAttachment(GENERIC_KEY, generic);
}
try {
return super.invoke(targetObject);
} finally {
context.setAttachments(null);

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.http;

import com.googlecode.jsonrpc4j.spring.JsonProxyFactoryBean;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.remoting.support.RemoteInvocationBasedAccessor;

/**
* JsonRpcProxyFactoryBean
*/
public class JsonRpcProxyFactoryBean extends RemoteInvocationBasedAccessor
implements MethodInterceptor,
InitializingBean,
FactoryBean<Object>,
ApplicationContextAware {
private final JsonProxyFactoryBean jsonProxyFactoryBean;

public JsonRpcProxyFactoryBean(JsonProxyFactoryBean factoryBean) {
this.jsonProxyFactoryBean = factoryBean;
}

@Override
@SuppressWarnings("unchecked")
public void afterPropertiesSet() {
jsonProxyFactoryBean.afterPropertiesSet();
}

@Override
public Object invoke(MethodInvocation invocation)
throws Throwable {

return jsonProxyFactoryBean.invoke(invocation);
}

@Override
public Object getObject() {
return jsonProxyFactoryBean.getObject();
}

@Override
public Class<?> getObjectType() {
return jsonProxyFactoryBean.getObjectType();
}

@Override
public boolean isSingleton() {
return jsonProxyFactoryBean.isSingleton();
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
jsonProxyFactoryBean.setApplicationContext(applicationContext);
}

@Override
public void setServiceUrl(String serviceUrl) {
jsonProxyFactoryBean.setServiceUrl(serviceUrl);
}

@Override
public void setServiceInterface(Class<?> serviceInterface) {
jsonProxyFactoryBean.setServiceInterface(serviceInterface);
}

}
Original file line number Diff line number Diff line change
@@ -1,69 +1,88 @@
/*
* 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.http;


import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class HttpProtocolTest {

@Test
public void testJsonrpcProtocol() {
HttpServiceImpl server = new HttpServiceImpl();
assertFalse(server.isCalled());
ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?version=1.0.0");
Exporter<HttpService> exporter = protocol.export(proxyFactory.getInvoker(server, HttpService.class, url));
Invoker<HttpService> invoker = protocol.refer(HttpService.class, url);
HttpService client = proxyFactory.getProxy(invoker);
String result = client.sayHello("haha");
assertTrue(server.isCalled());
assertEquals("Hello, haha", result);
invoker.destroy();
exporter.unexport();
}

@Test
public void testJsonrpcProtocolForServerJetty() {
HttpServiceImpl server = new HttpServiceImpl();
assertFalse(server.isCalled());
ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?version=1.0.0&server=jetty");
Exporter<HttpService> exporter = protocol.export(proxyFactory.getInvoker(server, HttpService.class, url));
Invoker<HttpService> invoker = protocol.refer(HttpService.class, url);
HttpService client = proxyFactory.getProxy(invoker);
String result = client.sayHello("haha");
assertTrue(server.isCalled());
assertEquals("Hello, haha", result);
invoker.destroy();
exporter.unexport();
}

/*
* 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.http;


import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;

import org.apache.dubbo.rpc.service.GenericService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class HttpProtocolTest {

@Test
public void testJsonrpcProtocol() {
HttpServiceImpl server = new HttpServiceImpl();
assertFalse(server.isCalled());
ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?version=1.0.0");
Exporter<HttpService> exporter = protocol.export(proxyFactory.getInvoker(server, HttpService.class, url));
Invoker<HttpService> invoker = protocol.refer(HttpService.class, url);
HttpService client = proxyFactory.getProxy(invoker);
String result = client.sayHello("haha");
assertTrue(server.isCalled());
assertEquals("Hello, haha", result);
invoker.destroy();
exporter.unexport();
}

@Test
public void testJsonrpcProtocolForServerJetty() {
HttpServiceImpl server = new HttpServiceImpl();
assertFalse(server.isCalled());
ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?version=1.0.0&server=jetty");
Exporter<HttpService> exporter = protocol.export(proxyFactory.getInvoker(server, HttpService.class, url));
Invoker<HttpService> invoker = protocol.refer(HttpService.class, url);
HttpService client = proxyFactory.getProxy(invoker);
String result = client.sayHello("haha");
assertTrue(server.isCalled());
assertEquals("Hello, haha", result);
invoker.destroy();
exporter.unexport();
}

@Test
public void testGenericInvoke() {
HttpServiceImpl server = new HttpServiceImpl();
Assertions.assertFalse(server.isCalled());
ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?release=2.7.0");
Exporter<HttpService> exporter = protocol.export(proxyFactory.getInvoker(server, HttpService.class, url));
Invoker<GenericService> invoker = protocol.refer(GenericService.class, url);
GenericService client = proxyFactory.getProxy(invoker, true);
String result = (String) client.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"haha"});
Assertions.assertTrue(server.isCalled());
Assertions.assertEquals("Hello, haha", result);
invoker.destroy();
exporter.unexport();
}

}