Search in sources :

Example 41 with ConsumerConfig

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

the class CustomizeFilterTest method beforeClass.

@BeforeClass
public static void beforeClass() {
    customizeContainerRequestTestFilter = new CustomizeContainerRequestTestFilter();
    customizeContainerResponseTestFilter = new CustomizeContainerResponseTestFilter();
    customizeClientRequestTestFilter = new CustomizeClientRequestTestFilter();
    customizeClientResponseTestFilter = new CustomizeClientResponseTestFilter();
    JAXRSProviderManager.registerCustomProviderInstance(customizeContainerRequestTestFilter);
    JAXRSProviderManager.registerCustomProviderInstance(customizeContainerResponseTestFilter);
    JAXRSProviderManager.registerCustomProviderInstance(customizeClientRequestTestFilter);
    JAXRSProviderManager.registerCustomProviderInstance(customizeClientResponseTestFilter);
    providerFilter = new CustomizeTestFilter();
    List<Filter> providerFilters = new ArrayList<Filter>(2);
    providerFilters.add(providerFilter);
    ServerConfig restServer = new ServerConfig().setPort(8583).setProtocol(RpcConstants.PROTOCOL_TYPE_REST);
    List<ServerConfig> servers = new ArrayList<ServerConfig>(2);
    servers.add(restServer);
    providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setRegister(false).setServer(servers).setFilterRef(providerFilters);
    providerConfig.export();
    // rest服务
    clientFilter = new CustomizeTestFilter();
    List<Filter> clientFilters = new ArrayList<Filter>(2);
    clientFilters.add(clientFilter);
    ConsumerConfig<RestService> consumerConfigRest = new ConsumerConfig<RestService>().setInterfaceId(RestService.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setDirectUrl("rest://127.0.0.1:8583").setTimeout(1000).setFilterRef(clientFilters).setApplication(new ApplicationConfig().setAppName("TestClientRest"));
    filterRestService = consumerConfigRest.refer();
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ArrayList(java.util.ArrayList) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) BeforeClass(org.junit.BeforeClass)

Example 42 with ConsumerConfig

use of com.alipay.sofa.rpc.config.ConsumerConfig 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 43 with ConsumerConfig

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

the class AllTypeServiceTest method testAll.

@Test
public void testAll() {
    ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setDaemon(false);
    ProviderConfig<AllTypeService> CProvider = new ProviderConfig<AllTypeService>().setInterfaceId(AllTypeService.class.getName()).setRef(new AllTypeServiceImpl()).setServer(serverConfig2);
    CProvider.export();
    ConsumerConfig<AllTypeService> BConsumer = new ConsumerConfig<AllTypeService>().setInterfaceId(AllTypeService.class.getName()).setTimeout(50000).setSerialization("hessian").setDirectUrl("bolt://127.0.0.1:22222");
    AllTypeService helloService = BConsumer.refer();
    // 基本类型和数组
    helloService.echo();
    Assert.assertEquals("a1", helloService.echo2("a", 1));
    Assert.assertEquals(1, helloService.echoInt(1));
    Assert.assertEquals(AllTypeEnum.TB, helloService.echoEnum(AllTypeEnum.TB));
    Assert.assertEquals(AllTypeEnum2.TB, helloService.echoEnum2(AllTypeEnum2.TB));
    Assert.assertArrayEquals(new String[] { "11", "22" }, helloService.echoStringArray(new String[] { "11", "22" }));
    Assert.assertArrayEquals(new String[][] { { "aa", "bb" }, { "11", "22" } }, helloService.echoStringArray2(new String[][] { { "aa", "bb" }, { "11", "22" } }));
    // 集合
    List<String> list = new ArrayList<String>();
    list.add("11");
    list.add("22");
    Assert.assertEquals(list, helloService.echoList(list));
    list = new LinkedList<String>();
    list.add("11");
    list.add("22");
    Assert.assertEquals(list, helloService.echoList(list));
    list = new CopyOnWriteArrayList<String>();
    list.add("11");
    list.add("22");
    Assert.assertEquals(list, helloService.echoList(list));
    list = new LinkedList<String>();
    list.add("11");
    list.add("22");
    Assert.assertEquals(list, helloService.echoList(list));
    list = Arrays.asList("11", "22");
    Assert.assertEquals(list, helloService.echoList(list));
    MyList<String> myList = new MyList<String>();
    myList.setListName("xxx");
    myList.add("11");
    myList.add("22");
    MyList myList1 = helloService.echoMyList(myList);
    // Assert.assertEquals(myList.getListName(), myList1.getListName()); // TODO 目前不支持自定义LIST的字段
    // Assert.assertEquals(myList, myList1);
    Assert.assertEquals(myList.get(0), myList1.get(0));
    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("xx", 11);
    map.put("yy", 22);
    Assert.assertEquals(map, helloService.echoMap(map));
    map = new LinkedHashMap<String, Integer>();
    map.put("xx", 11);
    map.put("yy", 22);
    Assert.assertEquals(map, helloService.echoMap(map));
    map = new TreeMap<String, Integer>();
    map.put("xx", 11);
    map.put("yy", 22);
    Assert.assertEquals(map, helloService.echoMap(map));
    map = new ConcurrentHashMap<String, Integer>();
    map.put("xx", 11);
    map.put("yy", 22);
    Assert.assertEquals(map, helloService.echoMap(map));
    map = Collections.singletonMap("xx", 11);
    Assert.assertEquals(map, helloService.echoMap(map));
    MyMap myMap = new MyMap();
    myMap.setMapName("map");
    myMap.put("xx", 11);
    myMap.put("yy", 22);
    MyMap myMap1 = helloService.echoMyMap(myMap);
    // Assert.assertEquals(myMap.getMapName(), myMap1.getMapName()); // TODO 目前不支持自定义LIST的字段
    // Assert.assertEquals(myMap, myMap1);
    Assert.assertEquals(myMap.get("xx"), myMap1.get("xx"));
    Set<String> set = new HashSet<String>();
    set.add("11");
    set.add("22");
    Assert.assertEquals(set, helloService.echoSet(set));
    set = new TreeSet<String>();
    set.add("11");
    set.add("22");
    Assert.assertEquals(set, helloService.echoSet(set));
    set = new LinkedHashSet<String>();
    set.add("11");
    set.add("22");
    Assert.assertEquals(set, helloService.echoSet(set));
    set = new ConcurrentHashSet<String>();
    set.add("11");
    set.add("22");
    Assert.assertEquals(set, helloService.echoSet(set));
    MySet<String> mySet = new MySet<String>();
    mySet.setSetName("set");
    myList.add("11");
    myList.add("22");
    MySet mySet1 = helloService.echoMySet(mySet);
    // Assert.assertEquals(mySet.getSetName(), mySet1.getSetName()); // TODO 目前不支持自定义LIST的字段
    // Assert.assertEquals(mySet, mySet1);
    Assert.assertEquals(mySet.size(), mySet1.size());
    // 常用类型
    Date date = new Date();
    Assert.assertEquals(date, helloService.echoDate(date));
    Assert.assertEquals(new BigDecimal("6.6"), helloService.echoNum(new BigDecimal("2.2"), new BigInteger("3")));
    Assert.assertEquals(new BigInteger("3"), helloService.echoBigInteger(new BigInteger("3")));
    Assert.assertEquals(Currency.getInstance("CNY"), helloService.echoCurrency(Currency.getInstance("CNY")));
    // 自定义类型
    AllTypeObj obj = new AllTypeObj();
    AllTypeSubObj subObj = new AllTypeSubObj();
    Assert.assertEquals(obj, helloService.echoObj(obj));
    Assert.assertEquals(subObj, helloService.echoObj(subObj));
    Assert.assertEquals(obj, helloService.echoInterfaceObj(obj));
    Assert.assertEquals(subObj, helloService.echoInterfaceObj(subObj));
    Assert.assertEquals(obj, helloService.echoSubObj(obj));
    Assert.assertEquals(subObj, helloService.echoSubObj(subObj));
    {
        ConsumerConfig<AllTypeService> aConsumer = new ConsumerConfig<AllTypeService>().setInterfaceId(AllTypeService.class.getName()).setTimeout(50000).setSerialization("hessian").setRepeatedReferLimit(-1).setProxy("javassist").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setDirectUrl("bolt://127.0.0.1:22222");
        AllTypeService helloService2 = aConsumer.refer();
        Assert.assertEquals(0, helloService2.echoInt(1));
        boolean error = false;
        Integer v = null;
        try {
            v = (Integer) RpcInvokeContext.getContext().getFuture().get();
        } catch (Exception e) {
            error = true;
        }
        Assert.assertFalse(error);
        Assert.assertTrue(v == 1);
    }
    {
        ConsumerConfig<AllTypeService> aConsumer = new ConsumerConfig<AllTypeService>().setInterfaceId(AllTypeService.class.getName()).setTimeout(50000).setSerialization("hessian").setRepeatedReferLimit(-1).setProxy("jdk").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setDirectUrl("bolt://127.0.0.1:22222");
        AllTypeService helloService2 = aConsumer.refer();
        Assert.assertEquals(0, helloService2.echoInt(1));
        boolean error = false;
        Integer v = null;
        try {
            v = (Integer) RpcInvokeContext.getContext().getFuture().get();
        } catch (Exception e) {
            error = true;
        }
        Assert.assertFalse(error);
        Assert.assertTrue(v == 1);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) HashSet(java.util.HashSet) ConcurrentHashSet(com.alipay.sofa.rpc.common.struct.ConcurrentHashSet) LinkedHashSet(java.util.LinkedHashSet) Date(java.util.Date) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 44 with ConsumerConfig

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

the class MockTestRegistry method subscribe.

@Override
public List<ProviderGroup> subscribe(ConsumerConfig config) {
    String key = buildKey(config);
    Map<ConsumerConfig, ProviderInfoListener> listeners = notifyListeners.get(key);
    if (listeners == null) {
        listeners = new ConcurrentHashMap<ConsumerConfig, ProviderInfoListener>();
        Map<ConsumerConfig, ProviderInfoListener> old = notifyListeners.putIfAbsent(key, listeners);
        if (old != null) {
            listeners = old;
        }
    }
    final ProviderInfoListener listener = config.getProviderInfoListener();
    listeners.put(config, listener);
    ProviderGroup group = memoryCache.get(key);
    List<ProviderGroup> groups = new ArrayList<ProviderGroup>();
    if (group != null) {
        groups.add(group);
    }
    return doReturn(listener, groups);
}
Also used : ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) ArrayList(java.util.ArrayList) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup)

Example 45 with ConsumerConfig

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

the class NotFoundInvokerTest method testAll.

@Test
public void testAll() {
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setQueues(100).setCoreThreads(10).setMaxThreads(10);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1500)).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(30000).setFilterRef(Collections.<Filter>singletonList(new Filter() {

        @Override
        public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
            // 造一个假方法
            request.setMethodName(request.getMethodName() + "_unknown");
            return invoker.invoke(request);
        }
    })).setRegister(false);
    HelloService helloService = consumerConfig.refer();
    boolean ok = true;
    try {
        // 找不到方法
        helloService.sayHello("xxx", 22);
    } catch (Exception e) {
        if (e instanceof SofaRpcException) {
            Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        } else if (e instanceof UndeclaredThrowableException) {
            Assert.assertEquals(((SofaRpcException) e.getCause()).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        }
        ok = false;
    }
    Assert.assertFalse(ok);
    ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(30000).setRegister(false);
    EchoService echoService = consumerConfig2.refer();
    ok = true;
    try {
        echoService.echoStr("xx");
    } catch (Exception e) {
        if (e instanceof SofaRpcException) {
            Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        } else if (e instanceof UndeclaredThrowableException) {
            Assert.assertEquals(((SofaRpcException) e.getCause()).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        }
        ok = false;
    }
    Assert.assertFalse(ok);
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) EchoService(com.alipay.sofa.rpc.test.EchoService) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Aggregations

ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)139 Test (org.junit.Test)86 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)68 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)61 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)44 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)40 HelloService (com.alipay.sofa.rpc.test.HelloService)38 CountDownLatch (java.util.concurrent.CountDownLatch)27 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)21 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)21 ArrayList (java.util.ArrayList)19 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)18 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)14 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)12 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)12 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)10 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ConsumerBootstrap (com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap)8 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)8