Search in sources :

Example 91 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.

the class LazyBoltClientMain 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:22100").setRegister(false).setLazy(true).setTimeout(3000);
    HelloService helloService = consumerConfig.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    try {
        Thread.sleep(5000);
    } catch (Exception e) {
    }
    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) {
        }
    }
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 92 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.

the class GenericClientMain method main.

public static void main(String[] args) {
    ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("generic-client");
    ConsumerConfig<GenericService> consumerConfig = new ConsumerConfig<GenericService>().setApplication(applicationConfig).setInterfaceId(TestGenericService.class.getName()).setGeneric(true).setTimeout(50000).setDirectUrl("bolt://127.0.0.1:22222?appName=generic-server");
    GenericService testService = consumerConfig.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    while (true) {
        try {
            String s1 = (String) testService.$invoke("echoStr", new String[] { "java.lang.String" }, new Object[] { "1111" });
            LOGGER.warn("generic return :{}", s1);
            GenericObject genericObject = new GenericObject("com.alipay.sofa.rpc.invoke.generic.TestObj");
            genericObject.putField("str", "xxxx");
            genericObject.putField("num", 222);
            GenericObject o2 = (GenericObject) testService.$genericInvoke("echoObj", new String[] { "com.alipay.sofa.rpc.invoke.generic.TestObj" }, new Object[] { genericObject });
            LOGGER.warn("generic return :{}", o2);
            TestObj o3 = testService.$genericInvoke("echoObj", new String[] { "com.alipay.sofa.rpc.invoke.generic.TestObj" }, new Object[] { genericObject }, TestObj.class);
            LOGGER.warn("generic return :{}", o3);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        try {
            Thread.sleep(2000);
        } catch (Exception ignore) {
        }
    }
}
Also used : GenericService(com.alipay.sofa.rpc.api.GenericService) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) GenericObject(com.alipay.hessian.generic.model.GenericObject) GenericObject(com.alipay.hessian.generic.model.GenericObject)

Example 93 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.

the class GenericServerMain method main.

public static void main(String[] args) {
    ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("generic-server");
    ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
    ProviderConfig<TestGenericService> providerConfig = new ProviderConfig<TestGenericService>().setApplication(applicationConfig).setInterfaceId(TestGenericService.class.getName()).setRef(new TestGenericServiceImpl()).setServer(serverConfig2);
    providerConfig.export();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig)

Example 94 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.

the class GenericTripleDemo method main.

public static void main(String[] args) {
    RpcRunningState.setDebugMode(true);
    ApplicationConfig clientApp = new ApplicationConfig().setAppName("triple-client");
    ApplicationConfig serverApp = new ApplicationConfig().setAppName("triple-server");
    int port = 50052;
    if (args.length != 0) {
        LOGGER.debug("first arg is {}", args[0]);
        port = Integer.valueOf(args[0]);
    }
    RegistryConfig registryConfig = new RegistryConfig().setProtocol("zookeeper").setAddress("127.0.0.1:2181");
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
    ProviderConfig<OriginHello> providerConfig = new ProviderConfig<OriginHello>().setApplication(serverApp).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(OriginHello.class.getName()).setRef(new OriginHelloImpl()).setServer(serverConfig).setRegistry(registryConfig);
    providerConfig.export();
    ConsumerConfig<OriginHello> consumerConfig = new ConsumerConfig<OriginHello>();
    consumerConfig.setInterfaceId(OriginHello.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setRegistry(registryConfig).setApplication(clientApp);
    OriginHello helloService = consumerConfig.refer();
    LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
    LOGGER.info("Will try to greet " + "world" + " ...");
    while (true) {
        try {
            try {
                HelloRequest1 helloRequest1 = new HelloRequest1();
                helloRequest1.setName("ab");
                HelloRequest2 helloRequest2 = new HelloRequest2();
                helloRequest2.setName("cd");
                HelloResponse result = helloService.hello2(helloRequest1, helloRequest2);
                LOGGER.info("Invoke Success,hello: {} ", result.getMessage());
            } catch (StatusRuntimeException e) {
                LOGGER.error("RPC failed: {}", e.getStatus());
            } catch (Throwable e) {
                LOGGER.error("Unexpected RPC call breaks", e);
            }
        } catch (Exception e) {
            LOGGER.error("Unexpected RPC call breaks", e);
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) StatusRuntimeException(io.grpc.StatusRuntimeException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) StatusRuntimeException(io.grpc.StatusRuntimeException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 95 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.

the class SofaRegistryTest method testSubTwice.

@Test
public void testSubTwice() throws Exception {
    int timeoutPerSub = 5000;
    consumer1 = new ConsumerConfig();
    consumer1.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    // 订阅
    CountDownLatch latch = new CountDownLatch(2);
    MockProviderInfoListener providerInfoListener = new MockProviderInfoListener();
    providerInfoListener.setCountDownLatch(latch);
    consumer1.setProviderInfoListener(providerInfoListener);
    registry.subscribe(consumer1);
    latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
    Map<String, ProviderGroup> ps = providerInfoListener.getData();
    Assert.assertTrue(ps.size() > 0);
    Assert.assertNotNull(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
    Assert.assertEquals(0, ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
    // 重复订阅
    consumer2 = new ConsumerConfig();
    consumer2.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    CountDownLatch latch2 = new CountDownLatch(1);
    MockProviderInfoListener providerInfoListener2 = new MockProviderInfoListener();
    providerInfoListener2.setCountDownLatch(latch2);
    consumer2.setProviderInfoListener(providerInfoListener2);
    registry.subscribe(consumer2);
    latch2.await(timeoutPerSub, TimeUnit.MILLISECONDS);
    Map<String, ProviderGroup> ps2 = providerInfoListener2.getData();
    Assert.assertTrue(ps2.size() > 0);
    Assert.assertNotNull(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
    Assert.assertEquals(0, ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
    Assert.assertEquals(1, registry.subscribers.size());
    Assert.assertEquals(1, registry.configurators.size());
    latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
    Assert.assertTrue(ps2.size() > 0);
    Assert.assertNotNull(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
    Assert.assertEquals(0, ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size());
    // 1个服务 订阅服务列表和服务配置 2个dataId
    Assert.assertEquals(1, registry.subscribers.size());
    Assert.assertEquals(1, registry.configurators.size());
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)113 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)77 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)64 Test (org.junit.Test)53 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)52 HelloService (com.alipay.sofa.rpc.test.HelloService)30 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)15 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)14 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)14 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)13 ArrayList (java.util.ArrayList)12 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)11 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)11 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 EchoService (com.alipay.sofa.rpc.test.EchoService)10 EchoRequest (com.alipay.sofa.rpc.server.bolt.pb.EchoRequest)8 DemoServiceImpl (com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl)7 Filter (com.alipay.sofa.rpc.filter.Filter)7