use of com.alipay.remoting.Url in project sofa-rpc by sofastack.
the class BoltClientTransport method convertProviderToUrl.
/**
* 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);
}
// true的话
boltUrl.setConnWarmup(false);
if (RpcConstants.PROTOCOL_TYPE_BOLT.equals(providerInfo.getProtocolType())) {
boltUrl.setProtocol(RemotingConstants.PROTOCOL_BOLT);
} else {
boltUrl.setProtocol(RemotingConstants.PROTOCOL_TR);
}
return boltUrl;
}
use of com.alipay.remoting.Url in project sofa-rpc by sofastack.
the class ReuseBoltClientConnectionManagerTest method testConcurrentCreate.
@Test
public void testConcurrentCreate() throws Exception {
final ReuseBoltClientConnectionManager manager = new ReuseBoltClientConnectionManager(false);
final ClientTransportConfig config = buildConfig(12222);
// 并发创建
final CountDownLatch latch = new CountDownLatch(5);
List<Thread> threads = new ArrayList<Thread>(5);
final Url url = buildUrl(config);
for (int i = 0; i < 5; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Connection innerConnection = manager.getConnection(rpcClient, config, url);
System.out.println("url=" + url + ",connection=" + innerConnection);
} catch (Exception e) {
e.printStackTrace();
} finally {
latch.countDown();
}
}
}, "thread" + i);
threads.add(thread);
}
for (Thread thread : threads) {
thread.start();
}
latch.await(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(1, manager.urlConnectionMap.size());
Assert.assertEquals(1, manager.connectionRefCounter.size());
Connection connection = manager.getConnection(rpcClient, config, url);
Assert.assertNotNull(connection);
final AtomicInteger atomicInteger = manager.connectionRefCounter.get(connection);
Assert.assertEquals(1, atomicInteger.get());
// 检查泄漏
manager.checkLeak();
Assert.assertTrue(CommonUtils.isEmpty(manager.urlConnectionMap));
Assert.assertTrue(CommonUtils.isEmpty(manager.connectionRefCounter));
}
use of com.alipay.remoting.Url in project sofa-rpc by sofastack.
the class ReuseBoltClientConnectionManagerTest method buildUrl.
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);
// 默认初始化connNum个长连接
url.setConnNum(1);
url.setConnWarmup(false);
return url;
}
Aggregations