Search in sources :

Example 66 with ApplicationConfig

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

the class DubooServerTest method testFuture.

@Test
public // future调用,从future中取值.
void testFuture() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2);
    // 发布一个服务,每个请求要执行1秒
    ApplicationConfig serverApplacation = new ApplicationConfig();
    serverApplacation.setAppName("server");
    providerConfig = new ProviderConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setRef(new DemoServiceImpl()).setServer(serverConfig).setBootstrap("dubbo").setRegister(false).setApplication(serverApplacation);
    providerConfig.export();
    ApplicationConfig clientApplication = new ApplicationConfig();
    clientApplication.setAppName("client");
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    MethodConfig methodConfig = new MethodConfig();
    methodConfig.setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
    methodConfig.setName("sayHello");
    consumerConfig = new ConsumerConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setDirectUrl("dubbo://127.0.0.1:20880").setTimeout(30000).setRegister(false).setProtocol("dubbo").setBootstrap("dubbo").setApplication(clientApplication).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setMethods(methodConfigs);
    final DemoService demoService = consumerConfig.refer();
    String result = demoService.sayHello("xxx");
    Assert.assertEquals(null, result);
    Future<Object> future = RpcContext.getContext().getFuture();
    String futureResult = null;
    try {
        futureResult = (String) future.get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    Assert.assertEquals("Hello xxx", futureResult);
}
Also used : ArrayList(java.util.ArrayList) DemoService(com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoService) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ExecutionException(java.util.concurrent.ExecutionException) DemoServiceImpl(com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl) Test(org.junit.Test)

Example 67 with ApplicationConfig

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

the class DubooServerTest method testSync.

@Test
public // 同步调用,直连
void testSync() {
    try {
        // 只有1个线程 执行
        ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2);
        // 发布一个服务,每个请求要执行1秒
        ApplicationConfig serverApplacation = new ApplicationConfig();
        serverApplacation.setAppName("server");
        providerConfig = new ProviderConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setRef(new DemoServiceImpl()).setBootstrap("dubbo").setServer(serverConfig).setRegister(false).setApplication(serverApplacation);
        providerConfig.export();
        ApplicationConfig clientApplication = new ApplicationConfig();
        clientApplication.setAppName("client");
        consumerConfig = new ConsumerConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setDirectUrl("dubbo://127.0.0.1:20880").setBootstrap("dubbo").setTimeout(30000).setRegister(false).setProtocol("dubbo").setApplication(clientApplication);
        final DemoService demoService = consumerConfig.refer();
        String result = demoService.sayHello("xxx");
        Assert.assertTrue(result.equalsIgnoreCase("hello xxx"));
    } catch (Exception e) {
        e.printStackTrace();
        Assert.assertTrue(false);
    }
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) DemoService(com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ExecutionException(java.util.concurrent.ExecutionException) DemoServiceImpl(com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl) Test(org.junit.Test)

Example 68 with ApplicationConfig

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

the class DubooServerTest method testConsumerWithNoDubboServiceVersion.

@Test(expected = com.alibaba.dubbo.rpc.RpcException.class)
public // 同步调用,直连,dubbo 消费没有指定dubbo服务版本version
void testConsumerWithNoDubboServiceVersion() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2);
    // 发布一个服务,每个请求要执行1秒
    ApplicationConfig serverApplacation = new ApplicationConfig();
    serverApplacation.setAppName("server");
    providerConfig = new ProviderConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setRef(new DemoServiceImpl()).setBootstrap("dubbo").setServer(serverConfig).setParameter("version", "1.0.1").setRegister(false).setApplication(serverApplacation);
    providerConfig.export();
    ApplicationConfig clientApplication = new ApplicationConfig();
    clientApplication.setAppName("client");
    consumerConfig = new ConsumerConfig<DemoService>().setInterfaceId(DemoService.class.getName()).setDirectUrl("dubbo://127.0.0.1:20880").setBootstrap("dubbo").setTimeout(30000).setRegister(false).setProtocol("dubbo").setApplication(clientApplication);
    final DemoService demoService = consumerConfig.refer();
    String result = demoService.sayHello("xxx");
    Assert.assertTrue(result.equalsIgnoreCase("hello xxx"));
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) DemoService(com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) DemoServiceImpl(com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl) Test(org.junit.Test)

Example 69 with ApplicationConfig

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

the class ClientA method main.

public static void main(String[] args) {
    // A 服务
    ConsumerConfig<ServiceB> consumerConfig = new ConsumerConfig<ServiceB>().setApplication(new ApplicationConfig().setAppName("AAA")).setInterfaceId(ServiceB.class.getName()).setDirectUrl("bolt://127.0.0.1:12298?appName=BBB").setRegister(false).setTimeout(3000);
    ServiceB serviceB = consumerConfig.refer();
    while (true) {
        try {
            int ret0 = serviceB.getInt(999);
            LOGGER.info("ret0:" + ret0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
        }
    }
}
Also used : ServiceB(com.alipay.sofa.rpc.asynchain.ServiceB) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig)

Example 70 with ApplicationConfig

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

the class BoltClientMain 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:22000").setRegister(false).setTimeout(3000);
    HelloService helloService = consumerConfig.refer();
    ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setApplication(application).setDirectUrl("bolt://127.0.0.1:22000").setRegister(false).setTimeout(3000);
    EchoService echoService = consumerConfig2.refer();
    consumerConfig2.unRefer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    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) EchoService(com.alipay.sofa.rpc.test.EchoService) HelloService(com.alipay.sofa.rpc.test.HelloService)

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