Skip to content

Commit

Permalink
Merge pull request #702 from sunnights/dev
Browse files Browse the repository at this point in the history
fix: common client only support for motan2
  • Loading branch information
rayzhang0603 authored May 17, 2018
2 parents 7ef9d73 + 3f4d126 commit 5eeade4
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@

package com.weibo.api.motan.config;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.config.annotation.ConfigDesc;
import com.weibo.api.motan.exception.MotanErrorMsgConstant;
import com.weibo.api.motan.exception.MotanFrameworkException;
import com.weibo.api.motan.util.LoggerUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;

/**
*
Expand Down Expand Up @@ -94,8 +93,7 @@ protected void appendConfigParams(Map<String, String> parameters, String prefix)
String name = method.getName();
if (isConfigMethod(method)) {
int idx = name.startsWith("get") ? 3 : 2;
String prop = name.substring(idx, idx + 1).toLowerCase() + name.substring(idx + 1);
String key = prop;
String key = name.substring(idx, idx + 1).toLowerCase() + name.substring(idx + 1);
ConfigDesc configDesc = method.getAnnotation(ConfigDesc.class);
if (configDesc != null && !StringUtils.isBlank(configDesc.key())) {
key = configDesc.key();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ public synchronized void initRef() {

checkInterfaceAndMethods(interfaceClass, methods);

clusterSupports = new ArrayList<ClusterSupport<T>>(protocols.size());
List<Cluster<T>> clusters = new ArrayList<Cluster<T>>(protocols.size());
clusterSupports = new ArrayList<>(protocols.size());
List<Cluster<T>> clusters = new ArrayList<>(protocols.size());
String proxy = null;

ConfigHandler configHandler = ExtensionLoader.getExtensionLoader(ConfigHandler.class).getExtension(MotanConstants.DEFAULT_VALUE);

List<URL> registryUrls = loadRegistryUrls();
String localIp = getLocalHostAddress(registryUrls);
for (ProtocolConfig protocol : protocols) {
Map<String, String> params = new HashMap<String, String>();
Map<String, String> params = new HashMap<>();
params.put(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_REFERER);
params.put(URLParamType.version.getName(), URLParamType.version.getValue());
params.put(URLParamType.refreshTimestamp.getName(), String.valueOf(System.currentTimeMillis()));
Expand All @@ -140,8 +140,10 @@ public synchronized void initRef() {
clusterSupports.add(clusterSupport);
clusters.add(clusterSupport.getCluster());

proxy = (proxy == null) ? refUrl.getParameter(URLParamType.proxy.getName(), URLParamType.proxy.getValue()) : proxy;

if (proxy == null) {
String defaultValue = StringUtils.isBlank(serviceInterface) ? URLParamType.proxy.getValue() : MotanConstants.PROXY_COMMON;
proxy = refUrl.getParameter(URLParamType.proxy.getName(), defaultValue);
}
}

ref = configHandler.refer(interfaceClass, clusters, proxy);
Expand All @@ -150,7 +152,7 @@ public synchronized void initRef() {
}

private ClusterSupport<T> createClusterSupport(URL refUrl, ConfigHandler configHandler, List<URL> registryUrls) {
List<URL> regUrls = new ArrayList<URL>();
List<URL> regUrls = new ArrayList<>();

// 如果用户指定directUrls 或者 injvm协议访问,则使用local registry
if (StringUtils.isNotBlank(directUrl) || MotanConstants.PROTOCOL_INJVM.equals(refUrl.getProtocol())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.weibo.api.motan.cluster.Cluster;
import com.weibo.api.motan.rpc.DefaultRequest;
import com.weibo.api.motan.rpc.Request;
import com.weibo.api.motan.util.ReflectUtil;
import com.weibo.api.motan.util.RequestIdGenerator;

import java.util.List;
Expand All @@ -27,7 +26,6 @@ public Object call(String methodName, Object[] arguments, Class returnType, Map<
request.setMethodName(methodName);
request.setArguments(arguments);
request.setAttachments(attachments);
request.setParamtersDesc(ReflectUtil.getParamsDesc(arguments));
return invokeRequest(request, returnType, async);
}

Expand Down Expand Up @@ -58,7 +56,6 @@ public Request buildRequest(String methodName, Object[] arguments) {
request.setInterfaceName(interfaceName);
request.setMethodName(methodName);
request.setArguments(arguments);
request.setParamtersDesc(ReflectUtil.getParamsDesc(arguments));
return request;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ public static String getMethodParamDesc(Method method) {
return builder.substring(0, builder.length() - 1);
}

public static String getParamsDesc(Object[] params) {
StringBuilder builder = new StringBuilder();
for (Object param : params) {
String className = getName(param.getClass());
builder.append(className).append(PARAM_CLASS_SPLIT);
}
return builder.substring(0, builder.length() - 1);
}

/**
* 获取方法的标示 : method_name + "(" + paramDesc + ")"
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,21 @@

package com.weibo.motan.demo.client;

import com.weibo.api.motan.proxy.CommonHandler;
import com.weibo.api.motan.rpc.Future;
import com.weibo.api.motan.rpc.FutureListener;
import com.weibo.api.motan.rpc.Request;
import com.weibo.api.motan.rpc.ResponseFuture;
import com.weibo.motan.demo.service.MotanDemoService;
import com.weibo.motan.demo.service.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoRpcClient {

public static void main(String[] args) throws Throwable {
public static void main(String[] args) throws InterruptedException {

ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:motan_demo_client.xml"});

MotanDemoService service = (MotanDemoService) ctx.getBean("motanDemoReferer");
System.out.println(service.hello("motan"));

CommonHandler client = (CommonHandler) ctx.getBean("motanDemoReferer2");
User user = new User(1, "AAA");
System.out.println(user);

user = (User) client.call("rename", new Object[]{user, "BBB"}, User.class);
System.out.println(user);

ResponseFuture future = (ResponseFuture) client.asyncCall("rename", new Object[]{user, "CCC"}, User.class);
user = (User) future.getValue();
System.out.println(user);

ResponseFuture future2 = (ResponseFuture) client.asyncCall("rename", new Object[]{user, "DDD"}, User.class);
future2.addListener(new FutureListener() {
@Override
public void operationComplete(Future future) {
System.out.println(future.getValue());
}
});

Request request = client.buildRequest("rename", new Object[]{user, "EEE"});
request.setAttachment("a", "a");
user = (User) client.call(request, User.class);
System.out.println(user);

for (int i = 0; i < Integer.MAX_VALUE; i++) {
System.out.println(service.hello("motan" + i));
Thread.sleep(1000);
}
System.out.println("motan demo is finish.");
System.exit(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,43 @@

package com.weibo.motan.demo.client;

import com.weibo.api.motan.config.ProtocolConfig;
import com.weibo.api.motan.config.RefererConfig;
import com.weibo.api.motan.config.RegistryConfig;
import com.weibo.api.motan.proxy.CommonHandler;
import com.weibo.api.motan.rpc.Future;
import com.weibo.api.motan.rpc.FutureListener;
import com.weibo.api.motan.rpc.Request;
import com.weibo.api.motan.rpc.ResponseFuture;
import com.weibo.motan.demo.service.MotanDemoService;
import com.weibo.motan.demo.service.PbParamService;
import com.weibo.motan.demo.service.model.User;
import io.grpc.examples.routeguide.Point;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Motan2RpcClient {

public static void main(String[] args) throws InterruptedException {

public static void main(String[] args) throws Throwable {
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:motan2_demo_client.xml"});

// hessian
MotanDemoService service = (MotanDemoService) ctx.getBean("motanDemoReferer");
print(service);

// simple serialization
service = (MotanDemoService) ctx.getBean("motanDemoReferer-simple");
print(service);

//pb serialization
// pb serialization
PbParamService pbService = (PbParamService) ctx.getBean("motanDemoReferer-pb");
System.out.println(pbService.getFeature(Point.newBuilder().setLatitude(123).setLongitude(456).build()));

// common client
CommonHandler xmlClient = (CommonHandler) ctx.getBean("motanDemoReferer-common-client");
motan2XmlCommonClientDemo(xmlClient);
motan2ApiCommonClientDemo();

System.out.println("motan demo is finish.");
System.exit(0);
}
Expand All @@ -52,4 +66,62 @@ public static void print(MotanDemoService service) throws InterruptedException {
}
}

public static void motan2XmlCommonClientDemo(CommonHandler client) throws Throwable {
System.out.println(client.call("hello", new Object[]{"a"}, String.class));

User user = new User(1, "AAA");
System.out.println(user);

user = (User) client.call("rename", new Object[]{user, "BBB"}, User.class);
System.out.println(user);

ResponseFuture future = (ResponseFuture) client.asyncCall("rename", new Object[]{user, "CCC"}, User.class);
user = (User) future.getValue();
System.out.println(user);

ResponseFuture future2 = (ResponseFuture) client.asyncCall("rename", new Object[]{user, "DDD"}, User.class);
future2.addListener(new FutureListener() {
@Override
public void operationComplete(Future future) {
System.out.println(future.getValue());
}
});

Request request = client.buildRequest("rename", new Object[]{user, "EEE"});
request.setAttachment("a", "a");
user = (User) client.call(request, User.class);
System.out.println(user);

client.call("rename", new Object[]{null, "FFF"}, void.class);
}

public static void motan2ApiCommonClientDemo() throws Throwable {
RefererConfig<CommonHandler> referer = new RefererConfig<>();

// 设置服务端接口
referer.setInterface(CommonHandler.class);
referer.setServiceInterface("com.weibo.motan.demo.service.MotanDemoService");

// 配置服务的group以及版本号
referer.setGroup("motan-demo-rpc");
referer.setVersion("1.0");
referer.setRequestTimeout(1000);

// 配置注册中心直连调用
RegistryConfig registry = new RegistryConfig();
registry.setRegProtocol("direct");
registry.setAddress("127.0.0.1:8001");
referer.setRegistry(registry);

// 配置RPC协议
ProtocolConfig protocol = new ProtocolConfig();
protocol.setId("motan2");
protocol.setName("motan2");
referer.setProtocol(protocol);

// 使用服务
CommonHandler client = referer.getRef();
System.out.println(client.call("hello", new Object[]{"a"}, String.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@
<motan:referer id="motanDemoReferer-pb" registry="registry3"
interface="com.weibo.motan.demo.service.PbParamService" protocol="motan2-pb"
connectTimeout="1000" requestTimeout="1000" basicReferer="motantestClientBasicConfig"/>
<motan:referer id="motanDemoReferer-common-client" protocol="motan2" registry="registry"
serviceInterface="com.weibo.motan.demo.service.MotanDemoService"
connectTimeout="1000" requestTimeout="1000" basicReferer="motantestClientBasicConfig"/>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,5 @@
<motan:referer id="motanDemoReferer"
interface="com.weibo.motan.demo.service.MotanDemoService"
connectTimeout="1000" requestTimeout="1000" basicReferer="motantestClientBasicConfig"/>
<motan:referer id="motanDemoReferer2" proxy="common"
serviceInterface="com.weibo.motan.demo.service.MotanDemoService"
connectTimeout="1000" requestTimeout="1000" basicReferer="motantestClientBasicConfig">
</motan:referer>

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public String hello(String name) {

@Override
public User rename(User user, String name) {
if (user == null) {
System.out.println("user: null");
return null;
}
System.out.println(user.getId() + " rename " + user.getName() + " to " + name);
user.setName(name);
return user;
Expand Down
19 changes: 3 additions & 16 deletions motan-springsupport/src/test/resources/spi_test_context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,11 @@

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:motan="http://api.weibo.com/schema/motan"
xmlns:weibo="http://api.weibo.com/schema/weibo"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://api.weibo.com/schema/weibo http://api.weibo.com/schema/weibo.xsd
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">

<motan:spi interfaceClass="com.weibo.api.motan.config.springsupport.ISpi"
spiClass="com.weibo.api.motan.config.springsupport.SpiImpl"></motan:spi>
<motan:spi interfaceClass="com.weibo.api.motan.config.springsupport.ISpi"
spiClass="com.weibo.api.motan.config.springsupport.SpiImpl"/>

</beans>

0 comments on commit 5eeade4

Please # to comment.