grpc(二)记一次grpc debug--io.grpc.StatusRuntimeException: UNKNOWN

1、起初是dingding一直报错:

instance:服务器名
err:GrpcClient#placeOrder: io.grpc.StatusRuntimeException: UNKNOWN

2、定位错误位置(找到问题,复现问题

  上面标红的代码是调用dingding,所以可以确定是调用grpc时,grpc内部报错,所以返回status message为 UNKNOWN

public Object placeOrder(Integer uid, 其他参数) {

        PlaceOrderRequest request = PlaceOrderRequest.newBuilder().setUserid(uid).build();
        GrpcReply response;

        try {
            response = tradeBlockingStub.placeOrder(request);
            UtilFunctions.log.info("GrpcClient#placeOrder rusult: code:{}, data:{}", 
                    response.getCode(), response.getData());
            return response;
        } catch (StatusRuntimeException e) {
            UtilFunctions.log.error("GrpcClient#placeOrder: msg:{}, exception:{}", e.toString(), e);
            UtilFunctions.reportError("GrpcClient#placeOrder: " + e.toString(), e);
            return null;
        }
    }

  

  查看linux上的日志,发现controller接收的数据price为NaN。

  所以,我在本地给price参数传NaN进行测试,果然出现同样的错误。grpc报错,原因是给double类型的参数传了NaN,打印信息:

四月 10, 2019 1:20:48 下午 io.grpc.internal.SerializingExecutor run
严重: Exception while executing runnable io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$1HalfClosed@2d37b2d0
java.lang.NumberFormatException

  

 3、解决

  在controller对Double类型的数据进行判断。

Double obj;
obj.isNaN();

4、测试前台传NaN数据

  4.1、新建一个springboot项目(版本2.1.3)

  4.2、application.properties

server.port=8090
server.servlet.context-path=/

  4.3、IndexController.java

@RestController
public class IndexController {

    @RequestMapping("/index")
    public Object demo1(Double price, Integer num) {
        // 前台给price传NaN是可以的
        // ==== price: NaN ====
        if (price.isNaN()) {
            System.out.println("---- price.isNaN ----");
        }
        System.out.println("==== price: " + price + " ====");
        System.out.println(price.getClass().getName());

        System.out.println("==== num: " + num + " ====");
        System.out.println(num.getClass().getName());

        Map<String, Object> result = new HashMap<>();
        result.put("price", price);
        result.put("num", num);

        return result;
    }
}

  4.4、使用firefox测试

猜你喜欢

转载自www.cnblogs.com/xy-ouyang/p/10682926.html
今日推荐