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

The following examples show how to use com.alipay.sofa.rpc.core.request.SofaRequest#setInterfaceName() . 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: SofaRpcUtilsTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetInterfaceResourceName() {
    SofaRequest request = new SofaRequest();
    request.setInterfaceName("com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService");
    String interfaceResourceName = SofaRpcUtils.getInterfaceResourceName(request);
    assertEquals("com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService", interfaceResourceName);
}
 
Example 2
Source File: SofaRpcUtilsTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMethodResourceName() {
    SofaRequest request = new SofaRequest();
    request.setInterfaceName("com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService");
    request.setMethodName("sayHello");
    request.setMethodArgSigs(new String[]{"java.lang.String", "int"});
    String methodResourceName = SofaRpcUtils.getMethodResourceName(request);
    assertEquals("com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService#sayHello(java.lang.String,int)", methodResourceName);
}
 
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 doSelect() throws Exception {

    WeightConsistentHashLoadBalancer loadBalancer = new WeightConsistentHashLoadBalancer(null);

    Map<Integer, Integer> cnt = new HashMap<Integer, Integer>(40);
    int size = 20;
    int total = 100000;
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(ConsistentHashLoadBalancerTest.class.getName());
    request.setMethod(ConsistentHashLoadBalancerTest.class.getMethod("doSelect"));
    for (int i = 0; i < size; i++) {
        cnt.put(9000 + i, 0);
    }
    List<ProviderInfo> providers = buildDiffWeightProviderList(size);
    long start = System.currentTimeMillis();
    for (int i = 0; i < total; i++) {
        ProviderInfo provider = loadBalancer.doSelect(request, providers);
        int port = provider.getPort();
        cnt.put(port, cnt.get(port) + 1);
    }
    long end = System.currentTimeMillis();
    System.out.println("elapsed" + (end - start) + "ms");
    System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");

    int count = 0;
    for (int i = 0; i < size; i++) {
        if (cnt.get(9000 + i) > 0) {
            count++;
        }
    }
    // 应该落在同一台机器上
    Assert.assertTrue(count == 1);

}
 
Example 9
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 10
Source File: ConsistentHashLoadBalancerTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void doSelect() throws Exception {

    ConsistentHashLoadBalancer loadBalancer = new ConsistentHashLoadBalancer(null);

    Map<Integer, Integer> cnt = new HashMap<Integer, Integer>();
    int size = 20;
    int total = 100000;
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(ConsistentHashLoadBalancerTest.class.getName());
    request.setMethod(ConsistentHashLoadBalancerTest.class.getMethod("doSelect"));
    for (int i = 0; i < size; i++) {
        cnt.put(9000 + i, 0);
    }
    List<ProviderInfo> providers = buildSameWeightProviderList(size);
    long start = System.currentTimeMillis();
    for (int i = 0; i < total; i++) {
        ProviderInfo provider = loadBalancer.doSelect(request, providers);
        int port = provider.getPort();
        cnt.put(port, cnt.get(port) + 1);
    }
    long end = System.currentTimeMillis();
    LOGGER.info("elapsed" + (end - start) + "ms");
    LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

    int count = 0;
    for (int i = 0; i < size; i++) {
        if (cnt.get(9000 + i) > 0) {
            count++;
        }
    }
    Assert.assertTrue(count == 1);// 应该落在一台机器上

}
 
Example 11
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 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;
}