Java Code Examples for com.alipay.sofa.rpc.core.request.SofaRequest#setMethodArgs()

The following examples show how to use com.alipay.sofa.rpc.core.request.SofaRequest#setMethodArgs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComplexDataGenerator.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
public SofaRequest generateSofaRequest() {
    SofaRequest request = new SofaRequest();

    request.setTargetServiceUniqueName("liqiwei");
    request.setMethodName("sayHello");
    request.setTargetAppName("HelloService");

    request.setMethodArgSigs(new String[] { "com.lqw.testPerson", "int" });
    request.setMethodArgs(new Object[] { generateGenericPerson_6(), 1992 });
    request.setMethod(null);

    request.addRequestProps("trace", new HashMap<String, String>());

    return request;
}
 
Example 2
Source File: SofaRpcUtilsTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMethodArguments() {
    SofaRequest request = new SofaRequest();
    request.setMethodArgs(new Object[]{"Sentinel", 2020});
    Object[] arguments = SofaRpcUtils.getMethodArguments(request);
    assertEquals(arguments.length, 2);
    assertEquals("Sentinel", arguments[0]);
    assertEquals(2020, arguments[1]);
}
 
Example 3
Source File: ProtobufSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void decodeSofaRequest(AbstractByteBuf data, SofaRequest sofaRequest, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    // 解析request信息
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService != null) {
        sofaRequest.setTargetServiceUniqueName(targetService);
        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetService);
        sofaRequest.setInterfaceName(interfaceName);
    } else {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName != null) {
        sofaRequest.setMethodName(methodName);
    } else {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }
    String targetApp = head.remove(RemotingConstants.HEAD_TARGET_APP);
    if (targetApp != null) {
        sofaRequest.setTargetAppName(targetApp);
    }

    // 解析tracer等信息
    parseRequestHeader(RemotingConstants.RPC_TRACE_NAME, head, sofaRequest);
    if (RpcInvokeContext.isBaggageEnable()) {
        parseRequestHeader(RemotingConstants.RPC_REQUEST_BAGGAGE, head, sofaRequest);
    }
    for (Map.Entry<String, String> entry : head.entrySet()) {
        sofaRequest.addRequestProp(entry.getKey(), entry.getValue());
    }

    // 根据接口+方法名找到参数类型 此处要处理byte[]为空的吗
    Class requestClass = protobufHelper.getReqClass(targetService,
        sofaRequest.getMethodName());
    Object pbReq = decode(data, requestClass, head);
    sofaRequest.setMethodArgs(new Object[] { pbReq });
    sofaRequest.setMethodArgSigs(new String[] { requestClass.getName() });
}
 
Example 4
Source File: SofaRequestHessianSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaRequest template)
    throws SofaRpcException {
    try {
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
        Hessian2Input input = new Hessian2Input(inputStream);
        input.setSerializerFactory(serializerFactory);
        Object object = input.readObject();
        SofaRequest tmp = (SofaRequest) object;
        String targetServiceName = tmp.getTargetServiceUniqueName();
        if (targetServiceName == null) {
            throw buildDeserializeError("Target service name of request is null!");
        }
        // copy values to template
        template.setMethodName(tmp.getMethodName());
        template.setMethodArgSigs(tmp.getMethodArgSigs());
        template.setTargetServiceUniqueName(tmp.getTargetServiceUniqueName());
        template.setTargetAppName(tmp.getTargetAppName());
        template.addRequestProps(tmp.getRequestProps());

        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetServiceName);
        template.setInterfaceName(interfaceName);

        // decode args
        String[] sig = template.getMethodArgSigs();
        Class<?>[] classSig = ClassTypeUtils.getClasses(sig);
        final Object[] args = new Object[sig.length];
        for (int i = 0; i < template.getMethodArgSigs().length; ++i) {
            args[i] = input.readObject(classSig[i]);
        }
        template.setMethodArgs(args);
        input.close();
    } catch (IOException e) {
        throw buildDeserializeError(e.getMessage(), e);
    }
}
 
Example 5
Source File: SofaRequestHessianSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaRequest decodeObject(AbstractByteBuf data, Map<String, String> context) throws SofaRpcException {
    try {
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
        Hessian2Input input = new Hessian2Input(inputStream);
        input.setSerializerFactory(serializerFactory);
        Object object = input.readObject();
        SofaRequest sofaRequest = (SofaRequest) object;
        String targetServiceName = sofaRequest.getTargetServiceUniqueName();
        if (targetServiceName == null) {
            throw buildDeserializeError("Target service name of request is null!");
        }
        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetServiceName);
        sofaRequest.setInterfaceName(interfaceName);

        String[] sig = sofaRequest.getMethodArgSigs();
        Class<?>[] classSig = ClassTypeUtils.getClasses(sig);

        final Object[] args = new Object[sig.length];
        for (int i = 0; i < sofaRequest.getMethodArgSigs().length; ++i) {
            args[i] = input.readObject(classSig[i]);
        }
        sofaRequest.setMethodArgs(args);
        input.close();
        return sofaRequest;
    } catch (IOException e) {
        throw buildDeserializeError(e.getMessage(), e);
    }
}
 
Example 6
Source File: JacksonSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void decodeSofaRequest(AbstractByteBuf data, SofaRequest sofaRequest, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    // 解析request信息
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService != null) {
        sofaRequest.setTargetServiceUniqueName(targetService);
        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetService);
        sofaRequest.setInterfaceName(interfaceName);
    } else {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName != null) {
        sofaRequest.setMethodName(methodName);
    } else {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }
    String targetApp = head.remove(RemotingConstants.HEAD_TARGET_APP);
    if (targetApp != null) {
        sofaRequest.setTargetAppName(targetApp);
    }

    // parse tracer and baggage
    parseRequestHeader(RemotingConstants.RPC_TRACE_NAME, head, sofaRequest);
    if (RpcInvokeContext.isBaggageEnable()) {
        parseRequestHeader(RemotingConstants.RPC_REQUEST_BAGGAGE, head, sofaRequest);
    }
    for (Map.Entry<String, String> entry : head.entrySet()) {
        sofaRequest.addRequestProp(entry.getKey(), entry.getValue());
    }

    // according interface and method name to find parameter types
    JavaType[] requestClassList = jacksonHelper.getReqClass(targetService, sofaRequest.getMethodName());
    Object[] reqList = decode(data, requestClassList);
    sofaRequest.setMethodArgs(reqList);
    sofaRequest.setMethodArgSigs(parseArgSigs(requestClassList));
}
 
Example 7
Source File: ProtostuffSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void decodeSofaRequest(AbstractByteBuf data, SofaRequest sofaRequest, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    // 解析request信息
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService != null) {
        sofaRequest.setTargetServiceUniqueName(targetService);
        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetService);
        sofaRequest.setInterfaceName(interfaceName);
    } else {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName != null) {
        sofaRequest.setMethodName(methodName);
    } else {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }
    String targetApp = head.remove(RemotingConstants.HEAD_TARGET_APP);
    if (targetApp != null) {
        sofaRequest.setTargetAppName(targetApp);
    }

    // 解析tracer等信息
    parseRequestHeader(RemotingConstants.RPC_TRACE_NAME, head, sofaRequest);
    if (RpcInvokeContext.isBaggageEnable()) {
        parseRequestHeader(RemotingConstants.RPC_REQUEST_BAGGAGE, head, sofaRequest);
    }
    for (Map.Entry<String, String> entry : head.entrySet()) {
        sofaRequest.addRequestProp(entry.getKey(), entry.getValue());
    }

    // 根据接口+方法名找到参数类型 此处要处理byte[]为空的吗
    Class requestClass = protostuffHelper.getReqClass(targetService,
        sofaRequest.getMethodName());
    Object pbReq = decode(data, requestClass, head);
    sofaRequest.setMethodArgs(new Object[] { pbReq });
    sofaRequest.setMethodArgSigs(new String[] { requestClass.getName() });
}
 
Example 8
Source File: WeightConsistentHashLoadBalancerTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 测试根据权重数据的分布符合比例
 *
 * @throws Exception
 */
@Test
public void testWeight() throws Exception {
    WeightConsistentHashLoadBalancer loadBalancer = new WeightConsistentHashLoadBalancer(null);
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(ConsistentHashLoadBalancerTest.class.getName());
    request.setMethod(ConsistentHashLoadBalancerTest.class.getMethod("doSelect"));
    int size = 20;
    int total = 100000;
    List<ProviderInfo> providers = buildDiffWeightProviderList(size);
    Map<Integer, Integer> map = new HashMap(total * 2);
    for (int i = 0; i < total; i++) {
        request.setMethodArgs(new Object[] { "method" + i });
        ProviderInfo provider = loadBalancer.doSelect(request, providers);
        Integer key = provider.getPort();
        if (map.containsKey(key)) {
            int count = map.get(key);
            map.put(key, ++count);
        } else {
            map.put(key, 0);
        }
    }
    Set<Map.Entry<Integer, Integer>> set = map.entrySet();
    Iterator<Map.Entry<Integer, Integer>> iterator = set.iterator();

    while (iterator.hasNext()) {
        Map.Entry<Integer, Integer> entry = iterator.next();
        int port = entry.getKey() - 9000;
        //最大误差不超过10%
        Assert.assertTrue(entry.getValue() > 500 * port * 0.90);
        Assert.assertTrue(entry.getValue() < 500 * port * 1.10);
    }
}
 
Example 9
Source File: MessageBuilder.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 构建请求,常用于代理类拦截
 *
 * @param clazz    接口类
 * @param method   方法
 * @param argTypes 方法参数类型
 * @param args     方法参数值
 * @return 远程调用请求
 */
public static SofaRequest buildSofaRequest(Class<?> clazz, Method method, Class[] argTypes, Object[] args) {
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(clazz.getName());
    request.setMethodName(method.getName());
    request.setMethod(method);
    request.setMethodArgs(args == null ? CodecUtils.EMPTY_OBJECT_ARRAY : args);
    request.setMethodArgSigs(ClassTypeUtils.getTypeStrs(argTypes, true));
    return request;
}
 
Example 10
Source File: RouterChainTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void buildProviderChain() {
    ConsumerConfig config = new ConsumerConfig();
    config.setBootstrap("test");
    ArrayList<Router> list = new ArrayList<Router>();
    config.setRouter(Arrays.asList("testChainRouter0", "-testChainRouter8"));
    list.add(new TestChainRouter1());
    list.add(new TestChainRouter2());
    list.add(new TestChainRouter3());
    list.add(new TestChainRouter4());
    list.add(new ExcludeRouter("-testChainRouter5"));
    config.setRouterRef(list);

    ConsumerBootstrap consumerBootstrap = Bootstraps.from(config);
    RouterChain chain = RouterChain.buildConsumerChain(consumerBootstrap);

    // build test data
    SofaRequest request = new SofaRequest();
    request.setMethodArgs(new String[] { "xxx" });
    request.setInvokeType("sync");
    List<ProviderInfo> providerInfos = new ArrayList<ProviderInfo>();
    ProviderInfo providerInfo = new ProviderInfo();
    providerInfo.setHost("127.0.0.1");
    providerInfo.setPort(12200);
    providerInfos.add(providerInfo);

    chain.route(request, providerInfos);
    Assert.assertEquals("r0>r7>r2>r4",
        RpcInternalContext.getContext().getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
}
 
Example 11
Source File: ComplexTestMix.java    From sofa-hessian with Apache License 2.0 4 votes vote down vote up
@Test
public void testSofaRequest() throws IOException {
    SofaRequest request = dg.generateSofaRequest();

    // serialization uses GenericHessian
    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    Hessian2Output output = new Hessian2Output(byteArray);
    output.setSerializerFactory(new GenericSerializerFactory());
    output.writeObject(request);

    final Object[] args = request.getMethodArgs();
    if (args != null) {
        for (int i = 0; i < args.length; i++) {
            output.writeObject(args[i]);
        }
    }
    output.close();
    byteArray.close();

    // deserialization uses Hessian
    byte[] body = byteArray.toByteArray();
    ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
    Hessian2Input hin = new Hessian2Input(bin);

    hin.setSerializerFactory(new SerializerFactory());

    SofaRequest deRequest = (SofaRequest) hin.readObject();
    String[] sig = deRequest.getMethodArgSigs();
    Class<?>[] classSig = new Class[sig.length];

    final Object[] deArgs = new Object[sig.length];
    for (int i = 0; i < deRequest.getMethodArgSigs().length; ++i) {
        deArgs[i] = hin.readObject(classSig[i]);
    }
    deRequest.setMethodArgs(deArgs);

    bin.close();
    hin.close();

    assertEquals(request.getTargetServiceUniqueName(), deRequest.getTargetServiceUniqueName());
    assertEquals(request.getMethodName(), deRequest.getMethodName());
    assertEquals(request.getTargetAppName(), deRequest.getTargetAppName());
    assertEquals(request.getRequestProps(), deRequest.getRequestProps());

    // 1st argument is a Person, and 2nd argument is an int
    cmpGPersonEqualPerson((GenericObject) request.getMethodArgs()[0],
        (Person) deRequest.getMethodArgs()[0]);
    assertEquals(request.getMethodArgs()[1], deRequest.getMethodArgs()[1]);

}
 
Example 12
Source File: MsgPackSerializer.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
private void decodeSofaRequest(AbstractByteBuf data, SofaRequest sofaRequest, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    // 解析request信息
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService != null) {
        sofaRequest.setTargetServiceUniqueName(targetService);
        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetService);
        sofaRequest.setInterfaceName(interfaceName);
    } else {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName != null) {
        sofaRequest.setMethodName(methodName);
    } else {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }
    String targetApp = head.remove(RemotingConstants.HEAD_TARGET_APP);
    if (targetApp != null) {
        sofaRequest.setTargetAppName(targetApp);
    }

    // parse tracer and baggage
    parseRequestHeader(RemotingConstants.RPC_TRACE_NAME, head, sofaRequest);
    if (RpcInvokeContext.isBaggageEnable()) {
        parseRequestHeader(RemotingConstants.RPC_REQUEST_BAGGAGE, head, sofaRequest);
    }
    for (Map.Entry<String, String> entry : head.entrySet()) {
        sofaRequest.addRequestProp(entry.getKey(), entry.getValue());
    }

    // according interface and method name to find paramter types
    Class requestClass = helper.getReqClass(targetService,
            sofaRequest.getMethodName());

    Object pbReq = decode(data, requestClass, head);
    sofaRequest.setMethodArgs(new Object[]{pbReq});
    sofaRequest.setMethodArgSigs(new String[]{requestClass.getName()});
}
 
Example 13
Source File: MessageBuilder.java    From sofa-rpc with Apache License 2.0 3 votes vote down vote up
/**
 * 构建请求,常用于代理类拦截
 *
 * @param clazz    接口类
 * @param method   方法名
 * @param argTypes 方法参数类型
 * @param args     方法参数值
 * @return 远程调用请求
 * @deprecated use {@link #buildSofaRequest(Class, Method, Class[], Object[])}
 */
@Deprecated
public static SofaRequest buildSofaRequest(Class<?> clazz, String method, Class[] argTypes, Object[] args) {
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(clazz.getName());
    request.setMethodName(method);
    request.setMethodArgs(args == null ? CodecUtils.EMPTY_OBJECT_ARRAY : args);
    request.setMethodArgSigs(ClassTypeUtils.getTypeStrs(argTypes, true));
    return request;
}