use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class BoltConsumerBootstrapTest method testAttrUpdate.
@Test
public void testAttrUpdate() {
// 发布一个服务
ServerConfig serverConfig0 = new ServerConfig().setStopTimeout(0).setPort(22224).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
ProviderConfig<HelloService> providerConfig0 = new ProviderConfig<HelloService>().setId("p-0").setInterfaceId(HelloService.class.getName()).setUniqueId("attr").setRef(new HelloServiceImpl(1000)).setServer(serverConfig0).setRepeatedExportLimit(5);
providerConfig0.export();
ConsumerConfig<HelloService> consumerConfig0 = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setUniqueId("attr").setProxy("jdk").setDirectUrl("bolt://127.0.0.1:22224").setTimeout(500);
HelloService proxy = consumerConfig0.refer();
Invoker invoker = JDKProxy.parseInvoker(proxy);
Cluster cluster = consumerConfig0.getConsumerBootstrap().getCluster();
boolean error = false;
try {
proxy.sayHello("11", 11);
} catch (Exception e) {
LOGGER.info(e.getMessage());
error = true;
}
Assert.assertTrue(error);
// wrong key
error = false;
try {
consumerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("loadbalance", "asdasd"));
} catch (Exception e) {
error = true;
}
Assert.assertFalse(error);
// wrong value
error = false;
try {
consumerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("loadBalancer", "asdasd"));
} catch (Exception e) {
error = true;
}
Assert.assertFalse(error);
// 动态加大超时时间
consumerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("timeout", "2000"));
Invoker invoker2 = JDKProxy.parseInvoker(proxy);
Cluster cluster2 = consumerConfig0.getConsumerBootstrap().getCluster();
error = false;
try {
proxy.sayHello("11", 11);
} catch (Exception e) {
e.printStackTrace();
error = true;
}
Assert.assertFalse(error);
Assert.assertTrue(invoker == invoker2);
Assert.assertTrue(cluster != cluster2);
// 切到一个没有的分组
consumerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("uniqueId", "attr2"));
error = false;
try {
proxy.sayHello("11", 11);
} catch (Exception e) {
error = true;
}
Assert.assertTrue(error);
// 切到一个有的分组
consumerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("uniqueId", "attr"));
error = false;
try {
proxy.sayHello("11", 11);
} catch (Exception e) {
error = true;
}
Assert.assertFalse(error);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class TempClassLoader method export.
@Test
public void export() throws Exception {
// 发布一个服务,每个请求要执行2秒
ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22223).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
ProviderConfig<HelloService> providerConfig0 = new ProviderConfig<HelloService>().setId("p-0").setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(2000)).setServer(serverConfig).setRegister(false);
providerConfig0.export();
ProviderConfig<HelloService> providerConfig1 = new ProviderConfig<HelloService>().setId("p-1").setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(2000)).setServer(serverConfig).setRegister(false);
try {
providerConfig1.export();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof SofaRpcRuntimeException);
}
ProviderConfig<HelloService> providerConfig2 = new ProviderConfig<HelloService>().setId("p-2").setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(2000)).setServer(serverConfig).setRepeatedExportLimit(2).setRegister(false);
providerConfig2.export();
ProviderConfig<HelloService> providerConfig3 = new ProviderConfig<HelloService>().setId("p-3").setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(2000)).setServer(serverConfig).setRepeatedExportLimit(2).setRegister(false);
try {
providerConfig3.export();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof SofaRpcRuntimeException);
}
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class BizThrowExceptionTest method testAll.
@Test
public void testAll() {
// 只有2个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<TestExceptionService> providerConfig = new ProviderConfig<TestExceptionService>().setInterfaceId(TestExceptionService.class.getName()).setRef(new TestExceptionServiceImpl()).setServer(serverConfig).setRegister(false);
providerConfig.export();
for (String proxy : new String[] { "jdk", "javassist" }) {
ConsumerConfig<TestExceptionService> consumerConfig = new ConsumerConfig<TestExceptionService>().setInterfaceId(TestExceptionService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(1000).setProxy(proxy).setRepeatedReferLimit(-1).setRegister(false);
final TestExceptionService service = consumerConfig.refer();
try {
service.throwRuntimeException();
Assert.fail();
} catch (Throwable e) {
Assert.assertTrue(e instanceof RuntimeException);
Assert.assertEquals(e.getMessage(), "RuntimeException");
}
try {
service.throwException();
Assert.fail();
} catch (Throwable e) {
Assert.assertTrue(e instanceof Exception);
Assert.assertEquals(e.getMessage(), "Exception");
}
try {
service.throwSofaException();
Assert.fail();
} catch (Throwable e) {
Assert.assertTrue(e instanceof SofaRpcException);
Assert.assertEquals(e.getMessage(), "SofaRpcException");
}
try {
service.throwDeclaredException();
Assert.fail();
} catch (Throwable e) {
Assert.assertTrue(e instanceof TestException);
Assert.assertEquals(e.getMessage(), "TestException");
}
try {
service.throwDeclaredExceptionWithoutReturn();
Assert.fail();
} catch (Throwable e) {
Assert.assertTrue(e instanceof TestException);
Assert.assertEquals(e.getMessage(), "DeclaredExceptionWithoutReturn");
}
}
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class FutureServerMain method main.
public static void main(String[] args) {
ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("future-server");
ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setApplication(applicationConfig).setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setServer(serverConfig2);
providerConfig.export();
ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>().setApplication(applicationConfig).setInterfaceId(EchoService.class.getName()).setRef(new EchoServiceImpl()).setServer(serverConfig2);
providerConfig2.export();
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class OnewayServerMain method main.
public static void main(String[] args) {
ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("oneway-server");
ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setApplication(applicationConfig).setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setServer(serverConfig2);
providerConfig.export();
ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>().setApplication(applicationConfig).setInterfaceId(EchoService.class.getName()).setRef(new EchoServiceImpl()).setServer(serverConfig2);
providerConfig2.export();
}
Aggregations