当前位置:网站首页>A set of code to launch seven golang web frameworks at the same time
A set of code to launch seven golang web frameworks at the same time
2022-06-23 17:59:00 【Trespass 】
Today we are going to do an interesting Go practice . Using the same code , In a process , At the same time to start 7 Different species Go Web frame .
Why do such boring things ?
The main purpose is to introduce rookie-ninja/rk-boot library .
Start what Go Web frame ?
We start the following at the same time Go Web frame .
Web frame | rk-boot rely on | edition |
|---|---|---|
go get github.com/rookie-ninja/rk-boot/gin | v1.2.14 (Stable) | |
go get github.com/rookie-ninja/rk-boot/grpc | v1.2.18 (Stable) | |
go get github.com/rookie-ninja/rk-boot/echo | v0.0.8 (Stable) | |
go get github.com/rookie-ninja/rk-boot/gf | v0.0.6 (Stable) | |
go get github.com/rookie-ninja/rk-boot/fiber | v0.0.4 (Testing) | |
go get github.com/rookie-ninja/rk-boot/zero | v0.0.2 (Testing) | |
go get github.com/rookie-ninja/rk-boot/mux | v0.0.2 (Testing) |
Quick start
1. install
We go through go get Install the following dependencies .
go get github.com/rookie-ninja/rk-boot/grpc go get github.com/rookie-ninja/rk-boot/gin go get github.com/rookie-ninja/rk-boot/echo go get github.com/rookie-ninja/rk-boot/gf go get github.com/rookie-ninja/rk-boot/fiber go get github.com/rookie-ninja/rk-boot/zero go get github.com/rookie-ninja/rk-boot/mux
2. establish boot.yaml
Web frame | port |
|---|---|
8081 | |
8082 | |
8083 | |
8084 | |
8085 | |
8086 | |
8087 |
In addition to specifying ports , We have also enabled the following two options :
- commonService: Provide /rk/v1/healthy This kind of universal API.
- loggingZap: RPC journal
---
grpc:
- name: grpc
port: 8081
enabled: true
commonService:
enabled: true # Optional, default: false
interceptors:
loggingZap:
enabled: true # Optional, default: false
gin:
- name: gin
port: 8082
enabled: true
commonService:
enabled: true # Optional, default: false
interceptors:
loggingZap:
enabled: true # Optional, default: false
echo:
- name: echo
port: 8083
enabled: true
commonService:
enabled: true # Optional, default: false
interceptors:
loggingZap:
enabled: true # Optional, default: false
gf:
- name: gf
port: 8084
enabled: true
commonService:
enabled: true # Optional, default: false
interceptors:
loggingZap:
enabled: true # Optional, default: false
fiber:
- name: fiber
port: 8085
enabled: true
commonService:
enabled: true # Optional, default: false
interceptors:
loggingZap:
enabled: true # Optional, default: false
zero:
- name: zero
port: 8086
enabled: true
commonService:
enabled: true # Optional, default: false
interceptors:
loggingZap:
enabled: true # Optional, default: false
mux:
- name: mux
port: 8087
enabled: true
commonService:
enabled: true # Optional, default: false
interceptors:
loggingZap:
enabled: true # Optional, default: false3. establish main.go
all Web The framework is all in use handleRequest() Function to handle the request .
gRPC exception , We didn't use protocol buffer, because PB Different protocols . We use grpc-gateway To simulate the .
grpcEntry On by default grpc-gateway, adopt grpcEntry.HttpMux Go to grpc-gateway Registered in the API.
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gofiber/adaptor/v2"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/labstack/echo/v4"
"github.com/rookie-ninja/rk-boot"
"github.com/rookie-ninja/rk-boot/echo"
"github.com/rookie-ninja/rk-boot/fiber"
"github.com/rookie-ninja/rk-boot/gf"
"github.com/rookie-ninja/rk-boot/gin"
"github.com/rookie-ninja/rk-boot/grpc"
"github.com/rookie-ninja/rk-boot/mux"
"github.com/rookie-ninja/rk-boot/zero"
"github.com/tal-tech/go-zero/rest"
"net/http"
)
const handlePath = "/v1/hello"
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// 6: go-zero @8086, must adds route before bootstrap
rkbootzero.GetZeroEntry("zero").Server.AddRoute(rest.Route{
Method: http.MethodGet,
Path: handlePath,
Handler: handleRequest,
})
// Bootstrap
boot.Bootstrap(context.Background())
// 1: grpc-gateway @8081
rkbootgrpc.GetGrpcEntry("grpc").HttpMux.HandleFunc(handlePath, handleRequest)
// 2: gin @8082
rkbootgin.GetGinEntry("gin").Router.Handle(http.MethodGet, handlePath, gin.WrapF(handleRequest))
// 3: echo @8083
rkbootecho.GetEchoEntry("echo").Echo.GET(handlePath, echo.WrapHandler(http.HandlerFunc(handleRequest)))
// 4: GoFrame @8084
rkbootgf.GetGfEntry("gf").Server.BindHandler(handlePath, ghttp.WrapF(handleRequest))
// 5: Fiber @8085, must call RefreshFiberRoutes()
rkbootfiber.GetFiberEntry("fiber").App.Get(handlePath, adaptor.HTTPHandler(http.HandlerFunc(handleRequest)))
rkbootfiber.GetFiberEntry("fiber").RefreshFiberRoutes()
// 7: mux @8087
rkbootmux.GetMuxEntry("mux").Router.HandleFunc(handlePath, handleRequest)
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}
// Handle request for all web frameworks
func handleRequest(writer http.ResponseWriter, req *http.Request) {
// 1: get query
name := req.URL.Query().Get("name")
// 2: marshal response
bytes, _ := json.Marshal(&Response{
Message: fmt.Sprintf("Hello %s", name),
})
// 3: write response
writer.WriteHeader(http.StatusOK)
writer.Write(bytes)
}
type Response struct {
Message string `json:"message"`
}4. Folder structure & go.mod
- Folder structure
. ├── boot.yaml ├── go.mod ├── go.sum └── main.go 0 directories, 4 files
- go.mod file
module github.com/rookie-ninja/rk-demo go 1.16 require ( github.com/gin-gonic/gin v1.7.7 github.com/gofiber/adaptor/v2 v2.1.15 github.com/gogf/gf/v2 v2.0.0-beta github.com/labstack/echo/v4 v4.6.1 github.com/rookie-ninja/rk-boot v1.4.0 github.com/rookie-ninja/rk-boot/echo v0.0.8 github.com/rookie-ninja/rk-boot/fiber v0.0.4 github.com/rookie-ninja/rk-boot/gf v0.0.6 github.com/rookie-ninja/rk-boot/gin v1.2.14 github.com/rookie-ninja/rk-boot/grpc v1.2.18 github.com/rookie-ninja/rk-boot/mux v0.0.2 github.com/rookie-ninja/rk-boot/zero v0.0.2 github.com/tal-tech/go-zero v1.2.4 )
5. start-up main.go
$ go run main.go
2022-01-03T19:15:14.016+0800 INFO boot/gf_entry.go:1050 Bootstrap gfEntry {"eventId": "264033ff-be9c-4147-871c-e4873ea1510d", "entryName": "gf"}
------------------------------------------------------------------------
endTime=2022-01-03T19:15:14.016473+08:00
startTime=2022-01-03T19:15:14.016057+08:00
elapsedNano=416178
timezone=CST
ids={"eventId":"264033ff-be9c-4147-871c-e4873ea1510d"}
app={"appName":"rk","appVersion":"","entryName":"gf","entryType":"GfEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"commonServiceEnabled":true,"commonServicePathPrefix":"/rk/v1/","gfPort":8084}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost
operation=Bootstrap
resCode=OK
eventStatus=Ended
EOE
...call /rk/v1/healthy To determine whether the service is started .
$ curl localhost:8081/rk/v1/healthy
{"healthy":true}
$ curl localhost:8082/rk/v1/healthy
{"healthy":true}
$ curl localhost:8083/rk/v1/healthy
{"healthy":true}
$ curl localhost:8084/rk/v1/healthy
{"healthy":true}
$ curl localhost:8085/rk/v1/healthy
{"healthy":true}
$ curl localhost:8086/rk/v1/healthy
{"healthy":true}
$ curl localhost:8087/rk/v1/healthy
{"healthy":true}6. verification
We will send /v1/hello Request to each port , And look at RPC journal .
- grpc-gateway@8081 (grpc-gateway Currently none RPC Log output )
$ curl "localhost:8081/v1/hello?name=rk-dev"
{"message":"Hello rk-dev"}$ curl "localhost:8082/v1/hello?name=rk-dev"
{"message":"Hello rk-dev"}
# RPC log from server side
------------------------------------------------------------------------
endTime=2022-01-03T19:18:26.23983+08:00
startTime=2022-01-03T19:18:26.239805+08:00
elapsedNano=25460
timezone=CST
ids={"eventId":"0d284016-f714-4c85-8af8-9715dc9ed35f"}
app={"appName":"rk","appVersion":"","entryName":"gin","entryType":"GinEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/v1/hello","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost:54102
operation=/v1/hello
resCode=200
eventStatus=Ended
EOE$ curl "localhost:8082/v1/hello?name=rk-dev"
{"message":"Hello rk-dev"}
# RPC log from server side
------------------------------------------------------------------------
endTime=2022-01-03T19:19:11.109838+08:00
startTime=2022-01-03T19:19:11.109817+08:00
elapsedNano=21242
timezone=CST
ids={"eventId":"34419c7c-1a78-484f-ba7a-bfca69178b82"}
app={"appName":"rk","appVersion":"","entryName":"echo","entryType":"EchoEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/v1/hello","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost:56995
operation=/v1/hello
resCode=200
eventStatus=Ended
EOE- gf@8084
$ curl "localhost:8084/v1/hello?name=rk-dev"
{"message":"Hello rk-dev"}
# RPC log from server side
------------------------------------------------------------------------
endTime=2022-01-03T19:19:39.905883+08:00
startTime=2022-01-03T19:19:39.905858+08:00
elapsedNano=24703
timezone=CST
ids={"eventId":"58bf8706-09ff-434e-b405-d6cdb8dbe8c2"}
app={"appName":"rk","appVersion":"","entryName":"gf","entryType":"GfEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/v1/hello","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost:58802
operation=/v1/hello
resCode=200
eventStatus=Ended
EOE$ curl "localhost:8085/v1/hello?name=rk-dev"
{"message":"Hello rk-dev"}
# RPC log from server side
------------------------------------------------------------------------
endTime=2022-01-03T19:20:48.834567+08:00
startTime=2022-01-03T19:20:48.834425+08:00
elapsedNano=142332
timezone=CST
ids={"eventId":"a98a6e9f-6519-4ded-971e-0b6e59f66096"}
app={"appName":"rk","appVersion":"","entryName":"fiber","entryType":"FiberEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/v1/hello","apiProtocol":"http","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=127.0.0.1:63237
operation=/v1/hello
resCode=200
eventStatus=Ended
EOE$ curl "localhost:8086/v1/hello?name=rk-dev"
{"message":"Hello rk-dev"}
# RPC log from server side
------------------------------------------------------------------------
endTime=2022-01-03T19:21:20.835415+08:00
startTime=2022-01-03T19:21:20.835391+08:00
elapsedNano=24495
timezone=CST
ids={"eventId":"a6a53d21-4cf4-4b45-97ca-2b190e438e9c","traceId":"bf7a2359d0813de4388dd11c4f161321"}
app={"appName":"rk","appVersion":"","entryName":"zero","entryType":"ZeroEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/v1/hello","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost:65299
operation=/v1/hello
resCode=200
eventStatus=Ended
EOE$ curl "localhost:8086/v1/hello?name=rk-dev"
{"message":"Hello rk-dev"}
# RPC log from server side
------------------------------------------------------------------------
endTime=2022-01-03T19:22:13.13191+08:00
startTime=2022-01-03T19:22:13.131889+08:00
elapsedNano=21449
timezone=CST
ids={"eventId":"8a0f2db6-8e13-4773-bedd-962060adbe41"}
app={"appName":"rk","appVersion":"","entryName":"mux","entryType":"MuxEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/v1/hello","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=127.0.0.1:52277
operation=/v1/hello
resCode=200
eventStatus=Ended
EOErk-boot Introduce
rk-boot It's a pass through YAML Start multiple Web Service framework .
It's kind of like Spring boot. Through integration rk-xxx Series Library , Can start a variety of Web frame . Of course , Users can also customize rk-xxx The library is integrated into rk-boot in .
rk-boot Bright spot
- Through the same format YAML file , Start different Web frame .
- Even if it's a different framework , Unified log ,Metrics, Tracing Equiform
- Users can implement rkentry.Entry From definition YAML file .
rk-boot Supported by Web frame
Welcome to contribute new Web Frame to rk-boot In the series .
Reference resources docs & rk-gin As an example .
frame | Development status | install | rely on |
|---|---|---|---|
Stable | go get github.com/rookie-ninja/rk-boot/gin | ||
Stable | go get github.com/rookie-ninja/rk-boot/grpc | ||
Stable | go get github.com/rookie-ninja/rk-boot/echo | ||
Stable | go get github.com/rookie-ninja/rk-boot/gf | ||
Testing | go get github.com/rookie-ninja/rk-boot/fiber | ||
Testing | go get github.com/rookie-ninja/rk-boot/zero | ||
Testing | go get github.com/rookie-ninja/rk-boot/mux |
边栏推荐
- The principle of MySQL index algorithm and the use of common indexes
- AMQP protocol
- Analysis of object class structure in Nanny level teaching (common class) [source code attached]
- ACM players take you to play with the array!
- 酒店入住时间和离店时间的日期选择
- FPN characteristic pyramid network
- Goframe framework: fast implementation of service end flow limiting Middleware
- POC about secureworks' recent azure Active Directory password brute force vulnerability
- 解答03:Smith圆为什么能“上感下容 左串右并”?
- JS reset form
猜你喜欢

torch学习(一):环境配置

FPN characteristic pyramid network

qYKVEtqdDg

Self supervised learning (SSL)

Interface ownership dispute

QT当中的【QSetting和.ini配置文件】以及【创建Resources.qrc】

《MPLS和VP体系结构》
![[qsetting and.Ini configuration files] and [create resources.qrc] in QT](/img/67/85a5e7f6ad4220600acd377248ef46.png)
[qsetting and.Ini configuration files] and [create resources.qrc] in QT

JSON - learning notes (message converter, etc.)
![[mae]masked autoencoders mask self encoder](/img/08/5ab2b0d5b81c723919046699bb6f6d.png)
[mae]masked autoencoders mask self encoder
随机推荐
Hapoxy cluster service setup
Date to localdatetime
Thymeleaf - learning notes
What is the problem with TS File Error 404 when easynvr plays HLS protocol?
How about stock online account opening and account opening process? Is online account opening safe?
Discussion on five kinds of zero crossing detection circuit
Ctfshow PHP features
MySQL事务及其特性与锁机制
Company offensive operation guide
一文入门智能开关的3种功能形态
手机开户一般哪个证券公司好?在线开户安全么?
MySQL installation, configuration and uninstall
单火线设计系列文章10:拓展应用-单火开关实现双控
Async/await
AMQP协议
Redis ubuntu18.04.6 intranet deployment
qYKVEtqdDg
Postgresql_根据执行计划优化SQL
Revil - blackmail Virus Emergency Response
Single fire wire design series article 10: expanding application - single fire switch realizes double control