kratos微服务框架学习笔记一(kratos-demo)
kratos微服务框架学习笔记一(kratos-demo)
TAG:本系列笔记以demo为主,适合微服务初学者入门,如果有地方我没具体写的话,那肯定是我也没去看,一笔带过了,所以很多细节可能还是需要自行研究哦! 补的话,得看时机,除非不恰饭哈。
常见微服务框架主要有这么几个
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本体
先拉代码

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

记得打开 go111module

kratos本体
类似于go命令,可以执行各种子工具,如go build、 go tool : kratos build 、kratos run
C:\server\src\test-src\Go_Test\kratos>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:\server\src\test-src\Go_Test\kratos>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:\server\src\go\src>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:\server\src\go\src\demo\internal\di\wire.go:17:65: DemoServer not declared by package api
wire: generate failed
exit status 1
Project: demo
OnlyGRPC: false
OnlyHTTP: false
Directory: C:\server\src\go\src\demo
项目创建成功.
其中提示了没安装protobuf,需要手动安装,先不管他直接跑demo试试。
2019/12/18 15:38:39 您还没安装protobuf,请进行手动安装:https://github.com/protocolbuffers/protobuf/releases
kratos run
C:\server\src\go\src\demo>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:\server\src\go\src\demo>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:\server\src\go\src\demo>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
..\api\client.go:15:68: undefined: DemoClient
..\api\client.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.
@<filename> 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
@<filename> 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目录重新创建:
这次能够运行成功了

先试试文档给的路径:

不行, 看看源码:
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
成功了:

kratos微服务框架学习笔记一(kratos-demo)的更多相关文章
- [goa]golang微服务框架学习--安装使用
当项目逐渐变大之后,服务增多,开发人员增加,单纯的使用go来写服务会遇到风格不统一,开发效率上的问题. 之前研究go的微服务架构go-kit最让人头疼的就是定义服务之后,还要写很多重复的框架代码, ...
- Spring Cloud 微服务架构学习笔记与示例
本文示例基于Spring Boot 1.5.x实现,如对Spring Boot不熟悉,可以先学习我的这一篇:<Spring Boot 1.5.x 基础学习示例>.关于微服务基本概念不了解的 ...
- [goa]golang微服务框架学习(二)-- 代码自动生成
之前用过go语言的反射来做一些代码生成,参考这篇. 但是这种方式,入侵太强,需要执行对应的申明调用, 所以对GOA框架的自动生成非常感兴趣,于是仔细研究了一下,发现用的比较巧妙, 这里先卖个关子,先看 ...
- 微服务框架学习二:Http调用
1. HTTP接口的意义 二进制接口使用的是java/hessian序列化协议,不能很好的与其他语言通信,虽然hessian也是一种跨语言的通用协议,但很多语言没有很好的实现该协议的产品.所以为了能够 ...
- [goa]golang微服务框架学习(三)-- 使用swagger-ui展示API
既然goa框架自动生成啦swagger-json文件,那么如何用swagger-ui展示出来呢? 这里分三步: 1.下载swagger-ui的web代码 2.添加swagger.json 和 swag ...
- Sping Cloud 微服务框架学习
Spring Cloud官方中文站 https://springcloud.cc
- .NET Core微服务架构学习与实践系列文章索引目录
一.为啥要总结和收集这个系列? 今年从原来的Team里面被抽出来加入了新的Team,开始做Java微服务的开发工作,接触了Spring Boot, Spring Cloud等技术栈,对微服务这种架构有 ...
- go微服务框架kratos学习笔记五(kratos 配置中心 paladin config sdk [断剑重铸之日,骑士归来之时])
目录 go微服务框架kratos学习笔记五(kratos 配置中心 paladin config sdk [断剑重铸之日,骑士归来之时]) 静态配置 flag注入 在线热加载配置 远程配置中心 go微 ...
- # go微服务框架kratos学习笔记六(kratos 服务发现 discovery)
目录 go微服务框架kratos学习笔记六(kratos 服务发现 discovery) http api register 服务注册 fetch 获取实例 fetchs 批量获取实例 polls 批 ...
随机推荐
- MySQL/MariaDB读写分离配置
DB读写分离描述 数据库的读写分离其实就是为了加减少数据库的压力:数据库的写入操作由主数据库来进行,读取操作由从数据库来进行操作.实现数据库读写分离技术是有很多方法的,在这里我就用一个比较简单的mys ...
- Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源
本文是 Spring Boot 整合数据持久化方案的最后一篇,主要和大伙来聊聊 Spring Boot 整合 Jpa 多数据源问题.在 Spring Boot 整合JbdcTemplate 多数据源. ...
- 力扣(LeetCode)位1的个数 个人题解
编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量). 示例 1: 输入:00000000000000000000000000001011 输出:3 ...
- 虚拟机和容器docker
云计算中最主要的技术就是虚拟机,开源虚拟机已经kvm已经集成到Linux内核!针对虚拟机浪费资源(CPU.内存.存储等)较大的缺陷,google力推Docker容器和容器管理平台Kubernetes. ...
- Java内存模型与volatile关键字
Java内存模型与volatile关键字 一).并发程序开发 并行程序的开发要涉及多线程.多任务间的协作和数据共享问题. 常用的并发控制:内部锁.重入锁.读写锁.信号量. 二).线程的特点 线程的特点 ...
- Js获取宽高度的归纳总结
首先,先吓唬一下我们的小白们!在js中的描述宽高的可以细分有22种. window.innerWidth //除去菜单栏的窗口宽度 window.innerHeight//除去菜单栏的窗口高度 win ...
- [ch03-01] 均方差损失函数
系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 3.1 均方差函数 MSE - Mean Square ...
- 【Luogu P1878】舞蹈课
Luogu P1878 事实上这道题并不难,但我真没弄懂我手写堆为什么过不了.所以 STL大法好!!! 基本思路 对于每一对相邻异性,将他们的舞蹈技术的差插入一个堆 通过维护这个小根堆,每次就可以取得 ...
- Sql server中用现有表中的数据创建Sql的Insert插入语句
之前,在Codeproject发表过一篇关于用现有表中数据创建Insert的Sql语句的存储过程,今天将其搬到这里来,注意本存储过程仅适用于SQL SERVER. 介绍 一些时候,你想导出一些现有表中 ...
- Solr搜索引擎【索引提交、事务日志、原子更新】
一.索引提交 当一个文档被添加到Solr中,但没有提交给索引之前,这个文档是无法被搜索的.换句话说,从查询的角度看,文档直到提交之后才是可见的.Solr有两种类型的提交:软提交和正常提交[也称硬提交] ...