use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class DubooServerTest method testFuture.
@Test
public // future调用,从future中取值.
void testFuture() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
ApplicationConfig serverApplacation = new ApplicationConfig();
serverApplacation.setAppName("server");
providerConfig = new ProviderConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setRef(new DemoServiceImpl()).setServer(serverConfig).setBootstrap("dubbo").setRegister(false).setApplication(serverApplacation);
providerConfig.export();
ApplicationConfig clientApplication = new ApplicationConfig();
clientApplication.setAppName("client");
List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
MethodConfig methodConfig = new MethodConfig();
methodConfig.setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
methodConfig.setName("sayHello");
consumerConfig = new ConsumerConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setDirectUrl("dubbo://127.0.0.1:20880").setTimeout(30000).setRegister(false).setProtocol("dubbo").setBootstrap("dubbo").setApplication(clientApplication).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setMethods(methodConfigs);
final DemoService demoService = consumerConfig.refer();
String result = demoService.sayHello("xxx");
Assert.assertEquals(null, result);
Future<Object> future = RpcContext.getContext().getFuture();
String futureResult = null;
try {
futureResult = (String) future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Assert.assertEquals("Hello xxx", futureResult);
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class DubooServerTest method testSync.
@Test
public // 同步调用,直连
void testSync() {
try {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
ApplicationConfig serverApplacation = new ApplicationConfig();
serverApplacation.setAppName("server");
providerConfig = new ProviderConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setRef(new DemoServiceImpl()).setBootstrap("dubbo").setServer(serverConfig).setRegister(false).setApplication(serverApplacation);
providerConfig.export();
ApplicationConfig clientApplication = new ApplicationConfig();
clientApplication.setAppName("client");
consumerConfig = new ConsumerConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setDirectUrl("dubbo://127.0.0.1:20880").setBootstrap("dubbo").setTimeout(30000).setRegister(false).setProtocol("dubbo").setApplication(clientApplication);
final DemoService demoService = consumerConfig.refer();
String result = demoService.sayHello("xxx");
Assert.assertTrue(result.equalsIgnoreCase("hello xxx"));
} catch (Exception e) {
e.printStackTrace();
Assert.assertTrue(false);
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class DubooServerTest method testConsumerWithNoDubboServiceVersion.
@Test(expected = com.alibaba.dubbo.rpc.RpcException.class)
public // 同步调用,直连,dubbo 消费没有指定dubbo服务版本version
void testConsumerWithNoDubboServiceVersion() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
ApplicationConfig serverApplacation = new ApplicationConfig();
serverApplacation.setAppName("server");
providerConfig = new ProviderConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setRef(new DemoServiceImpl()).setBootstrap("dubbo").setServer(serverConfig).setParameter("version", "1.0.1").setRegister(false).setApplication(serverApplacation);
providerConfig.export();
ApplicationConfig clientApplication = new ApplicationConfig();
clientApplication.setAppName("client");
consumerConfig = new ConsumerConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setDirectUrl("dubbo://127.0.0.1:20880").setBootstrap("dubbo").setTimeout(30000).setRegister(false).setProtocol("dubbo").setApplication(clientApplication);
final DemoService demoService = consumerConfig.refer();
String result = demoService.sayHello("xxx");
Assert.assertTrue(result.equalsIgnoreCase("hello xxx"));
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class ClientA method main.
public static void main(String[] args) {
// A 服务
ConsumerConfig<ServiceB> consumerConfig = new ConsumerConfig<ServiceB>().setApplication(new ApplicationConfig().setAppName("AAA")).setInterfaceId(ServiceB.class.getName()).setDirectUrl("bolt://127.0.0.1:12298?appName=BBB").setRegister(false).setTimeout(3000);
ServiceB serviceB = consumerConfig.refer();
while (true) {
try {
int ret0 = serviceB.getInt(999);
LOGGER.info("ret0:" + ret0);
} catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (Exception e) {
}
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class BoltClientMain 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:22000").setRegister(false).setTimeout(3000);
HelloService helloService = consumerConfig.refer();
ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setApplication(application).setDirectUrl("bolt://127.0.0.1:22000").setRegister(false).setTimeout(3000);
EchoService echoService = consumerConfig2.refer();
consumerConfig2.unRefer();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
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) {
}
}
}
Aggregations