gRPC services¶
How-to guideThe steps for one task.
A gRPC service sits behind Apinx like any other backend: the call is HTTP/2 end to end, the API's policies (keys, JWT, rate limits, quotas, IP filters) apply to every call, and the trace shows the gRPC status of each. Unary and streaming calls pass, in both directions. What Apinx does not do, by choice: gRPC-Web, and JSON-to-protobuf transcoding; a gRPC client talks to a gRPC server.
The backend¶
A gRPC server is a target like the others, with its own scheme:
| Target | Means |
|---|---|
grpc://10.0.0.5:50051 |
HTTP/2 without TLS, by prior knowledge: how most gRPC servers inside a network run (Go's grpc.NewServer, a plain ServerBuilder in Java, insecure in Python) |
grpcs://orders.internal:443 |
HTTP/2 over TLS (the same as https://), with the upstream's tls settings for a private CA |
upstreams:
- name: orders-grpc
targets:
- url: grpc://10.0.0.5:50051
timeouts:
total: 0s # streams live as long as they live
An http:// target would be reached over HTTP/1.1, which a gRPC server refuses; grpc:// is
what makes the connection HTTP/2. One upstream may mix the schemes.
The API¶
A gRPC client sends POST /package.Service/Method, and cannot add a prefix. So the API's base
path is / (every service of the backend), or the service name itself
(/helloworld.Greeter), and prefix stripping is off: the backend must receive the path as
the client sent it.
apis:
- name: greeter
base_path: /helloworld.Greeter
upstream: orders-grpc
strip_prefix: false
policies:
- type: api-key
routes:
- name: greeter-methods
api: greeter
path: /helloworld.Greeter/{method} # the full path in a gateway file
methods: [POST]
upstream: orders-grpc
strip_prefix: false
In the console: New API with the base path, prefix stripping off, and one operation
POST /{method} (or keep the catch-all operation the console creates).
Policies apply as to any request: an API key or a JWT travels as gRPC metadata
(x-api-key: …, authorization: Bearer … on the call), rate limits and quotas count the
calls, IP filters see the caller. Body policies (jq transforms, request validation, threat
protection) look at the content type and leave application/grpc alone.
The listener¶
A gRPC client needs HTTP/2 on the listener:
- an
httpslistener negotiates it through ALPN; - an
httplistener needsh2c: true(cleartext HTTP/2, prior knowledge).
Behind nginx, nginx must speak HTTP/2 to Apinx too (grpc_pass): the
Linux deployment shows the block.
In Apinx Cloud¶
Every organization, and every environment of it, answers at a host of its own as well as under its path segment, because a
gRPC client cannot add the /{org} segment: point it at {org}.apinx.io:443 (TLS; the
console's Admin › Gateway page shows the exact name). REST calls work at that host too. Every
plan has it, the Free plan included; the Free plan's target ports (80, 443, 8080, 8443) apply
to gRPC backends like any other, so a server on 50051 needs Enterprise or a listener on one of
those ports.
WebSocket sessions are held to the plan as well: on the Free plan a session lasts an hour at
most and ten may be open at once, on Enterprise a day and five hundred. A route whose
max_duration is 0s gets the plan's limit; one asking for more is refused at deploy
(422 APX-SAAS-0051); the handshake past the count gets 429 APX-RT-0008. Admin › Gateway
and the route editor show the numbers.
What the trace shows¶
A failed gRPC call is HTTP 200 on the wire with the status in the trailers. The request
inspector shows the status by name (gRPC NOT_FOUND (5)), the traffic list marks it, the access
log carries grpc_status, and analytics and metrics count it as the HTTP status its code maps
to (NOT_FOUND as a 404, UNAVAILABLE as a 503), so a failing service is seen to fail.
Health checks and timeouts¶
Active health checks on a grpc:// or grpcs:// target use the gRPC health protocol instead
of a GET: on the upstream's schedule the gateway calls grpc.health.v1.Health/Check for the
server as a whole (under the target's base path, if it has one) and takes SERVING as
healthy, NOT_SERVING as not. A server without the health service answers UNIMPLEMENTED,
which proves a gRPC server is listening: healthy, said once in the log. The check path and
expected_statuses do not apply to such a target; the interval, timeout and thresholds do.
Most gRPC servers add the health service in one line (grpc/health in Go,
grpc.health.v1 in Java, grpc_health in Python). Passive health (ejecting a target after
failed calls) works as usual.
For long streams set the route's or the upstream's total timeout to 0s; a proxy in front
(nginx, Cloudflare) has its own idle limits.