use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.
the class AllConnectConnectionHolder method getAvailableClientTransport.
@Override
public ClientTransport getAvailableClientTransport(ProviderInfo providerInfo) {
// 先去存活列表
ClientTransport transport = aliveConnections.get(providerInfo);
if (transport != null) {
return transport;
}
// 再去亚健康列表
transport = subHealthConnections.get(providerInfo);
if (transport != null) {
return transport;
}
// 最后看看是否第一次调用未初始化
transport = uninitializedConnections.get(providerInfo);
if (transport != null) {
// 未初始化则初始化
synchronized (this) {
transport = uninitializedConnections.get(providerInfo);
if (transport != null) {
initClientTransport(consumerConfig.getInterfaceId(), providerInfo, transport);
uninitializedConnections.remove(providerInfo);
}
return getAvailableClientTransport(providerInfo);
}
}
if (createConnWhenAbsent) {
RpcInternalContext context = RpcInternalContext.peekContext();
String targetIP = (context == null) ? null : (String) context.getAttachment(RpcConstants.HIDDEN_KEY_PINPOINT);
/**
* RpcInvokeContext.getContext().setTargetUrl() 设置了地址,初始化tcp连接
*/
if (StringUtils.isNotBlank(targetIP)) {
ClientTransportConfig transportConfig = providerToClientConfig(providerInfo);
transport = ClientTransportFactory.getClientTransport(transportConfig);
initClientTransport(consumerConfig.getInterfaceId(), providerInfo, transport);
}
}
return transport;
}
use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.
the class ElasticConnectionHolder method addNode.
@Override
protected void addNode(List<ProviderInfo> providerInfoList) {
final String interfaceId = consumerConfig.getInterfaceId();
int providerSize = providerInfoList.size();
final String appName = consumerConfig.getAppName();
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, "Add provider of {}, size is : {}", interfaceId, providerSize);
}
if (providerSize > 0) {
int minSynConnectSize;
// 计算初始化连接最少数,优先使用初始化最小数属性进行计算,百分比属性默认为0
if (elasticConnectPercent > 0) {
double percent = elasticConnectPercent >= 100 ? 1 : elasticConnectPercent * 0.01;
minSynConnectSize = ((Double) (providerInfoList.size() * percent)).intValue();
} else {
minSynConnectSize = elasticConnectSize;
}
// 多线程建立连接
// 最大10个
int threads = Math.min(10, minSynConnectSize);
final CountDownLatch latch = new CountDownLatch(minSynConnectSize);
int connectTimeout = consumerConfig.getConnectTimeout();
NamedThreadFactory namedThreadFactory = new NamedThreadFactory("CLI-CONN-" + interfaceId, true);
ThreadPoolExecutor initPool = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(minSynConnectSize), namedThreadFactory);
// 第一次同步建立连接的连接数
int synInitConnectProviderSize = 0;
for (final ProviderInfo providerInfo : providerInfoList) {
if (synInitConnectProviderSize >= minSynConnectSize) {
break;
}
synInitConnectProviderSize++;
initClientRunnable(initPool, latch, providerInfo);
}
try {
int totalTimeout = ((synInitConnectProviderSize % threads == 0) ? (synInitConnectProviderSize / threads) : ((synInitConnectProviderSize / threads) + 1)) * connectTimeout + 500;
// 一直等到子线程都结束
latch.await(totalTimeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
LOGGER.errorWithApp(appName, "Exception when add provider", e);
} finally {
// 关闭线程池
initPool.shutdown();
}
final List<ProviderInfo> asynConnectProviderInfoList = providerInfoList.subList(synInitConnectProviderSize, providerInfoList.size());
if (!asynConnectProviderInfoList.isEmpty()) {
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, "asynConnectProviderInfoListSize:{}", asynConnectProviderInfoList.size());
}
final ExecutorService executorService = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(asynConnectProviderInfoList.size()), namedThreadFactory);
FutureTask<String> futureTask;
for (final ProviderInfo providerInfo : asynConnectProviderInfoList) {
final ClientTransportConfig config = providerToClientConfig(providerInfo);
futureTask = new FutureTask<String>(new Callable<String>() {
@Override
public String call() throws Exception {
ClientTransport transport = ClientTransportFactory.getClientTransport(config);
if (consumerConfig.isLazy()) {
uninitializedConnections.put(providerInfo, transport);
} else {
initClientTransport(interfaceId, providerInfo, transport);
}
return providerInfo.getHost() + ":" + providerInfo.getPort();
}
});
executorService.submit(futureTask);
}
}
}
}
use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.
the class Http2ClearTextBadRequestTest method testAll.
@Test
public void testAll() throws Exception {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12333).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setUniqueId("uuu").setRegister(false);
providerConfig.export();
ClientTransportConfig clientTransportConfig = new ClientTransportConfig();
clientTransportConfig.setProviderInfo(ProviderHelper.toProviderInfo("h2c://127.0.0.1:12333"));
Http2ClientTransport clientTransport = new Http2ClientTransport(clientTransportConfig);
clientTransport.connect();
{
// GET 图标
String url = "/favicon.ico";
FullHttpRequest httpRequest = buildHttpRequest(url);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
Assert.assertTrue(handler.content.length == 0);
}
{
// 其它未知命令
String url = "/com.alipay.sofa.rpc.server.http.HttpService/add";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.OPTIONS);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(400, handler.response.status().code());
Assert.assertTrue(StringUtils.isNotEmpty(getStringContent(handler)));
}
{
// HEAD 不存在的服务
String url = "/com.alipay.sofa.rpc.server.http.HttpService12313/add";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.HEAD);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
}
{
// HEAD 不存在的方法
String url = "/com.alipay.sofa.rpc.server.http.HttpService:uuu/xasdasdasd";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.HEAD);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
}
{
// HEAD 存在的方法
String url = "/com.alipay.sofa.rpc.server.http.HttpService:uuu/add";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.HEAD);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
}
{
// POST 异常:地址不对
String url = "/com.alipay.sofa";
FullHttpRequest httpRequest = buildHttpRequest(url);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(400, handler.response.status().code());
Assert.assertTrue(getStringContent(handler).contains("ip:port"));
}
{
// POST 未知序列化
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/asdasdas?code=xxx";
FullHttpRequest httpRequest = buildHttpRequest(url);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(400, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
}
{
// POST 不存在的接口
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/echoPb";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
}
{
// POST 不存在的方法
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/adasdada";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
}
{
// POST 不传 HEAD_SERIALIZE_TYPE
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/echoPb";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, "application/x-protobuf");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
EchoResponse response = EchoResponse.parseFrom(handler.content);
Assert.assertEquals("helloxxx", response.getMessage());
}
{
// POST 正常
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/echoPb";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
httpRequest.headers().add(RemotingConstants.HEAD_TARGET_APP, "serverApp1");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
EchoResponse response = EchoResponse.parseFrom(handler.content);
Assert.assertEquals("helloxxx", response.getMessage());
}
}
use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.
the class BoltClientTransportTest method doReuseTest.
@Test
public void doReuseTest() {
ServerConfig serverConfig = new ServerConfig().setPort(12222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
serverConfig.buildIfAbsent().start();
ServerConfig serverConfig2 = new ServerConfig().setPort(12223).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
serverConfig2.buildIfAbsent().start();
ClientTransportConfig clientTransportConfig = new ClientTransportConfig();
ProviderInfo providerInfo = ProviderHelper.toProviderInfo("127.0.0.1:12222");
clientTransportConfig.setProviderInfo(providerInfo);
BoltClientTransport clientTransport = new BoltClientTransport(clientTransportConfig);
clientTransport.disconnect();
final BoltClientConnectionManager connectionManager = BoltClientTransport.connectionManager;
if (connectionManager instanceof ReuseBoltClientConnectionManager) {
Assert.assertTrue(((ReuseBoltClientConnectionManager) connectionManager).urlConnectionMap.size() == 0);
Assert.assertTrue(((ReuseBoltClientConnectionManager) connectionManager).connectionRefCounter.size() == 0);
} else {
Assert.fail();
}
ClientTransportConfig config1 = new ClientTransportConfig();
config1.setProviderInfo(new ProviderInfo().setHost("127.0.0.1").setPort(12222)).setContainer("bolt");
BoltClientTransport clientTransport1 = (BoltClientTransport) ClientTransportFactory.getClientTransport(config1);
clientTransport1.connect();
ClientTransportConfig config2 = new ClientTransportConfig();
config2.setProviderInfo(new ProviderInfo().setHost("127.0.0.1").setPort(12222)).setContainer("bolt");
BoltClientTransport clientTransport2 = (BoltClientTransport) ClientTransportFactory.getClientTransport(config2);
clientTransport2.connect();
Assert.assertFalse(clientTransport1 == clientTransport2);
Assert.assertTrue(clientTransport1.fetchConnection() == clientTransport2.fetchConnection());
ClientTransportConfig config3 = new ClientTransportConfig();
config3.setProviderInfo(new ProviderInfo().setHost("127.0.0.1").setPort(12223)).setContainer("bolt");
BoltClientTransport clientTransport3 = (BoltClientTransport) ClientTransportFactory.getClientTransport(config3);
clientTransport3.connect();
Assert.assertFalse(clientTransport1 == clientTransport3);
Assert.assertFalse(clientTransport1.fetchConnection() == clientTransport3.fetchConnection());
ClientTransportFactory.releaseTransport(null, 500);
clientTransport1.currentRequests.set(4);
ClientTransportFactory.releaseTransport(clientTransport1, 500);
clientTransport2.currentRequests.set(0);
ClientTransportFactory.releaseTransport(clientTransport2, 500);
ClientTransportFactory.closeAll();
}
use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.
the class BoltClientTransportTest method testNotSupport.
@Test
public void testNotSupport() {
ClientTransportConfig config1 = new ClientTransportConfig();
config1.setProviderInfo(new ProviderInfo().setHost("127.0.0.1").setPort(12222)).setContainer("bolt");
BoltClientTransport transport = new BoltClientTransport(config1);
try {
transport.setChannel(null);
Assert.fail();
} catch (Exception e) {
}
try {
transport.getChannel();
Assert.fail();
} catch (Exception e) {
}
try {
transport.receiveRpcResponse(null);
Assert.fail();
} catch (Exception e) {
}
try {
transport.handleRpcRequest(null);
Assert.fail();
} catch (Exception e) {
}
}
Aggregations