Search in sources :

Example 1 with ConsumerBootstrap

use of com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap 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 ConsumerBootstrap

use of com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap in project sofa-rpc by sofastack.

the class AbstractClusterTest method testDestroyWithDestroyHook.

@Test
public void testDestroyWithDestroyHook() {
    ConsumerConfig consumerConfig = new ConsumerConfig().setProtocol("test").setBootstrap("test");
    ConsumerBootstrap consumerBootstrap = new ConsumerBootstrap(consumerConfig) {

        @Override
        public Object refer() {
            return null;
        }

        @Override
        public void unRefer() {
        }

        @Override
        public Object getProxyIns() {
            return null;
        }

        @Override
        public Cluster getCluster() {
            return null;
        }

        @Override
        public List<ProviderGroup> subscribe() {
            return null;
        }

        @Override
        public boolean isSubscribed() {
            return false;
        }
    };
    AbstractCluster abstractCluster = new AbstractCluster(consumerBootstrap) {

        @Override
        protected SofaResponse doInvoke(SofaRequest msg) throws SofaRpcException {
            return null;
        }
    };
    List<String> hookActionResult = new ArrayList<>(2);
    Destroyable.DestroyHook destroyHook = new Destroyable.DestroyHook() {

        @Override
        public void preDestroy() {
            hookActionResult.add("preDestroy");
        }

        @Override
        public void postDestroy() {
            hookActionResult.add("postDestroy");
        }
    };
    abstractCluster.destroy(destroyHook);
    Assert.assertEquals(2, hookActionResult.size());
    Assert.assertEquals("preDestroy", hookActionResult.get(0));
    Assert.assertEquals("postDestroy", hookActionResult.get(1));
}
Also used : ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) Destroyable(com.alipay.sofa.rpc.base.Destroyable) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ArrayList(java.util.ArrayList) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Example 3 with ConsumerBootstrap

use of com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap in project sofa-rpc by sofastack.

the class LoadBalancerFactoryTest method getLoadBalancer.

@Test
public void getLoadBalancer() throws Exception {
    ConsumerConfig consumerConfig = new ConsumerConfig().setBootstrap("test").setLoadBalancer("test");
    ConsumerBootstrap bootstrap = Bootstraps.from(consumerConfig);
    Assert.assertEquals(LoadBalancerFactory.getLoadBalancer(bootstrap).getClass(), TestLoadBalancer.class);
    boolean error = false;
    try {
        consumerConfig.setLoadBalancer("xasdsa");
        LoadBalancerFactory.getLoadBalancer(bootstrap);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
}
Also used : ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Example 4 with ConsumerBootstrap

use of com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap in project sofa-rpc by sofastack.

the class DubboServerTest method testRegistrySync.

@Test
public // 同步调用,走服务注册中心
void testRegistrySync() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(10).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2).setHost(SystemInfo.getLocalHost());
    // 发布一个服务,每个请求要执行1秒
    ApplicationConfig serverApplacation = new ApplicationConfig();
    serverApplacation.setAppName("server");
    List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
    RegistryConfig registryConfig;
    registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK).setAddress("127.0.0.1:2181").setSubscribe(true).setRegister(true);
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    MethodConfig methodConfig = new MethodConfig();
    methodConfig.setTimeout(3000);
    methodConfig.setName("sayHello");
    methodConfigs.add(methodConfig);
    registryConfigs.add(registryConfig);
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setServer(serverConfig).setRegister(true).setBootstrap("dubbo").setRegistry(registryConfigs).setApplication(serverApplacation);
    providerConfig.export();
    ApplicationConfig clientApplication = new ApplicationConfig();
    clientApplication.setAppName("client");
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setTimeout(30000).setRegister(true).setProtocol("dubbo").setApplication(clientApplication).setRegistry(registryConfigs).setMethods(methodConfigs).setInJVM(false);
    final HelloService demoService = consumerConfig.refer();
    String result = demoService.sayHello("xxx", 22);
    Assert.assertNotNull(result);
    ConsumerBootstrap bootstrap = consumerConfig.getConsumerBootstrap();
    Assert.assertTrue(bootstrap instanceof DubboConsumerBootstrap);
    Assert.assertTrue(bootstrap.isSubscribed());
    Assert.assertNotNull(bootstrap.getProxyIns());
    bootstrap.unRefer();
    try {
        bootstrap.getCluster();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof UnsupportedOperationException);
    }
    try {
        bootstrap.subscribe();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof UnsupportedOperationException);
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ArrayList(java.util.ArrayList) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) BaseZkTest(com.alipay.sofa.rpc.registry.base.BaseZkTest)

Example 5 with ConsumerBootstrap

use of com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap in project sofa-rpc by sofastack.

the class BoltClientProxyInvokerTest method testParseSerializeType.

@Test
public void testParseSerializeType() throws Exception {
    ConsumerConfig consumerConfig = new ConsumerConfig().setProtocol("bolt");
    ConsumerBootstrap bootstrap = Bootstraps.from(consumerConfig);
    BoltClientProxyInvoker invoker = new BoltClientProxyInvoker(bootstrap);
    byte actual = invoker.parseSerializeType(RpcConstants.SERIALIZE_HESSIAN2);
    assertEquals(RemotingConstants.SERIALIZE_CODE_HESSIAN, actual);
}
Also used : ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Aggregations

ConsumerBootstrap (com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap)9 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)8 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)4 Destroyable (com.alipay.sofa.rpc.base.Destroyable)2 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 ProviderBootstrap (com.alipay.sofa.rpc.bootstrap.ProviderBootstrap)1 AddressHolder (com.alipay.sofa.rpc.client.AddressHolder)1 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 TestChainRouter1 (com.alipay.sofa.rpc.client.router.TestChainRouter1)1 TestChainRouter2 (com.alipay.sofa.rpc.client.router.TestChainRouter2)1 TestChainRouter3 (com.alipay.sofa.rpc.client.router.TestChainRouter3)1 TestChainRouter4 (com.alipay.sofa.rpc.client.router.TestChainRouter4)1 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)1 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)1 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1