当前位置:网站首页>Grpc: how to make grpc provide swagger UI?
Grpc: how to make grpc provide swagger UI?
2022-06-24 03:20:00 【Trespass 】
Introduce
This article will introduce how to make a gRPC On top of the service Swagger UI.
In order to provide Swagger UI, Let's first make gRPC Provide Restful API, then ,Swagger UI To access the background .
- In order to make gRPC Provide REST API, We need to use grpc-gateway
- We need to use protoc-gen-openapiv2 from proto file , establish Swagger UI Required documents
Please visit the following address for a complete tutorial :https://rkdev.info/cnhttps://rkdocs.netlify.app/cn ( spare )
precondition
Have used GRPC All users should know ,protocol buffer The file needs to use the relevant command line , hold .proto File compiled into .go file .
According to different needs , Will use different command line files . With Go Language as an example , We need the following command line files roughly .
Tools | Introduce | install |
|---|---|---|
protocol buffer Command line required for compilation | ||
from proto file , Generate .go file | ||
from proto file , Generate GRPC dependent .go file | ||
from proto file , Generate grpc-gateway dependent .go file | ||
from proto file , Generate swagger Parameter files required for the interface |
In addition to installing the above command line , We also need to , Run at least 4 Two different commands to compile *.proto file , It's very obscure .
For specific operation methods, please refer to my previous article :【GRPC: Use Buf Quick compilation GRPC proto file 】 Or visit :【https://rkdev.info/cn/docs/bootstrapper/user-guide/grpc-golang/basic/grpc-gateway/】
install
go get github.com/rookie-ninja/rk-boot go get github.com/rookie-ninja/rk-grpc
Quick start
1. establish api/v1/greeter.proto
syntax = "proto3";
package api.v1;
option go_package = "api/v1/greeter";
service Greeter {
rpc Greeter (GreeterRequest) returns (GreeterResponse) {}
}
message GreeterRequest {
string name = 1;
}
message GreeterResponse {
string message = 1;
}2. establish api/v1/gw_mapping.yaml
type: google.api.Service
config_version: 3
# Please refer google.api.Http in https://github.com/googleapis/googleapis/blob/master/google/api/http.proto file for details.
http:
rules:
- selector: api.v1.Greeter.Greeter
get: /api/v1/greeter3. establish buf.yaml
version: v1beta1
name: github.com/rk-dev/rk-demo
build:
roots:
- api4. establish buf.gen.yaml
version: v1beta1
plugins:
# protoc-gen-go needs to be installed, generate go files based on proto files
- name: go
out: api/gen
opt:
- paths=source_relative
# protoc-gen-go-grpc needs to be installed, generate grpc go files based on proto files
- name: go-grpc
out: api/gen
opt:
- paths=source_relative
- require_unimplemented_servers=false
# protoc-gen-grpc-gateway needs to be installed, generate grpc-gateway go files based on proto files
- name: grpc-gateway
out: api/gen
opt:
- paths=source_relative
- grpc_api_configuration=api/v1/gw_mapping.yaml
# protoc-gen-openapiv2 needs to be installed, generate swagger config files based on proto files
- name: openapiv2
out: api/gen
opt:
- grpc_api_configuration=api/v1/gw_mapping.yaml5. compile proto file
$ buf generate
The following files will be created .
$ tree api/gen
api/gen
└── v1
├── greeter.pb.go
├── greeter.pb.gw.go
├── greeter.swagger.json
└── greeter_grpc.pb.go
1 directory, 4 files6. establish boot.yaml
grpc:
- name: greeter # Name of grpc entry
port: 8080 # Port of grpc entry
enabled: true # Enable grpc entry
sw:
enabled: true # Enable swagger
jsonPath: "api/gen/v1" # Provide swagger config file path7. establish main.go
package main
import (
"context"
"fmt"
"github.com/rookie-ninja/rk-boot"
"github.com/rookie-ninja/rk-demo/api/gen/v1"
"github.com/rookie-ninja/rk-grpc/boot"
"google.golang.org/grpc"
)
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// ***************************************
// ******* Register GRPC & Gateway *******
// ***************************************
// Get grpc entry with name
grpcEntry := boot.GetEntry("greeter").(*rkgrpc.GrpcEntry)
// Register grpc registration function
grpcEntry.AddRegFuncGrpc(registerGreeter)
// Register grpc-gateway registration function
grpcEntry.AddRegFuncGw(greeter.RegisterGreeterHandlerFromEndpoint)
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}
// Implementation of [type GrpcRegFunc func(server *grpc.Server)]
func registerGreeter(server *grpc.Server) {
greeter.RegisterGreeterServer(server, &GreeterServer{})
}
// Implementation of grpc service defined in proto file
type GreeterServer struct{}
func (server *GreeterServer) Greeter(ctx context.Context, request *greeter.GreeterRequest) (*greeter.GreeterResponse, error) {
return &greeter.GreeterResponse{
Message: fmt.Sprintf("Hello %s!", request.Name),
}, nil
}8. Folder structure
$ tree . ├── api │ ├── gen │ │ └── v1 │ │ ├── greeter.pb.go │ │ ├── greeter.pb.gw.go │ │ ├── greeter.swagger.json │ │ └── greeter_grpc.pb.go │ └── v1 │ ├── greeter.proto │ └── gw_mapping.yaml ├── boot.yaml ├── buf.gen.yaml ├── buf.yaml ├── go.mod ├── go.sum └── main.go 4 directories, 12 files
9. verification
visit Swagger:http://localhost:8080/sw
边栏推荐
- Tencent cloud CIF engineering efficiency summit ends perfectly
- Chapter 4: LED flash case of PS bare metal and FreeRTOS case development
- Cp/rm/mv parameters
- Process kill problem
- How does the fortress machine connect to the server? Which is easy to use, fortress machine or firewall?
- Gigabyte was attacked by blackmail software, and the FBI banned the hacker organization Revil | global network security hotspot
- [hot] with a budget of only 100 yuan, how to build a 1-year web site on Tencent cloud??
- Understanding Devops from the perspective of decision makers
- Sinclair radio stopped broadcasting many TV stations, suspected of being attacked by blackmail software
- What is the maximum bandwidth of EIP? Mbps how to modify the bandwidth of EIP
猜你喜欢
![[51nod] 2106 an odd number times](/img/af/59b441420aa4f12fd50f5062a83fae.jpg)
[51nod] 2106 an odd number times

Simple and beautiful weather code

2022-2028 global anti counterfeiting label industry research and trend analysis report

UI automation based on Selenium
![[summary of interview questions] zj5](/img/d8/ece82f8b2479adb948ba706f6f5039.jpg)
[summary of interview questions] zj5

2022-2028 global aircraft front wheel steering system industry research and trend analysis report
![[51nod] 3216 Awards](/img/94/fdb32434d1343040d711c76568b281.jpg)
[51nod] 3216 Awards
![[51nod] 2653 section XOR](/img/2d/cb4bf4e14939ce432cac6d35b6a41b.jpg)
[51nod] 2653 section XOR
![[51nod] 2102 or minus and](/img/68/0d966b0322ac1517dd2800234d386d.jpg)
[51nod] 2102 or minus and

Get to know MySQL database
随机推荐
Why do cloud desktops use rack servers? Why choose cloud desktop?
The medical technology giant was blackmailed and Microsoft announced 74 security vulnerabilities | global network security hotspot
How does the fortress machine connect to the server? Which is easy to use, fortress machine or firewall?
What is the GPU usage for cloud desktops and servers? What can cloud desktop do?
What is the maximum bandwidth of EIP? Mbps how to modify the bandwidth of EIP
[Tencent cloud] how can the MySQL database on the cloud fully back up the data base script?
What are the functions of Fortress machine equipment
[51nod] 3216 Awards
How much is a fortress machine? Why do you need a fortress machine?
Does the user need a code signing certificate? What is the use of a code signing certificate
Why can't the fortress machine log in? What are the ways to solve the problem
US Treasury secretary says extortion software poses a threat to the economy, Google warns 2billion chrome users | global network security hotspot
14. Tencent cloud IOT device side learning - data template application development
What is edge computing? What are the characteristics of the Internet platform edge calculator?
How to design a hybrid system
golang with script
How to pair cloud game servers? Is the cloud game server expensive?
[competition experience sharing] design of intelligent guide rod
How to build a shopping website? What problems should be paid attention to in the construction of shopping websites?
Ligature in font design