kratos mysql_kratos微服务框架学习笔记一(kratos-demo)

article/2025/10/5 14:15:09

本文将为您描述kratos微服务框架学习笔记一(kratos-demo),教程操作步骤:

目录

kratos微服务框架学习笔记一(kratos-demo)

kratos本体

demo

kratos微服务框架学习笔记一(kratos-demo)

今年大部分时间飘过去了,没怎么更博和github,现在开发任务也差不多完成了,会比较轻松,考虑到今后发展,打算看看微服务框架。

常见微服务框架主要有这么几个

gizmo, a microservice toolkit from The New York Times ★

go-micro, a microservices client/server library ★

gotalk, async peer communication protocol & library

Kite, a micro-service framework

gocircuit, dynamic cloud orchestration

Kratos,bilibili开源的一套Go微服务框架,包含大量微服务相关框架及工具。

这里有一篇关于kit,go-mirco,gizmo,Kite的比较:

Go 微服务框架对比:Go Micro, Go Kit, Gizmo, Kite

我打算最先看看的框架是kratos,也不为什么,因为喜欢b站(#^.^#)。

github上关于kratos的介绍:

名字来源于:《战神》游戏以希腊神话为背景,讲述由凡人成为战神的奎托斯(Kratos)成为战神并展开弑神屠杀的冒险历程。

目标

致力于提供完整的微服务研发体验,整合相关框架及工具后,微服务治理相关部分可对整体业务开发周期无感,从而更加聚焦于业务交付。对每位开发者而言,整套Kratos框架也是不错的学习仓库,可以了解和参考到bilibili在微服务方面的技术积累和经验。

Features

HTTP Blademaster:核心基于gin进行模块化设计,简单易用、核心足够轻量;

GRPC Warden:基于官方gRPC开发,集成discovery服务发现,并融合P2C负载均衡;

Cache:优雅的接口化设计,非常方便的缓存序列化,推荐结合代理模式overlord;

Database:集成MySQL/HBase/TiDB,添加熔断保护和统计支持,可快速发现数据层压力;

Config:方便易用的paladin sdk,可配合远程配置中心,实现配置版本管理和更新;

Log:类似zap的field实现高性能日志库,并结合log-agent实现远程日志管理;

Trace:基于opentracing,集成了全链路trace支持(gRPC/HTTP/MySQL/Redis/Memcached);

Kratos Tool:工具链,可快速生成标准项目,或者通过Protobuf生成代码,非常便捷使用gRPC、HTTP、swagger文档;

kratos本体

先拉代码

5f27bffca7e3eb5f65a216b182b36da1.png

如果下不动可以试试配置下代理

0e514df6207681148c1206ba3ee7152d.png

记得打开 go111module

6805f393e0d48f2f45ed0eab745ff02a.png

kratos本体

类似于go命令,可以执行各种子工具,如go build、 go tool : kratos build 、kratos run

C:serversrctest-srcGo_Testkratos>kratos -h

NAME:

kratos - kratos工具集

USAGE:

kratos [global options] command [command options] [arguments...]

VERSION:

0.3.1

COMMANDS:

new, n 创建新项目

build, b kratos build

run, r kratos run

tool, t kratos tool

version, v kratos version

self-upgrade kratos self-upgrade

help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:

--help, -h show help

--version, -v print the version

C:serversrctest-srcGo_Testkratos>kratos version

Version: 0.3.1

Go version: go1.13.5

Built: 2019/11/06

OS/Arch: windows/amd64

demo

Quick start

Requirments

Go version>=1.13

Installation

GO111MODULE=on && go get -u github.com/bilibili/kratos/tool/kratos

cd $GOPATH/src

kratos new kratos-demo

通过 kratos new 会快速生成基于kratos库的脚手架代码,如生成 kratos-demo

Build & Run

cd kratos-demo/cmd

go build

./cmd -conf ../configs

打开浏览器访问:http://localhost:8000/kratos-demo/start,你会看到输出了Golang 大法好 !!!

官方介绍的有点简洁,咱们动手一步步来:

先进入$GOPATH/src 目录

kratos new demo

C:serversrcgosrc>kratos new demo

go get -u github.com/bilibili/kratos/tool/kratos-gen-project

go: finding golang.org/x/sys latest

go: finding golang.org/x/crypto latest

genproject: 安装成功!

go: finding github.com/bilibili/kratos master

go: downloading github.com/bilibili/kratos v0.3.2-0.20191216053608-e8e05452b3b0

go: downloading google.golang.org/grpc v1.24.0

go: extracting google.golang.org/grpc v1.24.0

go: extracting github.com/bilibili/kratos v0.3.2-0.20191216053608-e8e05452b3b0

go: downloading golang.org/x/net v0.0.0-20191011234655-491137f69257

go: extracting golang.org/x/net v0.0.0-20191011234655-491137f69257

go: downloading github.com/prometheus/client_golang v1.1.0

go: downloading github.com/go-sql-driver/mysql v1.4.1

go: downloading github.com/fsnotify/fsnotify v1.4.7

go: downloading google.golang.org/genproto v0.0.0-20191009194640-548a555dbc03

go: downloading gopkg.in/go-playground/validator.v9 v9.29.1

go: downloading github.com/shirou/gopsutil v2.19.6+incompatible

go: extracting github.com/go-sql-driver/mysql v1.4.1

go: extracting github.com/prometheus/client_golang v1.1.0

go: extracting gopkg.in/go-playground/validator.v9 v9.29.1

go: extracting github.com/fsnotify/fsnotify v1.4.7

go: downloading github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2

go: downloading github.com/gogo/protobuf v1.3.0

go: extracting github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2

go: downloading github.com/go-playground/universal-translator v0.16.0

go: extracting github.com/go-playground/universal-translator v0.16.0

go: extracting google.golang.org/genproto v0.0.0-20191009194640-548a555dbc03

go: extracting github.com/gogo/protobuf v1.3.0

go: downloading github.com/go-playground/locales v0.12.1

go: downloading github.com/prometheus/common v0.6.0

go: downloading github.com/leodido/go-urn v1.1.0

go: extracting github.com/prometheus/common v0.6.0

go: extracting github.com/leodido/go-urn v1.1.0

go: downloading github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90

go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1

go: extracting github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90

go: extracting github.com/matttproud/golang_protobuf_extensions v1.0.1

go: downloading github.com/beorn7/perks v1.0.1

go: extracting github.com/go-playground/locales v0.12.1

go: extracting github.com/beorn7/perks v1.0.1

go get -u github.com/bilibili/kratos/tool/kratos-protoc

protoc: 安装成功!

2019/12/18 15:38:39 您还没安装protobuf,请进行手动安装:https://github.com/protocolbuffers/protobuf/releases

exit status 1

go get -u github.com/bilibili/kratos/tool/kratos-gen-bts

genbts: 安装成功!

Close: 无声明 忽略此方法

Ping: 无声明 忽略此方法

dao.bts.go: 生成成功

go get -u github.com/bilibili/kratos/tool/kratos-gen-mc

genmc: 安装成功!

mc.cache.go: 生成成功

go get -u github.com/google/wire/cmd/wire

go: finding github.com/google/wire v0.4.0

go: downloading github.com/google/wire v0.4.0

go: extracting github.com/google/wire v0.4.0

go: downloading golang.org/x/tools v0.0.0-20191105231337-689d0f08e67a

go: downloading github.com/google/subcommands v1.0.1

go: extracting github.com/google/subcommands v1.0.1

go: extracting golang.org/x/tools v0.0.0-20191105231337-689d0f08e67a

go: finding golang.org/x/tools latest

go: finding github.com/google/subcommands v1.0.1

go: downloading golang.org/x/tools v0.0.0-20191218040434-6f9e13bbec44

go: extracting golang.org/x/tools v0.0.0-20191218040434-6f9e13bbec44

wire: 安装成功!

wire: C:serversrcgosrcdemointernaldiwire.go:17:65: DemoServer not declared by package api

wire: generate failed

exit status 1

Project: demo

OnlyGRPC: false

OnlyHTTP: false

Directory: C:serversrcgosrcdemo

项目创建成功.

其中提示了没安装protobuf,需要手动安装,先不管他直接跑demo试试。

2019/12/18 15:38:39 您还没安装protobuf,请进行手动安装:https://github.com/protocolbuffers/protobuf/releases

kratos run

C:serversrcgosrcdemo>kratos run

go: downloading github.com/shirou/gopsutil v2.19.6+incompatible

build command-line-arguments: cannot load github.com/shirou/gopsutil/cpu: github.com/shirou/gopsutil@v2.19.6+incompatible: reading https://goproxy.io/github.com/shirou/gopsutil/@v/v2.19.6+incompatible.zip: 410 Gone

panic: exit status 1

goroutine 1 [running]:

main.runAction(0xc0000922c0, 0x0, 0xc000042f30)

C:/server/src/go/pkg/mod/github.com/bilibili/kratos@v0.3.1/tool/kratos/run.go:25 +0x36e

github.com/urfave/cli.HandleAction(0x603080, 0x65fda8, 0xc0000922c0, 0xc0000922c0, 0x0)

C:/server/src/go/pkg/mod/github.com/urfave/cli@v1.22.2/app.go:523 +0xc5

github.com/urfave/cli.Command.Run(0x64c994, 0x3, 0x0, 0x0, 0xc000042de0, 0x1, 0x1, 0x650d90, 0xa, 0x0, ...)

C:/server/src/go/pkg/mod/github.com/urfave/cli@v1.22.2/command.go:174 +0x523

github.com/urfave/cli.(*App).Run(0xc0000ea000, 0xc0000044a0, 0x2, 0x2, 0x0, 0x0)

C:/server/src/go/pkg/mod/github.com/urfave/cli@v1.22.2/app.go:276 +0x72c

main.main()

C:/server/src/go/pkg/mod/github.com/bilibili/kratos@v0.3.1/tool/kratos/main.go:57 +0x3f7

报错:

cannot load github.com/shirou/gopsutil

好像是不能加载gopsutil 包

直接一下个试试:

go get -u github.com/shirou/gopsutil

C:serversrcgosrcdemo>go get -u github.com/shirou/gopsutil

go: downloading github.com/shirou/gopsutil v2.19.11+incompatible

go: extracting github.com/shirou/gopsutil v2.19.11+incompatible

C:serversrcgosrcdemo>kratos run

go: downloading github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6

go: extracting github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6

go: downloading github.com/go-ole/go-ole v1.2.4

go: extracting github.com/go-ole/go-ole v1.2.4

# demo/api

..apiclient.go:15:68: undefined: DemoClient

..apiclient.go:21:9: undefined: NewDemoClient

panic: exit status 2

可以运行了,但是没跑起来,api路径貌似和protobuf是有关联的,那么安装个protobuf试试。

https://github.com/protocolbuffers/protobuf/releases

下载,我是windows系统在path里面添加下bin目录环境变量即可。

protoc

Usage: protoc [OPTION] PROTO_FILES

Parse PROTO_FILES and generate output based on the options given:

-IPATH, --proto_path=PATH Specify the directory in which to search for

imports. May be specified multiple times;

directories will be searched in order. If not

given, the current working directory is used.

If not found in any of the these directories,

the --descriptor_set_in descriptors will be

checked for required proto file.

--version Show version info and exit.

-h, --help Show this text and exit.

--encode=MESSAGE_TYPE Read a text-format message of the given type

from standard input and write it in binary

to standard output. The message type must

be defined in PROTO_FILES or their imports.

--decode=MESSAGE_TYPE Read a binary message of the given type from

standard input and write it in text format

to standard output. The message type must

be defined in PROTO_FILES or their imports.

--decode_raw Read an arbitrary protocol message from

standard input and write the raw tag/value

pairs in text format to standard output. No

PROTO_FILES should be given when using this

flag.

--descriptor_set_in=FILES Specifies a delimited list of FILES

each containing a FileDescriptorSet (a

protocol buffer defined in descriptor.proto).

The FileDescriptor for each of the PROTO_FILES

provided will be loaded from these

FileDescriptorSets. If a FileDescriptor

appears multiple times, the first occurrence

will be used.

-oFILE, Writes a FileDescriptorSet (a protocol buffer,

--descriptor_set_out=FILE defined in descriptor.proto) containing all of

the input files to FILE.

--include_imports When using --descriptor_set_out, also include

all dependencies of the input files in the

set, so that the set is self-contained.

--include_source_info When using --descriptor_set_out, do not strip

SourceCodeInfo from the FileDescriptorProto.

This results in vastly larger descriptors that

include information about the original

location of each decl in the source file as

well as surrounding comments.

--dependency_out=FILE Write a dependency output file in the format

expected by make. This writes the transitive

set of input file paths to FILE

--error_format=FORMAT Set the format in which to print errors.

FORMAT may be 'gcc' (the default) or 'msvs'

(Microsoft Visual Studio format).

--print_free_field_numbers Print the free field numbers of the messages

defined in the given proto files. Groups share

the same field number space with the parent

message. Extension ranges are counted as

occupied fields numbers.

--plugin=EXECUTABLE Specifies a plugin executable to use.

Normally, protoc searches the PATH for

plugins, but you may specify additional

executables not in the path using this flag.

Additionally, EXECUTABLE may be of the form

NAME=PATH, in which case the given plugin name

is mapped to the given executable even if

the executable's own name differs.

--cpp_out=OUT_DIR Generate C++ header and source.

--csharp_out=OUT_DIR Generate C# source file.

--java_out=OUT_DIR Generate Java source file.

--js_out=OUT_DIR Generate JavaScript source.

--objc_out=OUT_DIR Generate Objective C header and source.

--php_out=OUT_DIR Generate PHP source file.

--python_out=OUT_DIR Generate Python source file.

--ruby_out=OUT_DIR Generate Ruby source file.

@ Read options and filenames from file. If a

relative file path is specified, the file

will be searched in the working directory.

The --proto_path option will not affect how

this argument file is searched. Content of

the file will be expanded in the position of

@ as in the argument list. Note

that shell expansion is not applied to the

content of the file (i.e., you cannot use

quotes, wildcards, escapes, commands, etc.).

Each line corresponds to a single argument,

even if it contains spaces.

确保protoc能够执行,删除demo目录重新创建:

这次能够运行成功了

3bea0d0586e15aeb8d73434a51689181.png

先试试文档给的路径:

c3fb746fb4879968fe3bd5836f2a2f00.png

不行, 看看源码:

blademaster应该就是对gin框架的裁剪

看看demo服务做了什么,

// New new a bm server.

func New(s pb.DemoServer) (engine *bm.Engine, err error) {

var (

hc struct {

Server *bm.ServerConfig

}

)

if err = paladin.Get("http.toml").UnmarshalTOML(&hc); err != nil {

if err != paladin.ErrNotExist {

return

}

err = nil

}

svc = s

engine = bm.DefaultServer(hc.Server)

pb.RegisterDemoBMServer(engine, s)

initRouter(engine)

err = engine.Start()

return

}

func initRouter(e *bm.Engine) {

e.Ping(ping)

g := e.Group("/abc")

{

g.GET("/start", howToStart)

}

}

添加了个/abc组,注册了/start 路由.

// Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.

// For example, all the routes that use a common middlware for authorization could be grouped.

func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {

return &RouterGroup{

Handlers: group.combineHandlers(handlers),

basePath: group.calculateAbsolutePath(relativePath),

engine: group.engine,

root: false,

}

}

看介绍,大概意思是创建给使用相同中间件和路径前缀的路由使用的路由组。

试试路径

/abc/start

成功了:

acb7aaf77b2ffb29c57d09f4137db31f.pngkratos微服务框架学习笔记一(kratos-demo)就为您介绍到这里,感谢您关注懒咪学编程c.lanmit.com.

本文地址:https://c.lanmit.com/bianchengkaifa/go/30643.html


http://chatgpt.dhexx.cn/article/fn2lpu6l.shtml

相关文章

Kratos日志

一.如何在kratos框架中使用 参考官方文档中描述,为了方便业务自适配不同的 log 接入使用,Logger 只包含了最简单的 Log 接口。当业务需要在 Kratos 框架内部使用自定义的 log的时候,只需要简单实现方法即可。日志库较为公用建议放在kit基础库…

kratos框架学习,在Linux 下面使用kratos 创建demo 项目然后跑起来。使用 kratos new kratos-demo 一键创建项目,脚手架非常方便。但是依赖grpc没有起来

目录 前言1,关于kratos2,使用 kratos new kratos-demo 一键创建项目3,解决gRPC 编译问题4,只创建HTTP服务 前言 本文的原文连接是: https://blog.csdn.net/freewebsys/article/details/109139648 未经博主允许不得转载。 博主地址…

砥砺前行 | Kratos 框架 v2 版本架构演进之路

Kratos 是一套轻量级 Go 微服务框架,包含大量微服务相关功能及工具。名字来源于游戏《战神》,该游戏以希腊神话为背景,讲述了奎托斯(Kratos)由凡人成为战神并展开弑神屠杀的冒险历程。 写在前面 从 2021 年 2 月份&…

【kratos入门实战教程】1-kratos项目搭建和开发环境配置

1、系列目录 【kratos入门实战教程】0-商城项目介绍【kratos入门实战教程】1-kratos项目搭建和开发环境配置【kratos入门实战教程】2-实现注册登陆业务 2、概览 经过上一篇的说明,本篇教程正式开始搭建项目。深入解析工程项目的目录结构和介绍一些开发需要使用的工…

go微服务框架Kratos简单使用总结

Kratos是B站开源的一款go的微服务框架,最近PS5上的 战神诸神黄昏比较火,主角就是奎托斯。这个框架的名字就取自他。 在进行框架选型时,对比了目前主流的很多go微服务框架,如Zero,最后对比之下,选择了Krato…

c语言代码后return0表示什么意思,C语言中return 0 表示什么

满意答案 强计划止步不.. 2013.01.03 采纳率:44% 等级:12 已帮助:7948人 return表示函数结束, 也就是说CPU执行到return后, 就会跳转(如果要好理解的话, 是"回到")到调用它的地方, 然后继续执行. 而0, 是一个整型, 一般来说retu…

c语言代码后return0表示什么意思,return 0是什么意思 ?

return 0 代表告诉调用者程序是正常退出的。return是C预定义的语句,它提供了终止函数执行的一种方式。当return语句提供了一个值时,这个值就成为函数的返回值。 这个return语句结束main()函数的执行,把0返还给操作系统。从main()函数返回0表…

接口性能测试方案

一、 性能测试术语解释 1. 响应时间 响应时间即从应用系统发出请求开始,到客户端接收到最后一个字节数据为止所消耗的时间。响应时间按软件的特点再可以细分,如对于一个 C/S 软件(B/S一样)的响应时间可以细分为网络传输时间、应…

软件性能测试方案-性能测试准备

性能测试目的 1、性能调优 开发人员对系统调优后,需要测试人员配合去做性能测试,验证这次优化是否有效果。如果性能指标相比较之前的性能指标更好了,说明系统优化的有效果。反之说明调优不理想 2、新业务、新接口上线 系统从无到上线&…

参考文档一:性能测试---测试方案

性能测试详细测试方案 前言 平台XX项目系统已经成功发布,依据项目的规划,未来势必会出现业务系统中信息大量增长的态势。 随着业务系统在生产状态下日趋稳定、成熟,系统的性能问题也逐步成为了我们关注的焦点:每天大数据量的“…

【性能测试】性能测试方案设计

性能测试方案设计 1. 性能测试流程 系统架构调研 业务模型分析调研 测试需求分析设计测试方案测试环境搭建测试数据准备测试工具开发测试场景设置执行场景测试测试结果分析提交测试报告 2. 性能测试需求分析和范围确定 收集性能需求信息分析应用程序 系统设计和架构信息网…

接口性能测试方案 白皮书 V1.0

一、 性能测试术语解释 1. 响应时间 响应时间即从应用系统发出请求开始,到客户端接收到最后一个字节数据为止所消耗的时间。响应时间按软件的特点再可以细分,如对于一个 C/S 软件的响应时间可以细分为网络传输时间、应用服务器处理时间、数据库服务器处…

性能测试案例

做性能测试之前需要对Linux内核参数优化 Linux内核参数优化 Linux服务器默认支持1024个TCP链接,在性能测试时,无论压力机还是项目服务器,都需要对tcp参数进行一些优化 ulimit -n:查看当前Linux系统最大的连接数 修改Linux系统允许…

性能测试(一)

一)谈谈你对于性能测试的理解: 1)性能测试的概念 测试目的与功能测试的区别性能测试的指标 2)性能测试需要借助工具来进行测试,可以说说自己是用了哪些工具以及如何使用工具来进行性能测试 3)为了避免面试官在性能测试方面进行深究,主动说性能测试难就难…

最全面性能测试方案模板

性能测试方案书 1 简介 1.1目的 编写本文档的目的在于描述测试项目的测试范围,定义测试条件和目标,测试策略和要求,分析可能的风险,提供相应的规避措施或应急对策,并确定测试整体进度的计划和人力资源安排等。 测试目…

性能测试介绍

转载来源 公众号《测试开发技术》 1.性能测试要开专栏了 提起性能测试,可能很多互联网从业人员会感觉比较混淆(不仅仅只是测试人员会弄混淆,很多开发人员、管理人员对性能测试也都是一知半解)。性能测试,它是属于测试…

性能测试流程-性能测试2

性能测试流程 1. 性能测试需求分析 2. 性能测试计划及方案 3. 性能测试用例 4. 搭建测试环境 5. 测试数据准备 6. 测试脚本编写 7. 执行测试脚本 8. 性能测试监控 9. 性能分析和调优 10. 性能测试回归 11. 性能测试报告总结 1、性能测试需求分析 熟悉被测系统 熟…

【测试方案】性能方案设计

什么是性能测试? 压力测试:强调极端暴力 稳定性测试:在一定压力下,长时间运行的情况 基准测试:在特定条件下的性能测试 负载测试:不同负载下的表现 容量测试:最优容量 性能测试方案设计流程 需…

性能测试—性能测试方案设计思路总结

一、需求分析 1.测试目的 为什么测?目的在于测试系统相关性能能否满足业务需求。通常分以下两种情况: 1)新项目上线 2)老项目优化 如果是老项目优化,可考虑是否存有历史测试方案,如果有可以参考&#…

性能测试计划及方案

说明:性能测试实施第一份文档,也是最重要的一份文档。 主要内容: 1、项目背景 项目是用来做什么的 2、测试目标 确定此次性能测试的目标,决定测试策略(下文) 3、人员安排 明确性能测试的时间&#xff0c…