当前位置:网站首页>rk-boot framework combat (1)
rk-boot framework combat (1)
2022-07-29 23:54:00 【raymondyShan】
RK-BOOT 框架实战(1)
rk-boot The frame is able to passyamlQuick launch of popular code frameworks in the form of configuration files. 适用于Gin,Fiber,Zero等代码框架.

1. rk-bootThe advantages and necessity of the frame
以Gin 框架为例.
rk-bootIt is packaged on the basis of various popular frameworks.Of course if you are developing at the same time,This encapsulation function can also be implemented.These feature packsDatabase的初始化,JWT的相关配置,and system log records,以及SwaggerThe generation configuration of related documents is registered. rk-bootThe perfect integration of these functions in the sub ahyaml文件中,And realize the pluggable characteristics of system development.Let the user not have to focus too much on the initialization operation of the function,Let users pay more attention to the development of business logic in the system.
1.1 优点(官方文档)
通过rk-boot, Ability to build enterprise-level projects; 通过rk-boot, Enterprise level logs are added automatically,监控,调用链,permissions to the server; 通过rk-boot, Quickly switch to core dependencies; 通过rk-boot, Save the complex learning process of open source frameworks; 自定义Entry.
1.2 内置插件
rk-boot,In addition to encapsulating popular frameworks,还提供了database,CORS,zap等插件.

And the use of these plugins,Just through the config fileboot.yaml就可以启用.
2. 快速启动Web服务
2.1 实现的功能
api文档的生成(swagger) api服务生成
2.2 实践
创建boot.yaml 文件 创建main.go文件 生成api文档 请求api
2.2.1 创建boot.yaml配置文件
gin:
- name: greeter
port: 8080 # 监听端口
enabled: true # 开启 gin 微服务
sw:
enabled: true # 开启 Swagger UI,默认路径为 /sw
docs:
enabled: true # 开启 API Doc UI,默认路径为 /docs
prom:
enabled: true # 开启 Prometheus 客户端,默认路径为 /metrics
middleware:
logging:
enabled: true # 开启 API 日志中间件
prom:
enabled: true # 开启 API Prometheus 中间件
meta:
enabled: true # 开启 API 原数据中间件,自动生成 RequestID
2.2.2 创建main.go文件
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/rookie-ninja/rk-boot/v2"
"github.com/rookie-ninja/rk-gin/v2/boot"
"net/http"
)
// @title Swagger Example API
// @version 1.0
// @description This is a sample rk-demo server.
// @termsOfService http://swagger.io/terms/
// @securityDefinitions.basic BasicAuth
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
func main() {
boot := rkboot.NewBoot()
// 注册 API
entry := rkgin.GetGinEntry("greeter")
entry.Router.GET("/v1/greeter", Greeter)
// 启动
boot.Bootstrap(context.TODO())
// 等待关闭信号
boot.WaitForShutdownSig(context.TODO())
}
// Greeter handler
// @Summary Greeter
// @Id 1
// @Tags Hello
// @version 1.0
// @Param name query string true "name"
// @produce application/json
// @Success 200 {object} GreeterResponse
// @Router /v1/greeter [get]
func Greeter(ctx *gin.Context) {
ctx.JSON(http.StatusOK, &GreeterResponse{
Message: fmt.Sprintf("Hello %s!", ctx.Query("name")),
})
}
type GreeterResponse struct {
Message string
}
2.2.3 生成api接口文档
swag init
2.2.4 运行服务
$ go run main.go
2022-04-14T01:09:52.073+0800 INFO boot/gin_entry.go:656 Bootstrap GinEntry {"eventId": "432fcdfb-05e8-46a4-b7b2-d6b6967eba88", "entryName": "greeter", "entryType": "GinEntry"}
2022-04-14T01:09:52.074+0800 INFO boot/gin_entry.go:413 SwaggerEntry: http://localhost:8080/sw/
2022-04-14T01:09:52.074+0800 INFO boot/gin_entry.go:416 DocsEntry: http://localhost:8080/docs/
2022-04-14T01:09:52.074+0800 INFO boot/gin_entry.go:419 PromEntry: http://localhost:8080/metrics
------------------------------------------------------------------------
endTime=2022-04-14T01:09:52.074233+08:00
startTime=2022-04-14T01:09:52.073337+08:00
elapsedNano=896392
timezone=CST
ids={"eventId":"432fcdfb-05e8-46a4-b7b2-d6b6967eba88"}
app={"appName":"rk","appVersion":"local","entryName":"greeter","entryType":"GinEntry"}
env={"arch":"amd64","domain":"*","hostname":"lark.local","localIP":"192.168.101.5","os":"darwin"}
payloads={"docsEnabled":true,"docsPath":"/docs/","ginPort":8080,"promEnabled":true,"promPath":"/metrics","promPort":8080,"swEnabled":true,"swPath":"/sw/"}
counters={}
pairs={}
timing={}
remoteAddr=localhost
operation=Bootstrap
resCode=OK
eventStatus=Ended
EOE
2.2.5 验证
Swagger UI Api文档# http://localhost:8080/sw/

发送请求
$ curl -vs "localhost:8080/v1/greeter?name=rk-dev"
* ...
< X-Request-Id: 7120529c-893b-4caa-a425-bc268de5cbc0
< X-Rk-App-Domain: *
< X-Rk-App-Name: rk
< X-Rk-App-Unix-Time: 2022-04-15T03:37:22.848829+08:00
< X-Rk-App-Version: local
< X-Rk-Received-Time: 2022-04-14T01:18:39.013447+08:00
< ...
{"Message":"Hello rk-dev!"}
The above process is straightforward参考官方文档进行详细的查看
3. 总结
本篇文章主要介绍了rk-boot 框架,并结合GinThe framework is simple to use.下篇文章中,will be tailored to business needs,进行代码的优化,Practice for the following blocks:
使用Mysql数据库 功能拆分 自定义配置 Generate your own template
边栏推荐
- Sentinel入门
- Override and customize dependent native Bean methods
- jenkins搭建部署详细步骤
- Huawei 14 Days - (3) Kernel Development
- 深度学习的随机种子
- Adaptive feature fusion pyramid network for multi-classes agriculturalpest detection
- vim相关介绍(三)
- In 2022, the latest Gansu construction staff (material staff) mock exam questions and answers
- The difference and usage of call, apply and bind
- Design for failure常见的12种设计思想
猜你喜欢

windows下 PHP 安装

UE4 makes crosshair + recoil

High Numbers|Calculation of Triple Integral 3|Uncle High Numbers|Handwritten Notes

NumPy(二)

vim相关介绍(三)

Vulkan与OpenGL对比——Vulkan的全新渲染架构

NumPy(一)

Framework 到底该怎么学习?

关于MySQL索引的一些个人理解(部分参考MySQL45讲)

EA & UML Sun Gong Yip - Multitasking Programming Super Introductory - (7) About mutex, you must know
随机推荐
Elementary C language - first understanding of C language
Codeforces Round #245 (Div. 1) A (dfs)
全国双非院校考研信息汇总整理 Part.1
idea设置自动去除未引用(不再引用)的引用
高数下|三重积分的计算3|高数叔|手写笔记
关于 byte 的范围
vim相关介绍(三)
devops学习(六)Jenkins 持续部署-版本选择
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史
esp12f + tft 显示图片问题
新标杆!美创科技助力广西桂林某三甲医院实现勒索病毒主动防御
Mysql内外连接
2022/7/29 考试总结
PyTorch笔记 - Attention Is All You Need (1)
Framework 到底该怎么学习?
Dropout回顾
logback过期日志文件自动删除
容器化 | 在 Rancher 中部署 MySQL 集群
彻底搞懂kubernetes调度框架与插件
go语言(函数、闭包、defer、panic/recover,递归,结构体,json序列化与反序列化)