The grpc gateway is a service that acts as an intermediary between the client and grpc services. It serves as a bridge for communication between the client and the grpc service, responsible for forwarding client-initiated requests to the grpc server and returning the grpc server's response to the client. The grpc gateway can implement functions such as protocol conversion, routing, load balancing, caching, and rate limiting, thereby improving the availability and performance of the service.
Dependencies
- Prepare a microservice user.
- Prepare a proto file edusys_gw.proto.
- Install the tool sponge.
After installing the "sponge" tool, execute the following command to open the UI interface:
sponge run
Quick Creation of a GRPC Gateway Project
Go to the UI interface of "sponge", click on the left menu bar 【Protobuf】 --> 【generate gateway project】, fill in some parameters to generate the code for the grpc gateway project.
The web framework used is gin, and it also includes swagger documentation, common service governance code, build and deployment scripts, etc.
Enable GRPC Gateway Service to Call Microservice API Interfaces
Generate Connection Code
In order for the grpc gateway service to connect to the grpc service, additional code for connecting to the grpc service needs to be generated. Click on the left menu bar 【Public】 --> 【generate grpc connection code】, fill in the parameters to generate the code, and then move the generated code for connecting to the grpc service to the grpc gateway project.
Configure Microservice Addresses
Open the configuration file configs/edusys_gw.yml
and set the grpc service addresses.
grpcClient:
- name: "user"
host: "127.0.0.1"
port: 8282
registryDiscoveryType: ""
enableLoadBalance: false
Copy the proto file of the grpc service
To be able to call the methods of the grpc service in the grpc gateway service, you need to copy the proto file of the grpc service to the directory api/edusys_gw/v1
in the grpc gateway service. In the grpc gateway directory, execute the following command:
# Note: This command is used to copy the proto file of the microservice created using sponge
make copy-proto SERVER=../../4_micro-grpc-protobuf
Run Service
Run Microservice
Switch to the microservice user, and run the microservice:
make run
Run grpc Gateway Service
Switch to the "edusys-grpc-gateway" directory, and execute the following commands:
# Generate code
make proto
# Open internal/service/user_gw.go and fill in specific logic code based on the example code provided
# Here is the specific logic code: https://github.com/zhufuyi/sponge_examples/blob/main/5_micro-gin-rpc-gateway/edusys-grpc-gateway/internal/service/edusys_gw.go
# Compile and start the service
make run
Open http://localhost:8080/apis/swagger/index.html in your browser to test the API interfaces.
This is the grpc gateway service code generated from the above steps https://github.com/zhufuyi/sponge_examples/tree/main/5_micro-gin-rpc-gateway
Click to view detailed grpce gateway service development documentation https://go-sponge.com/zh-cn/rpc-gateway-development-protobuf
Top comments (0)