当前位置:网站首页>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入门
- USACO2008通信线路
- MySQL函数(经典收藏)
- 【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史
- 全国双非院校考研信息汇总整理 Part.7
- 种类并查集(反集),学习T宝代码
- Design for failure常见的12种设计思想
- Brute force recursion to dynamic programming 03 (knapsack problem)
- BEVDetNet:Bird‘s Eye View LiDAR Point Cloud based Real-time 3D Object Detection for Autonomous Drivi
- logback过期日志文件自动删除
猜你喜欢

437. 路径总和 III ●●

微信小程序获取手机号getPhoneNumber接口报错44002

彻底搞懂kubernetes调度框架与插件

【无标题】

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

JVM初探- 内存分配、GC原理与垃圾收集器

【云原生Kubernetes】二进制搭建Kubernetes集群(中)——部署node节点

SQL Server、MySQL主从搭建,EF Core读写分离代码实现

EA&UML日拱一卒-状态图::重画按钮状态图

Tkinter:功能按钮Button
随机推荐
Getting Started with Sentinel
High Numbers|Calculation of Triple Integral 3|Uncle High Numbers|Handwritten Notes
指令集数据产品如何设计和实现报表协同系统——基于指令集物联网操作系统的工业协同制造项目开发实践
子无序测试
SQL Server、MySQL主从搭建,EF Core读写分离代码实现
The difference and usage of call, apply and bind
[2023 School Recruitment Questions] Summary of Common Interview Questions (7. Common Bus Protocols) (Continuously updated with subsequent interviews....)
容器化数据库必经之道
Override and customize dependent native Bean methods
Brute force recursion to dynamic programming 04 (digital string conversion)
NumPy(二)
接口性能测试方案设计方法有哪些?要怎么去写?
Mysql8.0新特性之详细版本
y81. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Monitoring Extension (12)
shell编写规范和变量
C陷阱与缺陷 第4章 链接 4.3 命名冲突与static修饰符
Add, delete, modify and query the database
读书笔记:《这才是心理学:看穿伪心理学的本质(第10版)》
深度学习的随机种子
PyTorch笔记 - Attention Is All You Need (1)