On March 17, 2018, NGINIX officially announced support for gRPC in Nginx 1.13.10. This announcement indicates that Nginx has completed native support for gRPC. As we all know, gRPC has been the new generation of microservices de facto standard RPC framework. For implementation, you can use the service framework and other means to achieve load balancing, but there is no very mature reverse proxy software for gRPC in the industry.
NGINIX supports gRPC as an old load balancing software. It can already broker TCP connections to gRPC. With the new version, it can also terminate, check and trace gRPC method calls:
- Publish gRPC services, then use NGINX to apply HTTP/2 TLS encryption, rate limiting, IP-based access control lists, and logging;
- Publish multiple gRPC services through a single endpoint, using NGINX to check and track calls to each internal service;
- Use Round Robin, Least Connections or other methods to allocate calls in the cluster to balance the load of the gRPC service cluster.
Use NGINX to proxy gRPC service
Inserting NGINX between client and server applications provides a stable and reliable gateway for server applications.
Use Docker container to build NGINX Server
Build a server using Nginx docker image:
$docker pull nginx:1.13.10 1.13.10: Pulling from library/nginx 2a72cBF407d6: pull complete fefa2faca81f: Pull complete 080aeede8114: Pull complete Digest: Sha256: c4ee0ecb376636258447e1d8effb56c09c75fe7acf756bf7c13efadf38aa0aca Status: Downloaded newer image for nginx: 1.13.10Copy the code
Nginx gRPC configuration
Nginx uses an HTTP server to listen for gRPC traffic and uses the GRpc_pass instruction to proxy traffic. Create the following proxy configuration for Nginx to listen for unencrypted gRPC traffic on port 80 and forward requests to the server on port ‘31320’ :
Grpc_proxy. Conf file:
Upstream grpcServers {server 10.15.160.1:31320; # Skywalking OAP backend 11800 address} server {listen 80 http2; location / { grpc_pass grpc://grpcservers; error_page 502 = /error502grpc; } location = /error502grpc { internal; default_type application/grpc; add_header grpc-status 14; add_header grpc-message "unavailable"; return 204; }}Copy the code
Start the Nginx container
Docker run - name mynginx4grpc - p 80-80 - v tmp4myworkspace: / etc/nginx/conf. D: ro - d nginx: 1.17Copy the code
The above Nginx configuration file is located in the tmp4myworkspace directory.
reference
- Module ngx_http_grpc_module
- Service Mesh: NGINX+gRPC
The article was reprinted from the official account “Xiaoke Coding Notes”
This article is published by OpenWrite!