关于批量接口

1. 用ids取数据的接口返回Map

使用

public Map<Integer, User> getUserByIds(Collection<Integer> ids) {
    ...
}

而不是

public List<User> getUserByIds(Collection<Integer> ids) {
    ...
}

原因是后者会引入一些困惑 :
1>id可以重复吗?重复时会去重吗?
2>返回的List的顺序和ids相关吗?
3>某个id不存在, List里是跳过还是null?

2.按ids取数据的RPC接口需要校验长度

接口实现时应该做ids最大长度校验. 若超出则直接抛异常, 否则可能会因性能上的问题拖垮整个服务.

import BoundExceededException;
Map<Integer, User> getUserbyIds(List<Integer> ids) throws BoundExceededException {
    if (ids.size() > bound) {
        throw new BoundExceededException();
    }
   ...
}
上一篇:如何让html中的td文字只显示部分


下一篇:使用阿里kube-eventer监控pod状态