Search in sources :

Example 1 with RestServiceImpl

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

the class RestDirectUrlTest method testAll.

@Test
public void testAll() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setBootstrap("rest").setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    final ConsumerConfig<RestService> consumerConfig = new ConsumerConfig<RestService>().setInterfaceId(RestService.class.getName()).setDirectUrl("rest://127.0.0.1:12300").setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setBootstrap("rest").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
    RestService restService = consumerConfig.refer();
    Assert.assertEquals(restService.query(11), "hello world !null");
    serverConfig.getServer().stop();
    // 关闭后再调用一个抛异常
    try {
        restService.query(11);
    } catch (Exception e) {
        // 应该抛出异常
        Assert.assertTrue(e instanceof SofaRpcException);
    }
    Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return CommonUtils.isEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
        }
    }, true, 50, 40));
    serverConfig.getServer().start();
    // 等待客户端重连服务端
    Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return CommonUtils.isNotEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
        }
    }, true, 50, 60));
    Assert.assertEquals(restService.query(11), "hello world !null");
}
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.server.rest.RestServiceImpl) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) RestService(com.alipay.sofa.rpc.server.rest.RestService) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Callable(java.util.concurrent.Callable) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 2 with RestServiceImpl

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

the class MultiProtocolServerExpTest method testMultiProtocolExp.

@Test
public void testMultiProtocolExp() throws NoSuchFieldException {
    try {
        Field field = DefaultProviderBootstrap.class.getDeclaredField("EXPORTED_KEYS");
        Map map = null;
        field.setAccessible(true);
        map = (ConcurrentMap<String, AtomicInteger>) field.get(null);
        map.clear();
    } catch (IllegalAccessException e1) {
        e1.printStackTrace();
    }
    try {
        // 只有2个线程 执行
        ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(1).setMaxThreads(2);
        // 发布一个服务,每个请求要执行1秒
        ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig).setRepeatedExportLimit(1).setRegister(false);
        providerConfig.export();
        ServerConfig serverConfig2 = new ServerConfig().setStopTimeout(0).setPort(22223).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setQueues(100).setCoreThreads(1).setMaxThreads(2);
        // 发布一个服务,每个请求要执行1秒
        ProviderConfig<RestService> providerConfig2 = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig2).setRepeatedExportLimit(1).setRegister(false);
        providerConfig2.export();
        ProviderConfig<RestService> providerConfig3 = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig2).setRepeatedExportLimit(1).setRegister(false);
        providerConfig3.export();
        Assert.fail();
    } catch (Throwable e) {
        // reflect to fetch export key
        ConcurrentMap<String, AtomicInteger> map = null;
        Field field = DefaultProviderBootstrap.class.getDeclaredField("EXPORTED_KEYS");
        try {
            field.setAccessible(true);
            map = (ConcurrentMap<String, AtomicInteger>) field.get(null);
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        }
        // two providers publish done, the third will false, and revert counter, export value is 1
        for (Map.Entry<String, AtomicInteger> entry : map.entrySet()) {
            AtomicInteger atomicInteger = entry.getValue();
            Assert.assertEquals(1, atomicInteger.get());
        }
    }
}
Also used : DefaultProviderBootstrap(com.alipay.sofa.rpc.bootstrap.DefaultProviderBootstrap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Field(java.lang.reflect.Field) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RestServiceImpl(com.alipay.sofa.rpc.server.rest.RestServiceImpl) RestService(com.alipay.sofa.rpc.server.rest.RestService) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 3 with RestServiceImpl

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

the class MultiProtocolServerTest method testMultiProtocol.

@Test
public void testMultiProtocol() {
    try {
        // 只有2个线程 执行
        ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(1).setMaxThreads(2);
        // 发布一个服务,每个请求要执行1秒
        ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig).setRepeatedExportLimit(1).setRegister(false);
        providerConfig.export();
        ServerConfig serverConfig2 = new ServerConfig().setStopTimeout(0).setPort(22223).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setQueues(100).setCoreThreads(1).setMaxThreads(2);
        // 发布一个服务,每个请求要执行1秒
        ProviderConfig<RestService> providerConfig2 = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig2).setRepeatedExportLimit(1).setRegister(false);
        providerConfig2.export();
    } catch (Throwable e) {
        Assert.fail();
    }
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) RestServiceImpl(com.alipay.sofa.rpc.server.rest.RestServiceImpl) RestService(com.alipay.sofa.rpc.server.rest.RestService) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest) Test(org.junit.Test)

Aggregations

ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)3 RestService (com.alipay.sofa.rpc.server.rest.RestService)3 RestServiceImpl (com.alipay.sofa.rpc.server.rest.RestServiceImpl)3 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)3 Test (org.junit.Test)3 DefaultProviderBootstrap (com.alipay.sofa.rpc.bootstrap.DefaultProviderBootstrap)1 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)1 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1