Protocol support
gRPC
Available since
- HAProxy 2.0
- HAProxy Enterprise 2.0r1
- HAProxy ALOHA 11.5
gRPC is a remote procedure call framework that allows a client application to invoke an API function on a server as if that function were defined in the client’s own code. It uses Protocol Buffers to serialize messages, which allows clients and servers to exchange messages even when the two are written using different programming languages. gRPC offers bidirectional streaming of messages, meaning that an interaction between the client and server can be initiated from either side and once a connection is established, calls can be streamed continuously, similar to the WebSocket protocol.
The load balancer can act as a Layer 7 proxy for gRPC traffic because gRPC uses HTTP/2 as its transport protocol. This gives you access to fetch methods for inspecting the messages as they pass through the load balancer.
Enable gRPC over HTTP/2 Jump to heading
-
To enable HTTP/2 between clients and the load balancer, configure the
bindline in afrontendsection as ansslendpoint. Theprotoparameter announces that the load balancer supports HTTP/2 (h2):haproxyfrontend grpc_servicemode httpbind :3001 proto h2default_backend grpc_servershaproxyfrontend grpc_servicemode httpbind :3001 proto h2default_backend grpc_servers -
Configure TLS connections to the servers in the
backendsection and specify the HTTP/2 protocol:haproxybackend grpc_serversmode httpbalance roundrobinserver s1 192.168.0.10:3000 check proto h2server s2 192.168.0.10:3000 check proto h2haproxybackend grpc_serversmode httpbalance roundrobinserver s1 192.168.0.10:3000 check proto h2server s2 192.168.0.10:3000 check proto h2
The ungrpc converter Jump to heading
gRPC uses Protocol Buffers to serialize messages. Use the ungrpc converter function to extract information from a gRPC Protocol Buffers message. It takes the ID of the Protocol Buffers field in a message. In the following example, the Protocol Buffers message has a field named numberOfDice that has an ID of 1:
textmessage RollDiceRequest {int32 numberOfDice = 1;}
textmessage RollDiceRequest {int32 numberOfDice = 1;}
In your configuration, specify this ID and the data type to extract the value. Here, we print the numberOfDice value to the end of the access log:
haproxyfrontend grpc_servicemode httpbind :3001 proto h2default_backend grpc_servers# Store the numberOfDice value in the sess.numberofdice variablehttp-request set-var(sess.numberofdice) req.body,ungrpc(1,int32)# add the variable to the end of the standard HTTP log formatlog-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r Number of dice: %[var(sess.numberofdice)]"
haproxyfrontend grpc_servicemode httpbind :3001 proto h2default_backend grpc_servers# Store the numberOfDice value in the sess.numberofdice variablehttp-request set-var(sess.numberofdice) req.body,ungrpc(1,int32)# add the variable to the end of the standard HTTP log formatlog-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r Number of dice: %[var(sess.numberofdice)]"
Your access log will now include the field’s value:
outputtext127.0.0.1:51780 [18/Dec/2020:18:08:21.722] grpc_service grpc_servers/s1 0/0/0/1/+1 200 +99 - - ---- 1/1/1/0/0 0/0 "POST http:localhost:3001/RollDiceService/RollDice HTTP/2.0" Number of dice: 2127.0.0.1:51780 [18/Dec/2020:18:08:23.725] grpc_service grpc_servers/s1 0/0/0/1/+1 200 +99 - - ---- 1/1/1/0/0 0/0 "POST http:localhost:3001/RollDiceService/RollDice HTTP/2.0" Number of dice: 2
outputtext127.0.0.1:51780 [18/Dec/2020:18:08:21.722] grpc_service grpc_servers/s1 0/0/0/1/+1 200 +99 - - ---- 1/1/1/0/0 0/0 "POST http:localhost:3001/RollDiceService/RollDice HTTP/2.0" Number of dice: 2127.0.0.1:51780 [18/Dec/2020:18:08:23.725] grpc_service grpc_servers/s1 0/0/0/1/+1 200 +99 - - ---- 1/1/1/0/0 0/0 "POST http:localhost:3001/RollDiceService/RollDice HTTP/2.0" Number of dice: 2
See also Jump to heading
- To force a protocol on the
binddirective, see bind - proto. - To force a protocol on the
serverdirective, see server - proto. - To fetch the request body, see req.body.
- To convert the protocol buffers message field from a gRPC message, see ungrpc.