Skip to content

Commit

Permalink
update demo for direct connect
Browse files Browse the repository at this point in the history
  • Loading branch information
qdaxb committed Jun 12, 2016
1 parent 5401b75 commit 79157af
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class MotanConstants {
public static final String SCOPE_LOCAL = "local";
public static final String SCOPE_REMOTE = "remote";
public static final String REGISTRY_PROTOCOL_LOCAL = "local";
public static final String REGISTRY_PROTOCOL_DIRECT = "direct";
public static final String REGISTRY_PROTOCOL_ZOOKEEPER = "zookeeper";
public static final String PROTOCOL_INJVM = "injvm";
public static final String PROTOCOL_MOTAN = "motan";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.weibo.api.motan.registry.support;

import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.exception.MotanFrameworkException;
import com.weibo.api.motan.registry.NotifyListener;
import com.weibo.api.motan.rpc.URL;

Expand All @@ -28,10 +30,43 @@
*/
public class DirectRegistry extends AbstractRegistry {

ConcurrentHashMap<URL, Object> subscribeUrls = new ConcurrentHashMap();
private ConcurrentHashMap<URL, Object> subscribeUrls = new ConcurrentHashMap();
private List<URL> directUrls = new ArrayList<URL>();

public DirectRegistry(URL url) {
super(url);
String address = url.getParameter("address");
if (address.contains(",")) {
try {
String[] directUrlArray = address.split(",");
for (String directUrl : directUrlArray) {
parseDirectUrl(directUrl);
}
} catch (Exception e) {
throw new MotanFrameworkException(
String.format("parse direct url error, invalid direct registry address %s, address should be ip1:port1,ip2:port2 ..."));
}
} else {
registerDirectUrl(url.getHost(), url.getPort());
}
}

private void parseDirectUrl(String directUrl) {
String[] ipAndPort = directUrl.split(":");
String ip = ipAndPort[0];
Integer port = Integer.parseInt(ipAndPort[1]);
if (port < 0 || port > 65535) {
throw new RuntimeException();
}
registerDirectUrl(ip, port);
}

private void registerDirectUrl(String ip, Integer port) {
URL url = new URL(MotanConstants.REGISTRY_PROTOCOL_DIRECT,ip,port,"");
directUrls.add(url);
}

private void parseIpAndPort(String directUrl) {
}

@Override
Expand All @@ -58,16 +93,16 @@ protected void doUnsubscribe(URL url, NotifyListener listener) {

@Override
protected List<URL> doDiscover(URL subscribeUrl) {
return createSubscribeUrl();
return createSubscribeUrl(subscribeUrl);
}

private List<URL> createSubscribeUrl() {
private List<URL> createSubscribeUrl(URL subscribeUrl) {
URL url = this.getUrl();
List result = new ArrayList();
for (URL subscribeUrl : subscribeUrls.keySet()) {
List result = new ArrayList(directUrls.size());
for (URL directUrl : directUrls) {
URL tmp = subscribeUrl.createCopy();
tmp.setHost(url.getHost());
tmp.setPort(url.getPort());
tmp.setHost(directUrl.getHost());
tmp.setPort(directUrl.getPort());
result.add(tmp);
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">

<!-- 注册中心配置 -->
<motan:registry regProtocol="local" name="motanDemo" />
<motan:registry regProtocol="direct" address="localhost:8002" name="motanDemo" />

<!-- motan协议配置 -->
<motan:protocol default="true" name="motan" haStrategy="failover"
Expand All @@ -35,7 +35,6 @@

<!-- 具体referer配置。使用方通过beanid使用服务接口类 -->
<motan:referer id="motanDemoReferer"
directUrl="localhost:8002"
interface="com.weibo.motan.demo.service.MotanDemoService"
connectTimeout="300" requestTimeout="300" basicReferer="motantestClientBasicConfig" />

Expand Down

0 comments on commit 79157af

Please sign in to comment.