Java Code Examples for com.alipay.sofa.rpc.client.ProviderInfo#getHost()

The following examples show how to use com.alipay.sofa.rpc.client.ProviderInfo#getHost() . 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 vote down vote up
/**
 * 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 vote down vote up
@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 vote down vote up
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: SofaRpcConsumerInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@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 5
Source File: ZookeeperOverrideObserverTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@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);
    }
}