当前位置:网站首页>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
边栏推荐
- Concise and practical time code
- What are the configuration requirements for cloud desktop servers? What are the main characteristics of the three points?
- Big coffee face to face | Dr. Chen Guoguo talks about intelligent voice
- What is the difference between elasticity and scalability of cloud computing? What does elastic scaling of cloud computing mean?
- Grpc: how to implement the restful API for file uploading?
- JD Logistics: from giant baby to mainstay
- Grpc: how to implement distributed log tracing?
- UI automation based on Selenium
- US Treasury secretary says extortion software poses a threat to the economy, Google warns 2billion chrome users | global network security hotspot
- Dry goods how to build a data visualization project from scratch?
猜你喜欢

2022-2028 global genome editing mutation detection kit industry survey and trend analysis report

Get to know MySQL database

UI automation based on Selenium

2022-2028 global aircraft audio control panel system industry research and trend analysis report

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

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] 3047 displacement operation](/img/cb/9380337adbc09c54a5b984cab7d3b8.jpg)
[51nod] 3047 displacement operation
![[51nod] 2102 or minus and](/img/68/0d966b0322ac1517dd2800234d386d.jpg)
[51nod] 2102 or minus and
![[summary of interview questions] zj6 redis](/img/4b/eadf66ca8d834f049f3546d348fa32.jpg)
[summary of interview questions] zj6 redis
随机推荐
How does cloud computing achieve elastic scaling? What are the characteristics of elasticity?
[competition experience sharing] design of intelligent guide rod
Introduce the comparison of various distributed configuration centers? Which distributed configuration center is better?
What is distributed configuration center Nacos? What are the functions of distributed configuration center Nacos?
Innovation or hype? Is low code a real artifact or a fake tuyere?
What does elastic scaling of cloud computing mean? What are the application scenarios for elastic scaling of cloud computing?
How to select a server with appropriate configuration when planning to build a live broadcast platform
What is fortress resource authorization? What is barrier machine?
What are the configuration requirements for cloud desktop servers? What are the main characteristics of the three points?
News | detailed explanation of network security vulnerabilities of branch enterprises
Tencent cloud CVM starts IPv6
Where is the cloud game server? Can individuals rent cloud game servers?
Tencent cloud CIF engineering efficiency summit ends perfectly
Troubleshooting and resolution of errors in easycvr calling batch deletion interface
Tencent Mu Lei: real scene 3D linking industrial Internet and consumer Internet
How to install the cloud desktop security server certificate? What can cloud desktops do?
How to set up a cloud desktop server? Is there a charge for cloud desktop server setup?
[see you] on October 24, we met at Tencent Binhai building
System library golang Org/x/time/rate frequency limiter bug
Grpc: how to implement the restful API for file uploading?