Search in sources :

Example 21 with ApplicationConfig

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

the class DubboClientMain method main.

public static void main(String[] args) throws InterruptedException {
    ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("dubbo-client");
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setApplication(applicationConfig).setInterfaceId(HelloService.class.getName()).setBootstrap("dubbo").setProtocol("dubbo").setUniqueId("xxx").setDirectUrl("dubbo://127.0.0.1:20080").setRegister(false).setTimeout(3000);
    HelloService helloService = consumerConfig.refer();
    ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>().setApplication(applicationConfig).setInterfaceId(EchoService.class.getName()).setBootstrap("dubbo").setUniqueId("xxx").setDirectUrl("dubbo://127.0.0.1:20080").setRegister(false).setTimeout(3000);
    EchoService echoService = consumerConfig2.refer();
    LOGGER.error("started at pid {}", RpcRuntimeContext.PID);
    try {
        for (int i = 0; i < 100; i++) {
            try {
                String s = helloService.sayHello("xxx", 22);
                LOGGER.info("{}", s);
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            }
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    }
    synchronized (DubboClientMain.class) {
        while (true) {
            DubboClientMain.class.wait();
        }
    }
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) EchoService(com.alipay.sofa.rpc.test.EchoService) HelloService(com.alipay.sofa.rpc.test.HelloService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 22 with ApplicationConfig

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

the class DubboServerMain method main.

public static void main(String[] args) {
    ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("dubbo-server");
    ServerConfig serverConfig = new ServerConfig().setProtocol("dubbo").setHost("127.0.0.1").setPort(20080).setSerialization("hessian2").setDaemon(false);
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setBootstrap("dubbo").setApplication(applicationConfig).setRef(new HelloServiceImpl()).setUniqueId("xxx").setServer(serverConfig).setRegister(false);
    ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setRef(new EchoServiceImpl()).setApplication(applicationConfig).setBootstrap("dubbo").setUniqueId("xxx").setServer(serverConfig).setRegister(false);
    providerConfig.export();
    providerConfig2.export();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    synchronized (DubboServerMain.class) {
        while (true) {
            try {
                DubboServerMain.class.wait();
            } catch (InterruptedException e) {
            }
        }
    }
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) 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 23 with ApplicationConfig

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

the class RestClientMultipleMain method main.

public static void main(String[] args) {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
    ConsumerConfig<RestService> consumerConfig = new ConsumerConfig<RestService>().setApplication(application).setInterfaceId(RestService.class.getName()).setProtocol("rest").setBootstrap("rest").setDirectUrl("rest://127.0.0.1:8888").setTimeout(3000);
    final RestService helloService = consumerConfig.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    final int threads = 50;
    final AtomicLong cnt = new AtomicLong(0);
    final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), // 无队列
    new NamedThreadFactory("client-"));
    for (int i = 0; i < threads; i++) {
        service1.execute(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        String s = helloService.get("1234567890");
                        cnt.incrementAndGet();
                    } catch (Exception e) {
                        LOGGER.error("", e);
                    }
                }
            }
        });
    }
    Thread thread = new Thread(new Runnable() {

        private long last = 0;

        @Override
        public void run() {
            while (true) {
                long count = cnt.get();
                long tps = count - last;
                LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
                last = count;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }, "Print-tps-THREAD");
    thread.start();
}
Also used : NamedThreadFactory(com.alipay.sofa.rpc.common.struct.NamedThreadFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RestService(com.alipay.sofa.rpc.rest.RestService)

Example 24 with ApplicationConfig

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

the class ProtobufServiceClientMultipleMain method main.

public static void main(String[] args) {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
    ConsumerConfig<ProtoService> consumerConfig = new ConsumerConfig<ProtoService>().setInterfaceId(// 指定接口
    ProtoService.class.getName()).setProtocol(// 指定协议
    "bolt").setApplication(application).setDirectUrl(// 指定直连地址
    "bolt://127.0.0.1:12200").setSerialization(// 指定序列化协议,默认为hessian
    "protobuf").setConnectTimeout(10 * 1000);
    final ProtoService helloService = consumerConfig.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    final int threads = 50;
    final AtomicLong cnt = new AtomicLong(0);
    final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), // 无队列
    new NamedThreadFactory("client-"));
    for (int i = 0; i < threads; i++) {
        service1.execute(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("12345678").build();
                        EchoResponse s = helloService.echoObj(request);
                        cnt.incrementAndGet();
                    } catch (Throwable e) {
                        LOGGER.error("", e);
                    }
                }
            }
        });
    }
    Thread thread = new Thread(new Runnable() {

        private long last = 0;

        @Override
        public void run() {
            while (true) {
                long count = cnt.get();
                long tps = count - last;
                LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
                last = count;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }, "Print-tps-THREAD");
    thread.start();
}
Also used : NamedThreadFactory(com.alipay.sofa.rpc.common.struct.NamedThreadFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 25 with ApplicationConfig

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

the class FutureClientMain method main.

public static void main(String[] args) throws InterruptedException {
    ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("future-client");
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setApplication(applicationConfig).setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setTimeout(50000).setDirectUrl("bolt://127.0.0.1:22222?appName=future-server");
    HelloService helloService = consumerConfig.refer();
    ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>().setApplication(applicationConfig).setInterfaceId(EchoService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setTimeout(50000).setDirectUrl("bolt://127.0.0.1:22222?appName=future-server");
    EchoService echoService = consumerConfig2.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    while (true) {
        try {
            String s1 = helloService.sayHello("xxx", 22);
            LOGGER.warn("must null :{}", s1);
            BoltResponseFuture future1 = (BoltResponseFuture) SofaResponseFuture.getFuture();
            String s2 = echoService.echoStr("yyy");
            LOGGER.warn("must null :{}", s2);
            BoltResponseFuture future2 = (BoltResponseFuture) SofaResponseFuture.getFuture();
            s1 = (String) future1.get();
            LOGGER.warn("get future1: {}, elapse: {}", s1, future1.getElapsedTime());
            s2 = (String) future2.get();
            LOGGER.warn("get future2: {}, elapse: {}", s2, future2.getElapsedTime());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        try {
            Thread.sleep(2000);
        } catch (Exception ignore) {
        }
    }
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) EchoService(com.alipay.sofa.rpc.test.EchoService) HelloService(com.alipay.sofa.rpc.test.HelloService) BoltResponseFuture(com.alipay.sofa.rpc.message.bolt.BoltResponseFuture)

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