Search in sources :

Example 1 with BoltClientTransport

use of com.alipay.sofa.rpc.transport.bolt.BoltClientTransport in project sofa-rpc by sofastack.

the class ReconnectTest method testReconnect.

@Test
public void testReconnect() throws Exception {
    ServerConfig serverConfig1 = new ServerConfig().setStopTimeout(0).setPort(22221).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setServer(serverConfig1).setRepeatedExportLimit(-1).setRegister(false);
    providerConfig.export();
    final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22221").setConnectionHolder("all").setRegister(false).setLazy(true).setReconnectPeriod(2000).setTimeout(3000);
    HelloService helloService = consumerConfig.refer();
    Assert.assertNotNull(helloService.sayHello("xxx", 11));
    // Mock server down, and RPC will throw exception(no available provider)
    providerConfig.unExport();
    ServerFactory.destroyAll();
    BoltClientTransport clientTransport = (BoltClientTransport) consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableClientTransport(ProviderHelper.toProviderInfo("bolt://127.0.0.1:22221"));
    clientTransport.disconnect();
    TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().isAvailableEmpty();
        }
    }, true, 100, 30);
    try {
        helloService.sayHello("xxx", 11);
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains(LogCodes.ERROR_TARGET_URL_INVALID));
    }
    // Mock server restart
    serverConfig1 = new ServerConfig().setStopTimeout(0).setPort(22221).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    providerConfig.setServer(Arrays.asList(serverConfig1)).export();
    // The consumer will reconnect to provider automatically
    TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().isAvailableEmpty();
        }
    }, false, 100, 30);
    // RPC return success
    Assert.assertNotNull(helloService.sayHello("xxx", 11));
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) BoltClientTransport(com.alipay.sofa.rpc.transport.bolt.BoltClientTransport) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 2 with BoltClientTransport

use of com.alipay.sofa.rpc.transport.bolt.BoltClientTransport in project sofa-rpc by sofastack.

the class ZookeeperServerRestartTest method testAll.

@Test
public void testAll() throws Exception {
    final RegistryConfig registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK).setAddress("127.0.0.1:2181").setConnectTimeout(100);
    final ZookeeperRegistry registry = (ZookeeperRegistry) RegistryFactory.getRegistry(registryConfig);
    registry.start();
    final ServerConfig serverConfig2 = new ServerConfig().setPort(22111).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
    final ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl("22")).setServer(serverConfig2).setRegistry(registryConfig).setRepeatedExportLimit(-1);
    providerConfig.export();
    final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRegistry(registryConfig).setTimeout(3333).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setReconnectPeriod(// avoid reconnect in this test case
    60000);
    HelloService helloService = consumerConfig.refer();
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
            }
            // mock network disconnect cause by server force kill (kill -9)
            BoltClientTransport clientTransport = (BoltClientTransport) consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections().values().iterator().next();
            clientTransport.disconnect();
            serverConfig2.getServer().stop();
        }
    });
    thread.start();
    Thread thread2 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(5200);
            } catch (InterruptedException e) {
            }
            serverConfig2.getServer().start();
            Registry registry1 = RegistryFactory.getRegistry(registryConfig);
            // mock server restart and register provider
            // if we don't unRegistry,create will fail,beacuse data is not clean quickly.
            registry1.unRegister(providerConfig);
            registry1.register(providerConfig);
        }
    });
    thread2.start();
    int times = 8;
    int count = 0;
    for (int i = 0; i < times; i++) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        try {
            LOGGER.info("result is {}", helloService.sayHello("11", 1));
            count++;
        } catch (Exception e) {
            LOGGER.warn(e.getMessage());
        }
    }
    Assert.assertTrue(count >= 4);
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) BoltClientTransport(com.alipay.sofa.rpc.transport.bolt.BoltClientTransport) Registry(com.alipay.sofa.rpc.registry.Registry) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Test(org.junit.Test) BaseZkTest(com.alipay.sofa.rpc.registry.base.BaseZkTest)

Aggregations

ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)2 HelloService (com.alipay.sofa.rpc.test.HelloService)2 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)2 BoltClientTransport (com.alipay.sofa.rpc.transport.bolt.BoltClientTransport)2 Test (org.junit.Test)2 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)1 Registry (com.alipay.sofa.rpc.registry.Registry)1 BaseZkTest (com.alipay.sofa.rpc.registry.base.BaseZkTest)1 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)1