Skip to Content
🎉 gRPCity 3.0 is released. Read more →

Config

gRPCity 暴露两个配置对象,分别对应它打交道的两层:

  • loadOptions —— .proto 文件的解析与编解码规则。在 loader.init() 时传入。
  • channelOptions —— 底层 gRPC channel 在网络层的行为。在 initClients() 与 initServer() 时传入。

两者都是 passthrough 形式:gRPCity 自带合理的默认值,并把你的覆盖叠加上去。

获取默认配置

import { defaultLoadOptions, defaultChannelOptions } from 'grpcity'

loadOptions

loadOptions 支持的字段:

字段名称有效值描述
keepCasetrue 或 false保留字段名。默认情况下将它们更改为驼峰命名法。
longsString 或 Number用于表示 long 值的类型。默认为 Long 对象类型。
enumsString用于表示 enum 值的类型。默认为数值类型。
bytesArray 或 String用于表示 bytes 值的类型。默认为 Buffer。
defaultstrue 或 false在输出对象上设置默认值。默认为 false。
arraystrue 或 false即使 defaults 为 false,对于缺失的数组值设置空数组。默认为 false。
objectstrue 或 false即使 defaults 为 false,对于缺失的对象值设置空对象。默认为 false。
oneofstrue 或 false对于当前字段,将虚拟的 oneof 属性设置为字段名。默认为 false。
jsontrue 或 false在 float 字段中将 Infinity 和 NaN 表示为字符串,并自动解码 google.protobuf.Any 值。默认为 false。
includeDirs字符串数组导入的 .proto 文件的搜索路径列表。

gRPCity 的默认值:

{ keepCase: true, longs: String, enums: String, defaults: false, oneofs: true }

channelOptions

channelOptions 原样透传给 @grpc/grpc-js。所有 channel 参数见 这里 ;grpc-js 当前实际支持的子集见 这里 。

gRPCity 的默认值面向典型微服务场景:较短的重连退避、保守的 keepalive、UNAVAILABLE 触发的基础重试策略:

{ // grpc库option只接收数值和string类型 // 配置grpc重连接时间(1s ~ 10s 随机数) 'grpc.min_reconnect_backoff_ms': 1000, 'grpc.max_reconnect_backoff_ms': 10000, // 通信超时 'grpc.grpclb_call_timeout_ms': 5000, // 下面3个参数解决长期空闲grpc客户端,不能及时发现tcp连接断开问题 // After waiting for a duration of this time, if the keepalive ping sender does not receive the ping ack, it will close the transport. 'grpc.keepalive_timeout_ms': (20 * 1000), // duration of this time the client/server pings its peer to see if the transport is still alive 'grpc.keepalive_time_ms': (120 * 1000), 'grpc.keepalive_permit_without_calls': 1, // 通信失败允许grpc库retry 'grpc.enable_retries': 1, // 配置retry参数 // 参考: https://github.com/grpc/proposal/blob/master/A6-client-retries.md#integration-with-service-config 'grpc.service_config': JSON.stringify({ retryPolicy: { // 1次正常发送+3次失败重试, 每次重试都有机会在不同于最初失败的子通道上发送 maxAttempts: 4, // 初次重试时间间隔: random(0, initialBackoff) 秒 // 第n次重试时间; random(0, min(initialBackoff*backoffMultiplier^(n-1), maxBackoff)) 秒 initialBackoff: '0.1s', maxBackoff: '1s', backoffMultiplier: 2, // 只有在网络不可达情况重试 retryableStatusCodes: ['UNAVAILABLE'] } }) }
Last updated on