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);
}
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;
}
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() + "]");
}
}
}
}
}
Aggregations