use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class FailoverHaStrategyTest method setUp.
@Before
@Override
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
super.setUp();
loadBalance = mockery.mock(LoadBalance.class);
final Referer<IWorld> referer1 = mockery.mock(Referer.class, "ref1");
final Referer<IWorld> referer2 = mockery.mock(Referer.class, "ref2");
referers = new ArrayList<Referer<IWorld>>();
referers.add(referer1);
referers.add(referer2);
failoverHaStrategy = new FailoverHaStrategy<IWorld>() {
@Override
protected List<Referer<IWorld>> selectReferers(Request request, LoadBalance<IWorld> loadBalance) {
return referers;
}
};
URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.LOCALHOST, 0, IWorld.class.getName());
url.addParameter(URLParamType.retries.getName(), String.valueOf(retries));
failoverHaStrategy.setUrl(url);
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class ClusterSpiTest method initCluster.
@SuppressWarnings("unchecked")
private void initCluster(boolean throwException) {
referers = new ArrayList<Referer<IHello>>();
for (int i = 0; i < 10; i++) {
referers.add(mockery.mock(Referer.class, "ref_" + i));
}
clusterSpi.setHaStrategy(new FailoverHaStrategy<IHello>());
clusterSpi.setLoadBalance(new RandomLoadBalance<IHello>());
URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
url.addParameter(URLParamType.throwException.getName(), String.valueOf(throwException));
url.addParameter(URLParamType.retries.getName(), "2");
clusterSpi.setUrl(url);
clusterSpi.onRefresh(referers);
clusterSpi.init();
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class ClusterSupport method doRefreshReferersByUrls.
private void doRefreshReferersByUrls(URL registryUrl, List<URL> serviceUrls) {
List<Referer<T>> newReferers = new ArrayList<>();
for (URL u : serviceUrls) {
if (!u.canServe(url)) {
continue;
}
Referer<T> referer = getExistingReferer(u, registryReferers.get(registryUrl));
if (referer == null) {
// careful u: serverURL, refererURL的配置会被serverURL的配置覆盖
URL refererURL = u.createCopy();
mergeClientConfigs(refererURL);
referer = protocol.refer(interfaceClass, refererURL, u);
}
if (referer != null) {
newReferers.add(referer);
}
}
if (CollectionUtil.isEmpty(newReferers)) {
onRegistryEmpty(registryUrl);
return;
}
// 此处不销毁referers,由cluster进行销毁
registryReferers.put(registryUrl, newReferers);
refreshCluster();
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class ClusterSupportTest method testRefreshReferers.
@Test
public void testRefreshReferers() {
MotanSwitcherUtil.setSwitcherValue("feature.motan.partial.server", true);
List<URL> copy = new ArrayList<URL>();
clusterSupport.notify(registries.get(regProtocol1).getUrl(), copy(copy, serviceUrls1.subList(0, 2)));
clusterSupport.notify(registries.get(regProtocol2).getUrl(), null);
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 2);
clusterSupport.refreshReferers();
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 2);
clusterSupport.notify(registries.get(regProtocol1).getUrl(), copy(copy, serviceUrls1.subList(0, 6)));
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 4);
Assert.assertEquals(getAvailableReferersCount(), 4);
Referer referer1 = clusterSupport.getCluster().getReferers().get(0);
Referer referer2 = clusterSupport.getCluster().getReferers().get(1);
// 设置1节点不可用,未小于阈值,不触发refresh
availableMap.put(referer1.getUrl().toString(), false);
clusterSupport.refreshReferers();
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 4);
Assert.assertEquals(getAvailableReferersCount(), 3);
// 设置2节点不可用,小于阈值,触发refresh
availableMap.put(referer2.getUrl().toString(), false);
clusterSupport.refreshReferers();
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 6);
Assert.assertEquals(getAvailableReferersCount(), 4);
// 设置1节点恢复,未大于阈值,不触发refresh
availableMap.put(referer2.getUrl().toString(), true);
clusterSupport.refreshReferers();
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 6);
Assert.assertEquals(getAvailableReferersCount(), 5);
// 设置0节点恢复,大于阈值,触发refresh
availableMap.put(referer1.getUrl().toString(), true);
clusterSupport.refreshReferers();
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 4);
Assert.assertEquals(getAvailableReferersCount(), 4);
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class ClusterTest method setUp.
@Override
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
super.setUp();
HaStrategy<IHello> ha = new FailoverHaStrategy<IHello>();
LoadBalance<IHello> lb = new RandomLoadBalance<IHello>();
referers = new ArrayList<Referer<IHello>>();
referers.add(mockery.mock(Referer.class, "ref1"));
referers.add(mockery.mock(Referer.class, "ref2"));
final URL url = URL.valueOf("motan%3A%2F%2F10.209.128.244%3A8000%2Fcom.weibo.api.motan.protocol.example.IWorld%3Fprotocol%3Dmotan%26export%3Dmotan%3A8000%26application%3Dapi%26module%3Dtest%26check%3Dtrue%26refreshTimestamp%3D1373275099717%26methodconfig.world%28void%29.retries%3D1%26id%3Dmotan%26methodconfig.world%28java.lang.String%29.retries%3D1%26methodconfig.world%28java.lang.String%2Cboolean%29.retries%3D1%26nodeType%3Dservice%26group%3Dwangzhe-test-yf%26shareChannel%3Dtrue%26&");
mockery.checking(new Expectations() {
{
for (Referer<IHello> ref : referers) {
atLeast(0).of(ref).getServiceUrl();
will(returnValue(url));
atLeast(1).of(ref).destroy();
}
}
});
cluster.setUrl(new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName()));
cluster.setHaStrategy(ha);
cluster.setLoadBalance(lb);
cluster.onRefresh(referers);
cluster.init();
}
Aggregations