use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class FailoverClusterTest method testRpcDirectInvokeFromContextWithAvailableProviders.
@Test
public void testRpcDirectInvokeFromContextWithAvailableProviders() {
ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(13900);
ProviderConfig<HelloService> provider = new ProviderConfig();
provider.setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl("x-demo-invoke")).setApplication(new ApplicationConfig().setAppName("x-test-server")).setProxy("javassist").setSerialization("hessian2").setServer(serverConfig).setTimeout(3000);
provider.export();
ConsumerConfig<HelloService> consumer = new ConsumerConfig();
consumer.setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("x-test-client")).setDirectUrl("bolt://127.0.0.1:65534").setProxy("javassist");
HelloService proxy = consumer.refer();
for (int i = 0; i < 3; i++) {
RpcInvokeContext.getContext().setTargetURL("127.0.0.1:13900");
Assert.assertEquals("x-demo-invoke", proxy.sayHello("x-demo-invoke", 1));
}
provider.unExport();
consumer.unRefer();
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class LazyConnectTest method testLazyFailWhenMultipleThreads.
@Test
public void testLazyFailWhenMultipleThreads() {
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22223").setTimeout(1000).setLazy(true).setRepeatedReferLimit(-1).setRegister(false);
final HelloService helloService = consumerConfig.refer();
int threads = 10;
final AtomicInteger count1 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(threads);
for (int i = 0; i < threads; i++) {
try {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 10; j++) {
try {
helloService.sayHello("xxx", 20);
count1.incrementAndGet();
} catch (Exception ignore) {
}
}
latch.countDown();
}
}, "thread" + i);
thread.start();
} catch (Exception ignore) {
}
}
try {
latch.await(3000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
}
Assert.assertEquals(count1.get(), 0);
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class LazyConnectTest method testLazy.
@Test
public void testLazy() {
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(1000).setLazy(true).setRepeatedReferLimit(-1).setRegister(false);
final HelloService helloService = consumerConfig.refer();
int count1 = 0;
for (int i = 0; i < 4; i++) {
try {
helloService.sayHello("xxx", 20 + i);
count1++;
} catch (Exception ignore) {
}
}
Assert.assertEquals(count1, 4);
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class AsyncCallbackTest method testAll.
@Test
public void testAll() {
serverConfig = new ServerConfig().setPort(22220).setDaemon(false);
// C服务的服务端
CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setServer(serverConfig);
CProvider.export();
// B调C的客户端
Filter filter = new TestAsyncFilter();
BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(50000).setFilterRef(Arrays.asList(filter)).setDirectUrl("bolt://127.0.0.1:22220");
HelloService helloService = BConsumer.refer();
final CountDownLatch latch = new CountDownLatch(1);
final String[] ret = { null };
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
LOGGER.info("B get result: {}", appResponse);
ret[0] = (String) appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
LOGGER.info("B get app exception: {}", throwable);
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
LOGGER.info("B get sofa exception: {}", sofaException);
latch.countDown();
}
});
String ret0 = helloService.sayHello("xxx", 22);
// 第一次返回null
Assert.assertNull(ret0);
try {
latch.await(60000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
Assert.assertNotNull(ret[0]);
// 过滤器生效
assertTrue(ret[0].endsWith("append by async filter"));
RpcInvokeContext.removeContext();
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class RpcContextTest method testAll.
@Test
public void testAll() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
CtxHelloServiceImpl helloServiceImpl = new CtxHelloServiceImpl();
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-server")).setRef(helloServiceImpl).setServer(serverConfig).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-client")).setDirectUrl("bolt://127.0.0.1:22222?appName=test-server").setTimeout(30000).setRegister(false);
final HelloService helloService = consumerConfig.refer();
String str = helloService.sayHello("xxx", 123);
RpcServiceContext serviceContext = RpcContextManager.currentServiceContext(false);
RpcReferenceContext referenceContext = RpcContextManager.lastReferenceContext(false);
Assert.assertNull(serviceContext);
Assert.assertNotNull(referenceContext);
serviceContext = helloServiceImpl.serviceContext;
Assert.assertNotNull(serviceContext);
Assert.assertEquals(serviceContext.getCallerAppName(), "test-client");
Assert.assertEquals(referenceContext.getTargetAppName(), "test-server");
Assert.assertNotNull(referenceContext.getClientIP());
Assert.assertTrue(referenceContext.getClientPort() > 0);
}
{
final CountDownLatch latch = new CountDownLatch(1);
final String[] ret = { null };
ConsumerConfig<HelloService> consumerConfig2 = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-client")).setDirectUrl("bolt://127.0.0.1:22222?appName=test-server").setTimeout(2000).setInvokeType("callback").setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
ret[0] = (String) appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
latch.countDown();
}
}).setRegister(false);
final HelloService helloServiceCallback = consumerConfig2.refer();
String ret0 = helloServiceCallback.sayHello("xxx", 22);
// 第一次返回null
Assert.assertNull(ret0);
RpcServiceContext serviceContext = RpcContextManager.currentServiceContext(false);
RpcReferenceContext referenceContext = RpcContextManager.lastReferenceContext(false);
Assert.assertNull(serviceContext);
Assert.assertNotNull(referenceContext);
serviceContext = helloServiceImpl.serviceContext;
Assert.assertNotNull(serviceContext);
Assert.assertEquals(serviceContext.getCallerAppName(), "test-client");
Assert.assertEquals(referenceContext.getTargetAppName(), "test-server");
Assert.assertNotNull(referenceContext.getClientIP());
Assert.assertTrue(referenceContext.getClientPort() > 0);
try {
latch.await(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
Assert.assertNotNull(ret[0]);
}
}
Aggregations