use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class LocalBoltServerMain method main.
public static void main(String[] args) {
String file = System.getProperty("user.home") + File.separator + "localFileTest" + File.separator + "localRegistry.reg";
RegistryConfig registryConfig = new RegistryConfig().setProtocol("local").setFile(file);
ServerConfig serverConfig = new ServerConfig().setPort(22222).setDaemon(false);
ServerConfig serverConfig2 = new ServerConfig().setPort(22200).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setDaemon(false);
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setServer(serverConfig).setRegistry(registryConfig);
ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setRef(new EchoServiceImpl()).setServer(serverConfig).setRegistry(registryConfig);
providerConfig.export();
providerConfig2.export();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class GenericServerMain method main.
public static void main(String[] args) {
ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("generic-server");
ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
ProviderConfig<TestGenericService> providerConfig = new ProviderConfig<TestGenericService>().setApplication(applicationConfig).setInterfaceId(TestGenericService.class.getName()).setRef(new TestGenericServiceImpl()).setServer(serverConfig2);
providerConfig.export();
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class GenericTripleDemo method main.
public static void main(String[] args) {
RpcRunningState.setDebugMode(true);
ApplicationConfig clientApp = new ApplicationConfig().setAppName("triple-client");
ApplicationConfig serverApp = new ApplicationConfig().setAppName("triple-server");
int port = 50052;
if (args.length != 0) {
LOGGER.debug("first arg is {}", args[0]);
port = Integer.valueOf(args[0]);
}
RegistryConfig registryConfig = new RegistryConfig().setProtocol("zookeeper").setAddress("127.0.0.1:2181");
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
ProviderConfig<OriginHello> providerConfig = new ProviderConfig<OriginHello>().setApplication(serverApp).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(OriginHello.class.getName()).setRef(new OriginHelloImpl()).setServer(serverConfig).setRegistry(registryConfig);
providerConfig.export();
ConsumerConfig<OriginHello> consumerConfig = new ConsumerConfig<OriginHello>();
consumerConfig.setInterfaceId(OriginHello.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setRegistry(registryConfig).setApplication(clientApp);
OriginHello helloService = consumerConfig.refer();
LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
LOGGER.info("Will try to greet " + "world" + " ...");
while (true) {
try {
try {
HelloRequest1 helloRequest1 = new HelloRequest1();
helloRequest1.setName("ab");
HelloRequest2 helloRequest2 = new HelloRequest2();
helloRequest2.setName("cd");
HelloResponse result = helloService.hello2(helloRequest1, helloRequest2);
LOGGER.info("Invoke Success,hello: {} ", result.getMessage());
} catch (StatusRuntimeException e) {
LOGGER.error("RPC failed: {}", e.getStatus());
} catch (Throwable e) {
LOGGER.error("Unexpected RPC call breaks", e);
}
} catch (Exception e) {
LOGGER.error("Unexpected RPC call breaks", e);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class PolarisBoltServerMain method main.
public static void main(String[] args) {
RegistryConfig registryConfig = new RegistryConfig().setProtocol("polaris").setAddress("127.0.0.1:8091");
ServerConfig serverConfig = new ServerConfig().setPort(22101).setDaemon(false);
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl("result from 22101")).setServer(serverConfig).setRegistry(registryConfig);
ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setRef(new EchoServiceImpl()).setServer(serverConfig).setRegistry(registryConfig);
providerConfig.export();
providerConfig2.export();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class SofaRegistryTest method testAll.
@Test
public void testAll() throws Exception {
int timeoutPerSub = 5000;
serverConfig1 = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
provider = new ProviderConfig();
provider.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setServer(serverConfig1).setWeight(222).setTimeout(3000);
// 注册
registry.register(provider);
consumer1 = new ConsumerConfig();
consumer1.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
String tag0 = SofaRegistryHelper.buildListDataId(provider, serverConfig1.getProtocol());
String tag1 = SofaRegistryHelper.buildListDataId(consumer1, consumer1.getProtocol());
Assert.assertEquals(tag1, tag0);
// 订阅
CountDownLatch latch = new CountDownLatch(2);
MockProviderInfoListener providerInfoListener = new MockProviderInfoListener();
providerInfoListener.setCountDownLatch(latch);
consumer1.setProviderInfoListener(providerInfoListener);
registry.subscribe(consumer1);
latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
Map<String, ProviderGroup> ps = providerInfoListener.getData();
Assert.assertTrue(ps.size() > 0);
Assert.assertNotNull(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
Assert.assertTrue(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size() > 0);
// 反注册
latch = new CountDownLatch(1);
providerInfoListener.setCountDownLatch(latch);
registry.unRegister(provider);
latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
Assert.assertEquals(ps.toString(), 1, ps.size());
Assert.assertEquals(0, ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
// 一次发2个端口的再次注册
latch = new CountDownLatch(2);
providerInfoListener.setCountDownLatch(latch);
serverConfig2 = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12201);
provider.getServer().add(serverConfig2);
registry.register(provider);
latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
Assert.assertTrue(ps.size() > 0);
Assert.assertNotNull(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
Assert.assertEquals(2, ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
// 重复订阅
consumer2 = new ConsumerConfig();
consumer2.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
CountDownLatch latch2 = new CountDownLatch(1);
MockProviderInfoListener providerInfoListener2 = new MockProviderInfoListener();
providerInfoListener2.setCountDownLatch(latch2);
consumer2.setProviderInfoListener(providerInfoListener2);
registry.subscribe(consumer2);
latch2.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
Map<String, ProviderGroup> ps2 = providerInfoListener2.getData();
Assert.assertTrue(ps2.size() > 0);
Assert.assertNotNull(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
Assert.assertEquals(2, ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
Assert.assertEquals(1, registry.subscribers.size());
Assert.assertEquals(1, registry.configurators.size());
// 取消订阅者1
registry.unSubscribe(consumer1);
SofaRegistrySubscribeCallback callback = (SofaRegistrySubscribeCallback) registry.subscribers.get(tag1).getDataObserver();
Assert.assertFalse(callback.providerInfoListeners.contains(consumer1));
Assert.assertEquals(1, registry.subscribers.size());
Assert.assertEquals(1, registry.configurators.size());
// 批量反注册,判断订阅者2的数据
latch = new CountDownLatch(2);
providerInfoListener2.setCountDownLatch(latch);
List<ProviderConfig> providerConfigList = new ArrayList<ProviderConfig>();
providerConfigList.add(provider);
registry.batchUnRegister(providerConfigList);
latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
Assert.assertEquals(ps2.toString(), 1, ps2.size());
Assert.assertEquals(0, ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
// 1个服务 订阅服务列表和服务配置 2个dataId
Assert.assertEquals(1, registry.subscribers.size());
Assert.assertEquals(1, registry.configurators.size());
// 批量取消订阅
List<ConsumerConfig> consumerConfigList = new ArrayList<ConsumerConfig>();
consumerConfigList.add(consumer2);
registry.batchUnSubscribe(consumerConfigList);
Assert.assertEquals(0, registry.subscribers.size());
Assert.assertEquals(0, registry.configurators.size());
}
Aggregations