Client.invoke(url, request [,params])
对给定方法发起一个一元 RPC 请求。
要调用的给定方法必须事先通过 Client.load() 函数加载其 RPC 模式,否则将抛出错误。
在调用请求之前,必须先调用 Client.connect(),否则将抛出错误。
参数 | 类型 | 描述 |
---|---|---|
url | string | 要调用的 gRPC 方法 URL,格式为 /package.Service/Method ,例如 /google.cloud.language.v1.LanguageService/AnalyzeSentiment 。开头的斜杠 / 是可选的。 |
request | object | 规范请求对象,如 Protobuf JSON 映射所示。 |
params (可选) | object | 包含附加请求参数的 Params 对象。 |
返回值
类型 | 描述 |
---|---|
Response | gRPC Response 对象。 |
示例
import grpc from 'k6/net/grpc';
import { check } from 'k6';
const client = new grpc.Client();
client.load([], 'routeguide.proto');
export default () => {
client.connect('localhost:10000', { plaintext: true });
const response = client.invoke('main.RouteGuide/GetFeature', {
latitude: 410248224,
longitude: -747127767,
});
check(response, { 'status is OK': (r) => r && r.status === grpc.StatusOK });
console.log(response.message.name);
// output: 3 Hasta Way, Newton, NJ 07860, USA
client.close();
};