Search in sources :

Example 6 with Registry

use of com.alipay.sofa.rpc.registry.Registry 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)

Example 7 with Registry

use of com.alipay.sofa.rpc.registry.Registry in project sofa-boot by sofastack.

the class RpcBindingAdapter method outBinding.

/**
 * out binding, out binding means provide service
 *
 * @param contract           binding contract
 * @param binding            binding object
 * @param target             binding target
 * @param sofaRuntimeContext sofa runtime context
 * @return binding result
 */
@Override
public Object outBinding(Object contract, RpcBinding binding, Object target, SofaRuntimeContext sofaRuntimeContext) {
    ApplicationContext applicationContext = sofaRuntimeContext.getSofaRuntimeManager().getRootApplicationContext();
    ProviderConfigContainer providerConfigContainer = applicationContext.getBean(ProviderConfigContainer.class);
    ProcessorContainer processorContainer = applicationContext.getBean(ProcessorContainer.class);
    String uniqueName = providerConfigContainer.createUniqueName((Contract) contract, binding);
    ProviderConfig providerConfig = providerConfigContainer.getProviderConfig(uniqueName);
    processorContainer.processorProvider(providerConfig);
    if (providerConfig == null) {
        throw new ServiceRuntimeException(LogCodes.getLog(LogCodes.INFO_SERVICE_METADATA_IS_NULL, uniqueName));
    }
    try {
        providerConfig.export();
    } catch (Exception e) {
        throw new ServiceRuntimeException(LogCodes.getLog(LogCodes.ERROR_PROXY_PUBLISH_FAIL), e);
    }
    if (providerConfigContainer.isAllowPublish()) {
        providerConfig.setRegister(true);
        List<RegistryConfig> registrys = providerConfig.getRegistry();
        for (RegistryConfig registryConfig : registrys) {
            Registry registry = RegistryFactory.getRegistry(registryConfig);
            registry.init();
            registry.start();
            registry.register(providerConfig);
        }
    }
    return Boolean.TRUE;
}
Also used : ProcessorContainer(com.alipay.sofa.rpc.boot.runtime.adapter.processor.ProcessorContainer) RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ApplicationContext(org.springframework.context.ApplicationContext) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ProviderConfigContainer(com.alipay.sofa.rpc.boot.container.ProviderConfigContainer) Registry(com.alipay.sofa.rpc.registry.Registry) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 8 with Registry

use of com.alipay.sofa.rpc.registry.Registry in project sofa-boot by sofastack.

the class ProviderConfigContainer method publishAllProviderConfig.

/**
 * 发布所有 ProviderConfig 元数据信息到注册中心
 */
public void publishAllProviderConfig() {
    for (ProviderConfig providerConfig : getAllProviderConfig()) {
        ServerConfig serverConfig = (ServerConfig) providerConfig.getServer().get(0);
        if (!serverConfig.getProtocol().equalsIgnoreCase(SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO)) {
            providerConfig.setRegister(true);
            List<RegistryConfig> registrys = providerConfig.getRegistry();
            for (RegistryConfig registryConfig : registrys) {
                Registry registry = RegistryFactory.getRegistry(registryConfig);
                registry.init();
                registry.start();
                registry.register(providerConfig);
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("service published.  interfaceId[" + providerConfig.getInterfaceId() + "]; protocol[" + serverConfig.getProtocol() + "]");
                }
            }
        }
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Registry(com.alipay.sofa.rpc.registry.Registry)

Aggregations

Registry (com.alipay.sofa.rpc.registry.Registry)8 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)7 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)5 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)3 ProviderConfigContainer (com.alipay.sofa.rpc.boot.container.ProviderConfigContainer)2 ProcessorContainer (com.alipay.sofa.rpc.boot.runtime.adapter.processor.ProcessorContainer)2 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)2 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)2 ArrayList (java.util.ArrayList)2 ApplicationContext (org.springframework.context.ApplicationContext)2 Destroyable (com.alipay.sofa.rpc.base.Destroyable)1 ConsumerBootstrap (com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap)1 ProviderBootstrap (com.alipay.sofa.rpc.bootstrap.ProviderBootstrap)1 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)1 BaseZkTest (com.alipay.sofa.rpc.registry.base.BaseZkTest)1 HelloService (com.alipay.sofa.rpc.test.HelloService)1 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)1 BoltClientTransport (com.alipay.sofa.rpc.transport.bolt.BoltClientTransport)1