use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class TripleServerTest method testSyncWithUniqueId.
@Test
public // 同步调用,直连 有uniqueId
void testSyncWithUniqueId() {
ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("triple-server");
int port = 50052;
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
ProviderConfig<SofaGreeterTriple.IGreeter> providerConfig = new ProviderConfig<SofaGreeterTriple.IGreeter>().setApplication(applicationConfig).setUniqueId("abc").setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setRef(new GreeterImpl()).setServer(serverConfig);
providerConfig.export();
ConsumerConfig<SofaGreeterTriple.IGreeter> consumerConfig = new ConsumerConfig<SofaGreeterTriple.IGreeter>();
consumerConfig.setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setUniqueId("abc").setDirectUrl("tri://127.0.0.1:" + port);
SofaGreeterTriple.IGreeter greeterBlockingStub = consumerConfig.refer();
HelloRequest.DateTime dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00").build();
HelloReply reply = null;
HelloRequest request = HelloRequest.newBuilder().setName("world").setDateTime(dateTime).build();
reply = greeterBlockingStub.sayHello(request);
Assert.assertNotNull(reply);
consumerConfig = new ConsumerConfig<SofaGreeterTriple.IGreeter>();
consumerConfig.setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("tri://127.0.0.1:" + port);
greeterBlockingStub = consumerConfig.refer();
dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00").build();
request = HelloRequest.newBuilder().setName("world").setDateTime(dateTime).build();
reply = greeterBlockingStub.sayHello(request);
Assert.assertNotNull(reply);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class FailoverClusterTest method testSingleServer.
@Test
public void testSingleServer() {
// 只有2个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl() {
AtomicInteger cnt = new AtomicInteger();
@Override
public String sayHello(String name, int age) {
if (cnt.getAndIncrement() % 3 != 0) {
try {
Thread.sleep(2000);
} catch (Exception ignore) {
}
}
LOGGER.info("xxxxxxxxxxxxxxxxx" + age);
return "hello " + name + " from server! age: " + age;
}
}).setServer(serverConfig).setRegister(false);
providerConfig.export();
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setCluster("failover").setTimeout(1000).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(2, count1);
ConsumerConfig<HelloService> consumerConfig2 = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(1000).setCluster("failover").setRetries(// 失败后自动重试2次
2).setRegister(false);
final HelloService helloService2 = consumerConfig2.refer();
int count2 = 0;
for (int i = 0; i < 4; i++) {
try {
helloService2.sayHello("xxx", 22);
count2++;
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
Assert.assertEquals(4, count2);
Cluster cluster = consumerConfig2.getConsumerBootstrap().getCluster();
Assert.assertTrue(cluster.isAvailable());
}
use of com.alipay.sofa.rpc.config.ProviderConfig 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.ProviderConfig in project sofa-rpc by sofastack.
the class LazyConnectTest method startServer.
@BeforeClass
public static void startServer() {
RpcRunningState.setUnitTestMode(true);
// 只有2个线程 执行
serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setServer(serverConfig).setRegister(false);
providerConfig.export();
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class AsyncCallbackTest method testCallbackCallerHandleException.
@Test
public void testCallbackCallerHandleException() {
serverConfig = new ServerConfig().setPort(22223).setDaemon(false);
// RpcServer for C
CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(0)).setServer(serverConfig);
CProvider.export();
// RpcClient For B invoke C
Filter filter = new TestAsyncFilter();
BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(3000).setFilterRef(Arrays.asList(filter)).setRejectedExecutionPolicy(RejectedExecutionPolicy.CALLER_HANDLE_EXCEPTION.name()).setDirectUrl("bolt://127.0.0.1:22223");
HelloService helloService = BConsumer.refer();
int maxsize = RpcConfigs.getIntValue(RpcOptions.ASYNC_POOL_MAX);
int queuesize = RpcConfigs.getIntValue(RpcOptions.ASYNC_POOL_QUEUE);
int invokeCount = (maxsize + queuesize) * 2;
final CountDownLatch latch = new CountDownLatch(invokeCount);
AtomicInteger sofaExceptionCount = new AtomicInteger(0);
SofaResponseCallback callback = new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
LOGGER.info("B get result: {}", appResponse);
try {
Thread.sleep(100);
} catch (Exception e) {
}
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();
sofaExceptionCount.addAndGet(1);
}
};
for (int i = 0; i < invokeCount; i++) {
RpcInvokeContext.getContext().setResponseCallback(callback);
helloService.sayHello("" + i, 33);
}
try {
latch.await(100L * invokeCount, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
// Exception callbacks are triggered by IO threads after the thread pool is full, so the total number of responses received is equal to the total number of calls
assertEquals(0, latch.getCount());
// The number of exception callbacks triggered by IO threads must exist
assertTrue(sofaExceptionCount.get() > 0);
RpcInvokeContext.removeContext();
}
Aggregations