Search in sources :

Example 1 with AddressHolder

use of com.alipay.sofa.rpc.client.AddressHolder in project sofa-rpc by sofastack.

the class FaultBaseTest method getProviderInfoByHost.

static ProviderInfo getProviderInfoByHost(ConsumerConfig consumerConfig, String host) {
    ConsumerBootstrap consumerBootStrap = consumerConfig.getConsumerBootstrap();
    AddressHolder addressHolder = consumerBootStrap.getCluster().getAddressHolder();
    List<ProviderGroup> providerGroups = addressHolder.getProviderGroups();
    for (ProviderGroup providerGroup : providerGroups) {
        for (ProviderInfo providerInfo : providerGroup.getProviderInfos()) {
            if (providerInfo.getHost().equals(host)) {
                return providerInfo;
            }
        }
    }
    return null;
}
Also used : ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) AddressHolder(com.alipay.sofa.rpc.client.AddressHolder) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup)

Example 2 with AddressHolder

use of com.alipay.sofa.rpc.client.AddressHolder in project sofa-rpc by sofastack.

the class RegistryRouter method route.

@Override
public List<ProviderInfo> route(SofaRequest request, List<ProviderInfo> providerInfos) {
    // has  address. FIXME
    if (CommonUtils.isNotEmpty(providerInfos)) {
        return providerInfos;
    }
    AddressHolder addressHolder = consumerBootstrap.getCluster().getAddressHolder();
    if (addressHolder != null) {
        List<ProviderInfo> current = addressHolder.getProviderInfos(RpcConstants.ADDRESS_DEFAULT_GROUP);
        if (providerInfos != null) {
            providerInfos.addAll(current);
        } else {
            providerInfos = current;
        }
    }
    recordRouterWay(RPC_REGISTRY_ROUTER);
    return providerInfos;
}
Also used : AddressHolder(com.alipay.sofa.rpc.client.AddressHolder) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo)

Example 3 with AddressHolder

use of com.alipay.sofa.rpc.client.AddressHolder in project sofa-rpc by sofastack.

the class MeshRouter method route.

@Override
public List<ProviderInfo> route(SofaRequest request, List<ProviderInfo> providerInfos) {
    AddressHolder addressHolder = consumerBootstrap.getCluster().getAddressHolder();
    if (addressHolder != null) {
        List<ProviderInfo> current = addressHolder.getProviderInfos(RpcConstants.ADDRESS_DEFAULT_GROUP);
        if (providerInfos != null) {
            providerInfos.addAll(current);
        } else {
            providerInfos = current;
        }
    }
    recordRouterWay(RPC_MESH_ROUTER);
    return providerInfos;
}
Also used : AddressHolder(com.alipay.sofa.rpc.client.AddressHolder) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo)

Example 4 with AddressHolder

use of com.alipay.sofa.rpc.client.AddressHolder in project sofa-rpc by sofastack.

the class DirectUrlRouter method route.

@Override
public List<ProviderInfo> route(SofaRequest request, List<ProviderInfo> providerInfos) {
    AddressHolder addressHolder = consumerBootstrap.getCluster().getAddressHolder();
    if (addressHolder != null) {
        List<ProviderInfo> current = addressHolder.getProviderInfos(RpcConstants.ADDRESS_DIRECT_GROUP);
        if (providerInfos != null) {
            providerInfos.addAll(current);
        } else {
            providerInfos = current;
        }
    }
    recordRouterWay(RPC_DIRECT_URL_ROUTER);
    return providerInfos;
}
Also used : AddressHolder(com.alipay.sofa.rpc.client.AddressHolder) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo)

Example 5 with AddressHolder

use of com.alipay.sofa.rpc.client.AddressHolder in project sofa-rpc by sofastack.

the class ZookeeperOverrideObserverTest method testAll.

@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);
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) AddressHolder(com.alipay.sofa.rpc.client.AddressHolder) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) BaseZkTest(com.alipay.sofa.rpc.registry.base.BaseZkTest)

Aggregations

AddressHolder (com.alipay.sofa.rpc.client.AddressHolder)6 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)5 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)2 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)2 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)2 BaseZkTest (com.alipay.sofa.rpc.registry.base.BaseZkTest)2 Test (org.junit.Test)2 ConsumerBootstrap (com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap)1 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 HelloService (com.alipay.sofa.rpc.test.HelloService)1 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)1