Search in sources :

Example 1 with RestService

use of com.alipay.sofa.rpc.rest.RestService 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 2 with RestService

use of com.alipay.sofa.rpc.rest.RestService in project sofa-rpc by sofastack.

the class RestClientMain method main.

public static void main(String[] args) throws InterruptedException {
    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);
    RestService helloService = consumerConfig.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    while (true) {
        try {
            String s = helloService.add(22, "xxx");
            LOGGER.warn("add {}", s);
            s = helloService.query(22);
            LOGGER.warn("get {}", s);
            List<ExampleObj> es = new ArrayList<ExampleObj>();
            es.add(new ExampleObj().setName("xxx").setId(1));
            es.add(new ExampleObj().setName("yyy").setId(2));
            List<ExampleObj> rs = helloService.objects(es);
            LOGGER.warn("rs {}", rs.size());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
        }
    }
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ExampleObj(com.alipay.sofa.rpc.rest.ExampleObj) ArrayList(java.util.ArrayList) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) RestService(com.alipay.sofa.rpc.rest.RestService)

Example 3 with RestService

use of com.alipay.sofa.rpc.rest.RestService in project sofa-rpc by sofastack.

the class RestServerMain method main.

public static void main(String[] args) {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-server");
    /*
         访问地址:
         POST http://127.0.0.1:8888/rest/hello/code/name
         GET http://127.0.0.1:8888/rest/hello/code
         PUT http://127.0.0.1:8888/rest/hello/code/name
         DELETE http://127.0.0.1:8888/rest/hello/code
         GET http://127.0.0.1:8888/rest/get/1234567890
         POST http://127.0.0.1:8888/rest/post/1234567890 bodydddddd
         */
    ServerConfig serverConfig = new ServerConfig().setProtocol("rest").setPort(8888).setDaemon(false);
    ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setApplication(application).setRef(new RestServiceImpl()).setBootstrap("rest").setServer(serverConfig).setRegister(false);
    providerConfig.export();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) RestServiceImpl(com.alipay.sofa.rpc.rest.RestServiceImpl) RestService(com.alipay.sofa.rpc.rest.RestService)

Aggregations

ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)3 RestService (com.alipay.sofa.rpc.rest.RestService)3 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)2 NamedThreadFactory (com.alipay.sofa.rpc.common.struct.NamedThreadFactory)1 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1 ExampleObj (com.alipay.sofa.rpc.rest.ExampleObj)1 RestServiceImpl (com.alipay.sofa.rpc.rest.RestServiceImpl)1 ArrayList (java.util.ArrayList)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1