Search in sources :

Example 16 with ProviderConfig

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);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Invoker(com.alipay.sofa.rpc.invoke.Invoker) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) Cluster(com.alipay.sofa.rpc.client.Cluster) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 17 with ProviderConfig

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);
    }
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 18 with ProviderConfig

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");
        }
    }
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest) Test(org.junit.Test)

Example 19 with ProviderConfig

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();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) EchoService(com.alipay.sofa.rpc.test.EchoService) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) EchoServiceImpl(com.alipay.sofa.rpc.test.EchoServiceImpl)

Example 20 with ProviderConfig

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();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) EchoService(com.alipay.sofa.rpc.test.EchoService) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) EchoServiceImpl(com.alipay.sofa.rpc.test.EchoServiceImpl)

Aggregations

ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)121 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)88 Test (org.junit.Test)73 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)52 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)47 HelloService (com.alipay.sofa.rpc.test.HelloService)33 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)33 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)28 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)21 CountDownLatch (java.util.concurrent.CountDownLatch)19 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)17 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 Filter (com.alipay.sofa.rpc.filter.Filter)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 EchoService (com.alipay.sofa.rpc.test.EchoService)9 ArrayList (java.util.ArrayList)9 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)8 EchoServiceImpl (com.alipay.sofa.rpc.test.EchoServiceImpl)8 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)7 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)7