use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class LazyBoltClientMain method main.
public static void main(String[] args) throws InterruptedException {
ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setApplication(application).setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22100").setRegister(false).setLazy(true).setTimeout(3000);
HelloService helloService = consumerConfig.refer();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
try {
Thread.sleep(5000);
} catch (Exception e) {
}
while (true) {
try {
String s = helloService.sayHello("xxx", 22);
LOGGER.warn("{}", s);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
try {
Thread.sleep(2000);
} catch (Exception e) {
}
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class GenericClientMain method main.
public static void main(String[] args) {
ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("generic-client");
ConsumerConfig<GenericService> consumerConfig = new ConsumerConfig<GenericService>().setApplication(applicationConfig).setInterfaceId(TestGenericService.class.getName()).setGeneric(true).setTimeout(50000).setDirectUrl("bolt://127.0.0.1:22222?appName=generic-server");
GenericService testService = consumerConfig.refer();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
while (true) {
try {
String s1 = (String) testService.$invoke("echoStr", new String[] { "java.lang.String" }, new Object[] { "1111" });
LOGGER.warn("generic return :{}", s1);
GenericObject genericObject = new GenericObject("com.alipay.sofa.rpc.invoke.generic.TestObj");
genericObject.putField("str", "xxxx");
genericObject.putField("num", 222);
GenericObject o2 = (GenericObject) testService.$genericInvoke("echoObj", new String[] { "com.alipay.sofa.rpc.invoke.generic.TestObj" }, new Object[] { genericObject });
LOGGER.warn("generic return :{}", o2);
TestObj o3 = testService.$genericInvoke("echoObj", new String[] { "com.alipay.sofa.rpc.invoke.generic.TestObj" }, new Object[] { genericObject }, TestObj.class);
LOGGER.warn("generic return :{}", o3);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
try {
Thread.sleep(2000);
} catch (Exception ignore) {
}
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig 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.ApplicationConfig 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.ApplicationConfig in project sofa-rpc by sofastack.
the class SofaRegistryTest method testSubTwice.
@Test
public void testSubTwice() throws Exception {
int timeoutPerSub = 5000;
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);
// 订阅
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.assertEquals(0, 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, TimeUnit.MILLISECONDS);
Map<String, ProviderGroup> ps2 = providerInfoListener2.getData();
Assert.assertTrue(ps2.size() > 0);
Assert.assertNotNull(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
Assert.assertEquals(0, ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
Assert.assertEquals(1, registry.subscribers.size());
Assert.assertEquals(1, registry.configurators.size());
latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
Assert.assertTrue(ps2.size() > 0);
Assert.assertNotNull(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
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());
}
Aggregations