Java Code Examples for com.alipay.sofa.rpc.client.ProviderInfo#getPort()
The following examples show how to use
com.alipay.sofa.rpc.client.ProviderInfo#getPort() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BoltClientTransport.java From sofa-rpc with Apache License 2.0 | 6 votes |
/** * For convert provider to bolt url. * * @param transportConfig ClientTransportConfig * @param providerInfo ProviderInfo * @return Bolt Url */ protected Url convertProviderToUrl(ClientTransportConfig transportConfig, ProviderInfo providerInfo) { // Url的第一个参数,如果不用事件的话,其实无所谓 Url boltUrl = new Url(providerInfo.toString(), providerInfo.getHost(), providerInfo.getPort()); boltUrl.setConnectTimeout(transportConfig.getConnectTimeout()); // 默认初始化connNum个长连接,为了slb和vip的情况 final int connectionNum = transportConfig.getConnectionNum(); if (connectionNum > 0) { boltUrl.setConnNum(connectionNum); } else { boltUrl.setConnNum(1); } boltUrl.setConnWarmup(false); // true的话 if (RpcConstants.PROTOCOL_TYPE_BOLT.equals(providerInfo.getProtocolType())) { boltUrl.setProtocol(RemotingConstants.PROTOCOL_BOLT); } else { boltUrl.setProtocol(RemotingConstants.PROTOCOL_TR); } return boltUrl; }
Example 2
Source File: RestClientTransport.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override protected Object buildProxy(ClientTransportConfig transportConfig) throws SofaRpcException { SofaResteasyClientBuilder builder = new SofaResteasyClientBuilder(); ResteasyClient client = builder .registerProvider().logProviders() .establishConnectionTimeout(transportConfig.getConnectTimeout(), TimeUnit.MILLISECONDS) .socketTimeout(transportConfig.getInvokeTimeout(), TimeUnit.MILLISECONDS) .connectionPoolSize(Math.max(transportConfig.getConnectionNum(), MIN_CONNECTION_POOL_SIZE)) .build(); ProviderInfo provider = transportConfig.getProviderInfo(); String url = "http://" + provider.getHost() + ":" + provider.getPort() + StringUtils.CONTEXT_SEP + StringUtils.trimToEmpty(provider.getPath()); ResteasyWebTarget target = client.target(url); return target.proxy(ClassUtils.forName(transportConfig.getConsumerConfig().getInterfaceId())); }
Example 3
Source File: ReuseBoltClientConnectionManagerTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
private Url buildUrl(ClientTransportConfig clientTransportConfig) { ProviderInfo providerInfo2 = clientTransportConfig.getProviderInfo(); Url url = new Url(providerInfo2.toString(), providerInfo2.getHost(), providerInfo2.getPort()); url.setConnectTimeout(4500); url.setProtocol(RemotingConstants.PROTOCOL_BOLT); url.setConnNum(1); // 默认初始化connNum个长连接 url.setConnWarmup(false); return url; }
Example 4
Source File: SofaRegistrySubscribeCallback.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * merge url * * @param userDatas * @param configDatas * @return */ List<ProviderInfo> mergeProviderInfo(List<String> userDatas, List<String> configDatas) { // 是否自己缓存运算后的结果?? TODO List<ProviderInfo> providers = SofaRegistryHelper.parseProviderInfos(userDatas); // 交叉比较 if (CommonUtils.isNotEmpty(providers) && CommonUtils.isNotEmpty(configDatas)) { List<ProviderInfo> override = SofaRegistryHelper.parseProviderInfos(configDatas); Iterator<ProviderInfo> iterator = providers.iterator(); while (iterator.hasNext()) { ProviderInfo origin = iterator.next(); for (ProviderInfo over : override) { if (PROTOCOL_TYPE_OVERRIDE.equals(over.getProtocolType()) && StringUtils.equals(origin.getHost(), over.getHost()) && origin.getPort() == over.getPort()) { // host 和 port 相同 认为是一个地址 if (over.getWeight() != origin.getWeight()) { origin.setWeight(over.getWeight()); } if (CommonUtils.isTrue(over.getAttr(ProviderInfoAttrs.ATTR_DISABLED))) { if (LOGGER.isInfoEnabled()) { LOGGER.info("Provider is disabled by override. {}", origin.toUrl()); } iterator.remove(); // 禁用 删掉 } } } } } return providers; }
Example 5
Source File: WeightConsistentHashLoadBalancerTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * 测试相同参数会落到相同的节点 * * @throws Exception */ @Test public void doSelect() throws Exception { WeightConsistentHashLoadBalancer loadBalancer = new WeightConsistentHashLoadBalancer(null); Map<Integer, Integer> cnt = new HashMap<Integer, Integer>(40); int size = 20; int total = 100000; SofaRequest request = new SofaRequest(); request.setInterfaceName(ConsistentHashLoadBalancerTest.class.getName()); request.setMethod(ConsistentHashLoadBalancerTest.class.getMethod("doSelect")); for (int i = 0; i < size; i++) { cnt.put(9000 + i, 0); } List<ProviderInfo> providers = buildDiffWeightProviderList(size); long start = System.currentTimeMillis(); for (int i = 0; i < total; i++) { ProviderInfo provider = loadBalancer.doSelect(request, providers); int port = provider.getPort(); cnt.put(port, cnt.get(port) + 1); } long end = System.currentTimeMillis(); System.out.println("elapsed" + (end - start) + "ms"); System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns"); int count = 0; for (int i = 0; i < size; i++) { if (cnt.get(9000 + i) > 0) { count++; } } // 应该落在同一台机器上 Assert.assertTrue(count == 1); }
Example 6
Source File: WeightConsistentHashLoadBalancerTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * 测试根据权重数据的分布符合比例 * * @throws Exception */ @Test public void testWeight() throws Exception { WeightConsistentHashLoadBalancer loadBalancer = new WeightConsistentHashLoadBalancer(null); SofaRequest request = new SofaRequest(); request.setInterfaceName(ConsistentHashLoadBalancerTest.class.getName()); request.setMethod(ConsistentHashLoadBalancerTest.class.getMethod("doSelect")); int size = 20; int total = 100000; List<ProviderInfo> providers = buildDiffWeightProviderList(size); Map<Integer, Integer> map = new HashMap(total * 2); for (int i = 0; i < total; i++) { request.setMethodArgs(new Object[] { "method" + i }); ProviderInfo provider = loadBalancer.doSelect(request, providers); Integer key = provider.getPort(); if (map.containsKey(key)) { int count = map.get(key); map.put(key, ++count); } else { map.put(key, 0); } } Set<Map.Entry<Integer, Integer>> set = map.entrySet(); Iterator<Map.Entry<Integer, Integer>> iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry<Integer, Integer> entry = iterator.next(); int port = entry.getKey() - 9000; //最大误差不超过10% Assert.assertTrue(entry.getValue() > 500 * port * 0.90); Assert.assertTrue(entry.getValue() < 500 * port * 1.10); } }
Example 7
Source File: ConsistentHashLoadBalancerTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Test public void doSelect() throws Exception { ConsistentHashLoadBalancer loadBalancer = new ConsistentHashLoadBalancer(null); Map<Integer, Integer> cnt = new HashMap<Integer, Integer>(); int size = 20; int total = 100000; SofaRequest request = new SofaRequest(); request.setInterfaceName(ConsistentHashLoadBalancerTest.class.getName()); request.setMethod(ConsistentHashLoadBalancerTest.class.getMethod("doSelect")); for (int i = 0; i < size; i++) { cnt.put(9000 + i, 0); } List<ProviderInfo> providers = buildSameWeightProviderList(size); long start = System.currentTimeMillis(); for (int i = 0; i < total; i++) { ProviderInfo provider = loadBalancer.doSelect(request, providers); int port = provider.getPort(); cnt.put(port, cnt.get(port) + 1); } long end = System.currentTimeMillis(); LOGGER.info("elapsed" + (end - start) + "ms"); LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns"); int count = 0; for (int i = 0; i < size; i++) { if (cnt.get(9000 + i) > 0) { count++; } } Assert.assertTrue(count == 1);// 应该落在一台机器上 }
Example 8
Source File: SofaRpcConsumerInterceptor.java From skywalking with Apache License 2.0 | 5 votes |
@Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { SofaRequest sofaRequest = (SofaRequest) allArguments[0]; RpcInternalContext rpcContext = RpcInternalContext.getContext(); ProviderInfo providerInfo = rpcContext.getProviderInfo(); AbstractSpan span = null; final String host = providerInfo.getHost(); final int port = providerInfo.getPort(); final ContextCarrier contextCarrier = new ContextCarrier(); final String operationName = generateOperationName(providerInfo, sofaRequest); span = ContextManager.createExitSpan(operationName, contextCarrier, host + ":" + port); CarrierItem next = contextCarrier.items(); while (next.hasNext()) { next = next.next(); String key = next.getHeadKey(); String skyWalkingKey = SKYWALKING_PREFIX + key; sofaRequest.addRequestProp(skyWalkingKey, next.getHeadValue()); } Tags.URL.set(span, generateRequestURL(providerInfo, sofaRequest)); span.setComponent(ComponentsDefine.SOFARPC); SpanLayer.asRPCFramework(span); }
Example 9
Source File: ZookeeperOverrideObserverTest.java From sofa-rpc with Apache License 2.0 | 4 votes |
@Test public void testAll() throws Exception { try { RegistryConfig registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK) .setAddress("127.0.0.1:2181"); ZookeeperRegistry registry = (ZookeeperRegistry) RegistryFactory .getRegistry(registryConfig); registry.start(); ServerConfig serverConfig = new ServerConfig().setPort(22222) .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT); ProviderConfig<OverrideService> providerConfig = new ProviderConfig<OverrideService>() .setInterfaceId(OverrideService.class.getName()).setRef(new OverrideServiceImpl(22222)) .setServer(serverConfig).setRegistry(registryConfig) .setParameter(ProviderInfoAttrs.ATTR_WARMUP_TIME, "2000") .setParameter(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT, "100").setWeight(0); ServerConfig serverConfig2 = new ServerConfig().setPort(22111) .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT); ProviderConfig<OverrideService> providerConfig2 = new ProviderConfig<OverrideService>() .setInterfaceId(OverrideService.class.getName()).setRef(new OverrideServiceImpl(22111)) .setServer(serverConfig2).setRegistry(registryConfig).setRepeatedExportLimit(-1) .setWeight(0); providerConfig.export(); providerConfig2.export(); ConsumerConfig<OverrideService> consumerConfig = new ConsumerConfig<OverrideService>() .setInterfaceId(OverrideService.class.getName()).setRegistry(registryConfig) .setTimeout(3333).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT); OverrideService overrideService = consumerConfig.refer(); AddressHolder addressHolder = consumerConfig.getConsumerBootstrap().getCluster() .getAddressHolder(); Assert.assertTrue(addressHolder.getAllProviderSize() == 2); providerConfig2.unExport(); Assert.assertTrue(delayGetSize(addressHolder, 1, 100) == 1); List<String> path = registry.getZkClient().getChildren() .forPath("/sofa-rpc/" + OverrideService.class.getCanonicalName() + "/providers"); String url = URLDecoder.decode(path.get(0), "UTF-8"); ProviderInfo providerInfo = ProviderHelper.toProviderInfo(url); // 模拟下发一个override String override1 = providerInfo.getProtocolType() + "://" + providerInfo.getHost() + ":" + providerInfo.getPort() + "?timeout=2345"; String overridePath1 = "/sofa-rpc/" + OverrideService.class.getCanonicalName() + "/overrides/" + URLEncoder.encode(override1, "UTF-8"); registry.getZkClient().create().creatingParentContainersIfNeeded() .withMode(CreateMode.PERSISTENT).forPath(overridePath1); Assert.assertTrue(delayGetTimeout(consumerConfig, 2345, 100) == 2345); // 删除目前没有影响 registry.getZkClient().delete().forPath(overridePath1); Thread.sleep(500); Assert.assertTrue(delayGetTimeout(consumerConfig, 2345, 100) == 2345); // 恢复到3333 String override2 = providerInfo.getProtocolType() + "://" + providerInfo.getHost() + ":" + providerInfo.getPort() + "?timeout=3333"; String overridePath2 = "/sofa-rpc/" + OverrideService.class.getCanonicalName() + "/overrides/" + URLEncoder.encode(override2, "UTF-8"); registry.getZkClient().create().creatingParentContainersIfNeeded() .withMode(CreateMode.PERSISTENT).forPath(overridePath2); Assert.assertTrue(delayGetTimeout(consumerConfig, 3333, 100) == 3333); // 清除持久化的 path registry.getZkClient().delete().forPath(overridePath2); } catch (Throwable e) { LOGGER.error("ZookeeperOverrideObserver test case failed", e); Assert.assertTrue(e.getMessage(), false); } }