Example 5, Automatically generate grpc gateway service project code, easy to achieve cross-service grpc calls
Tags: go, gin, grpc, microservices
Published: 2023-11-07 06:20:16

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](https://github.com/zhufuyi/sponge_examples/tree/main/4_micro-grpc-protobuf).
- Prepare a proto file [edusys_gw.proto](https://github.com/zhufuyi/sponge_examples/blob/main/5_micro-gin-rpc-gateway/edusys-grpc-gateway/api/edusys_gw/v1/edusys_gw.proto).
- Install the tool [sponge](https://github.com/zhufuyi/sponge/blob/main/assets/install-en.md).

After installing the "sponge" tool, execute the following command to open the UI interface:

```bash
sponge run
```

<br>

### 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hc1vi3330r8a9ve77exf.png)

The web framework used is [gin](https://github.com/gin-gonic/gin), and it also includes swagger documentation, common service governance code, build and deployment scripts, etc.

<br>

### 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wfx00eugmfpxesnwmbk4.png)

<br>

#### Configure Microservice Addresses

Open the configuration file `configs/edusys_gw.yml` and set the grpc service addresses.

```yaml  
grpcClient:  
  - name: "user"
    host: "127.0.0.1"
    port: 8282
    registryDiscoveryType: "" 
    enableLoadBalance: false
```

<br>

#### 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:

```bash
# Note: This command is used to copy the proto file of the microservice created using sponge
make copy-proto SERVER=../../4_micro-grpc-protobuf
```

<br>

### Run Service

#### Run Microservice

Switch to the microservice [user](https://github.com/zhufuyi/sponge_examples/tree/main/4_micro-grpc-protobuf), and run the microservice:

```bash  
make run
```

<br>

#### Run grpc Gateway Service

Switch to the "edusys-grpc-gateway" directory, and execute the following commands:

```bash
# 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](http://localhost:8080/apis/swagger/index.html) in your browser to test the API interfaces.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfv8o99qmct0g0s5es19.png)

<br>

**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](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](https://go-sponge.com/zh-cn/rpc-gateway-development-protobuf)