/** Empty notify cause forbidden, non-empty notify cancels forbidden state */
  @Test
  public void testEmptyNotifyCauseForbidden() {
    RegistryDirectory registryDirectory = getRegistryDirectory();
    List invokers = null;

    List<URL> serviceUrls = new ArrayList<URL>();
    registryDirectory.notify(serviceUrls);

    RpcInvocation inv = new RpcInvocation();
    try {
      invokers = registryDirectory.list(inv);
    } catch (RpcException e) {
      Assert.assertEquals(RpcException.FORBIDDEN_EXCEPTION, e.getCode());
      Assert.assertEquals(false, registryDirectory.isAvailable());
    }

    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
    serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1,getXXX2,getXXX3"));

    registryDirectory.notify(serviceUrls);
    inv.setMethodName("getXXX2");
    invokers = registryDirectory.list(inv);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    Assert.assertEquals(2, invokers.size());
  }
  // 通知成3个invoker===================================
  private void test_Notified3invokers(RegistryDirectory registryDirectory) {
    List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
    serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1,getXXX2,getXXX3"));

    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());

    invocation = new RpcInvocation();

    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(3, invokers.size());

    invocation.setMethodName("getXXX");
    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(3, invokers.size());

    invocation.setMethodName("getXXX1");
    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(3, invokers.size());

    invocation.setMethodName("getXXX2");
    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());

    invocation.setMethodName("getXXX3");
    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());
  }
예제 #3
0
 public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
   System.out.println("MyDubboFilter");
   RpcInvocation inv = (RpcInvocation) invocation;
   inv.setAttachmentIfAbsent("test", "test value from RPC");
   Result result = invoker.invoke(invocation);
   System.out.println("result in MyDubboFilter: " + result);
   return result;
 }
  /** 测试mock provider下发 */
  @Test
  public void testNotify_MockProviderOnly() {
    RegistryDirectory registryDirectory = getRegistryDirectory();

    List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
    serviceUrls.add(SERVICEURL.setProtocol(Constants.MOCK_PROTOCOL));

    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    invocation = new RpcInvocation();

    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());

    RpcInvocation mockinvocation = new RpcInvocation();
    mockinvocation.setAttachment(Constants.INVOCATION_NEED_MOCK, "true");
    invokers = registryDirectory.list(mockinvocation);
    Assert.assertEquals(1, invokers.size());
  }
  // 测试调用和registry url的path无关
  @Test
  public void test_NotifiedDubbo1() {
    URL errorPathUrl =
        URL.valueOf("notsupport:/" + "xxx" + "?refer=" + URL.encode("interface=" + service));
    RegistryDirectory registryDirectory = getRegistryDirectory(errorPathUrl);
    List<URL> serviceUrls = new ArrayList<URL>();
    URL Dubbo1URL = URL.valueOf("dubbo://127.0.0.1:9098?lazy=true");
    serviceUrls.add(Dubbo1URL.addParameter("methods", "getXXX"));
    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());

    invocation = new RpcInvocation();

    List<Invoker<DemoService>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());

    invocation.setMethodName("getXXX");
    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());
    Assert.assertEquals(DemoService.class.getName(), invokers.get(0).getUrl().getPath());
  }
예제 #6
0
  public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    String generic = invoker.getUrl().getParameter(Constants.GENERIC_KEY);
    if (ProtocolUtils.isGeneric(generic)
        && !Constants.$INVOKE.equals(invocation.getMethodName())
        && invocation instanceof RpcInvocation) {
      RpcInvocation invocation2 = (RpcInvocation) invocation;
      String methodName = invocation2.getMethodName();
      Class<?>[] parameterTypes = invocation2.getParameterTypes();
      Object[] arguments = invocation2.getArguments();

      String[] types = new String[parameterTypes.length];
      for (int i = 0; i < parameterTypes.length; i++) {
        types[i] = ReflectUtils.getName(parameterTypes[i]);
      }
      Object[] args = PojoUtils.generalize(arguments);

      invocation2.setMethodName(Constants.$INVOKE);
      invocation2.setParameterTypes(GENERIC_PARAMETER_TYPES);
      invocation2.setArguments(new Object[] {methodName, types, args});
      Result result = invoker.invoke(invocation2);

      if (!result.hasException()) {
        Object value = result.getValue();
        try {
          Method method = invoker.getInterface().getMethod(methodName, parameterTypes);
          return new RpcResult(
              PojoUtils.realize(value, method.getReturnType(), method.getGenericReturnType()));
        } catch (NoSuchMethodException e) {
          throw new RpcException(e.getMessage(), e);
        }
      } else if (result.getException() instanceof GenericException) {
        GenericException exception = (GenericException) result.getException();
        try {
          String className = exception.getExceptionClass();
          Class<?> clazz = ReflectUtils.forName(className);
          Throwable targetException = null;
          Throwable lastException = null;
          try {
            targetException = (Throwable) clazz.newInstance();
          } catch (Throwable e) {
            lastException = e;
            for (Constructor<?> constructor : clazz.getConstructors()) {
              try {
                targetException =
                    (Throwable)
                        constructor.newInstance(new Object[constructor.getParameterTypes().length]);
                break;
              } catch (Throwable e1) {
                lastException = e1;
              }
            }
          }
          if (targetException != null) {
            try {
              Field field = Throwable.class.getDeclaredField("detailMessage");
              if (!field.isAccessible()) {
                field.setAccessible(true);
              }
              field.set(targetException, exception.getExceptionMessage());
            } catch (Throwable e) {
              logger.warn(e.getMessage(), e);
            }
            result = new RpcResult(targetException);
          } else if (lastException != null) {
            throw lastException;
          }
        } catch (Throwable e) {
          throw new RpcException(
              "Can not deserialize exception "
                  + exception.getExceptionClass()
                  + ", message: "
                  + exception.getExceptionMessage(),
              e);
        }
      }
      return result;
    }

    if (invocation.getMethodName().equals(Constants.$INVOKE)
        && invocation.getArguments() != null
        && invocation.getArguments().length == 3
        && ProtocolUtils.isGeneric(generic)) {

      if (ProtocolUtils.isJavaGenericSerialization(generic)) {
        Object[] args = (Object[]) invocation.getArguments()[2];

        for (Object arg : args) {
          if (!(byte[].class == arg.getClass())) {
            error(arg.getClass().getName());
          }
        }
      }

      ((RpcInvocation) invocation)
          .setAttachment(
              Constants.GENERIC_KEY, invoker.getUrl().getParameter(Constants.GENERIC_KEY));
    }
    return invoker.invoke(invocation);
  }
  @Test
  public void testParametersMerge() {
    RegistryDirectory registryDirectory = getRegistryDirectory();
    URL regurl =
        noMeaningUrl
            .addParameter("test", "reg")
            .addParameterAndEncoded(
                Constants.REFER_KEY,
                "key=query&" + Constants.LOADBALANCE_KEY + "=" + LeastActiveLoadBalance.NAME);
    RegistryDirectory<RegistryDirectoryTest> registryDirectory2 =
        new RegistryDirectory(RegistryDirectoryTest.class, regurl);
    registryDirectory2.setProtocol(protocol);

    List<URL> serviceUrls = new ArrayList<URL>();
    // 检验注册中心的参数需要被清除
    {
      serviceUrls.clear();
      serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
      registryDirectory.notify(serviceUrls);

      invocation = new RpcInvocation();
      List invokers = registryDirectory.list(invocation);

      Invoker invoker = (Invoker) invokers.get(0);
      URL url = invoker.getUrl();
      Assert.assertEquals(null, url.getParameter("key"));
    }
    // 检验服务提供方的参数需要merge
    {
      serviceUrls.clear();
      serviceUrls.add(
          SERVICEURL.addParameter("methods", "getXXX2").addParameter("key", "provider"));

      registryDirectory.notify(serviceUrls);
      invocation = new RpcInvocation();
      List invokers = registryDirectory.list(invocation);

      Invoker invoker = (Invoker) invokers.get(0);
      URL url = invoker.getUrl();
      Assert.assertEquals("provider", url.getParameter("key"));
    }
    // 检验服务query的参数需要与providermerge 。
    {
      serviceUrls.clear();
      serviceUrls.add(
          SERVICEURL.addParameter("methods", "getXXX3").addParameter("key", "provider"));

      registryDirectory2.notify(serviceUrls);
      invocation = new RpcInvocation();
      List invokers = registryDirectory2.list(invocation);

      Invoker invoker = (Invoker) invokers.get(0);
      URL url = invoker.getUrl();
      Assert.assertEquals("query", url.getParameter("key"));
    }

    {
      serviceUrls.clear();
      serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
      registryDirectory.notify(serviceUrls);

      invocation = new RpcInvocation();
      List invokers = registryDirectory.list(invocation);

      Invoker invoker = (Invoker) invokers.get(0);
      URL url = invoker.getUrl();
      Assert.assertEquals(false, url.getParameter(Constants.CHECK_KEY, false));
    }
    {
      serviceUrls.clear();
      serviceUrls.add(
          SERVICEURL.addParameter(Constants.LOADBALANCE_KEY, RoundRobinLoadBalance.NAME));
      registryDirectory2.notify(serviceUrls);

      invocation = new RpcInvocation();
      invocation.setMethodName("get");
      List invokers = registryDirectory2.list(invocation);

      Invoker invoker = (Invoker) invokers.get(0);
      URL url = invoker.getUrl();
      Assert.assertEquals(
          LeastActiveLoadBalance.NAME, url.getMethodParameter("get", Constants.LOADBALANCE_KEY));
    }
    // test geturl
    {
      Assert.assertEquals(null, registryDirectory2.getUrl().getParameter("mock"));
      serviceUrls.clear();
      serviceUrls.add(SERVICEURL.addParameter(Constants.MOCK_KEY, "true"));
      registryDirectory2.notify(serviceUrls);

      Assert.assertEquals("true", registryDirectory2.getUrl().getParameter("mock"));
    }
  }