kratos micro service framework learning note 1 (kratos demo)
Most of the time this year has passed. It's not much more extensive and github. Now the development task is almost completed, which will be easier. Considering the future development, I plan to take a look at the microservice framework.
There are several common microservice frameworks
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, an open-source Go microservice framework of bilibilibili, contains a large number of microservice related frameworks and tools.
Here is a comparison of kit, go mirco, gizmo and kite:
I plan to see kratos as the first frame, and I don't know why, because I like b station.
Introduction to kratos on github:
The name comes from: the game of God of war is based on Greek mythology. It tells the adventures of Kratos, who became God of war from mortals, becoming God of war and killing gods.
target
Committed to providing a complete microservice R & D experience and integrating relevant frameworks and tools, the relevant parts of microservice governance can be insensitive to the overall business development cycle, thus focusing more on business delivery. For each developer, the Kratos framework is also a good learning warehouse. You can learn and refer to bilibili bili's technology accumulation and experience in micro services.
Features
HTTP Blademaster: the core is modular design based on gin, simple and easy to use, and the core is light enough;
GRPC Warden: Based on official gRPC development, it integrates discovery service discovery and P2C load balancing;
Cache: elegant interface design, very convenient cache serialization. It is recommended to combine the proxy mode overload;
Database: integrate MySQL/HBase/TiDB, add fuse protection and statistical support, and quickly discover data layer pressure;
Config: easy to use paladin sdk, which can cooperate with remote configuration center to realize configuration version management and update;
Log: a field similar to zap realizes high-performance log library, and realizes remote log management with log agent;
Trace: Based on opentracing, integrated full link trace support (gRPC/HTTP/MySQL/Redis/Memcached);
Kratos Tool: tool chain, which can quickly generate standard projects, or generate code through Protobuf. It is very convenient to use gRPC, HTTP, swagger documents;
kratos Ontology
Pull code first
If it doesn't work, you can try to configure the agent
Remember to open go111 module
kratos Ontology
Similar to the go command, you can execute various sub tools, such as go build, go tool: Kratos build, kratos run
C:\server\src\test-src\Go_Test\kratos>kratos -h NAME: kratos - kratos Content USAGE: kratos [global options] command [command options] [arguments...] VERSION: 0.3.1 COMMANDS: new, n Create a new project 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
Through kratos new, scaffolding code based on kratos library will be generated quickly, such as kratos demo
Build & Run
cd kratos-demo/cmd
go build
./cmd -conf ../configs
Open the browser and visit: http: / / localhost: 8000 / Kratos demo / start, you will see the output of gorang!!!
The official introduction is a little simple. Let's go step by step:
Go to the $GOPATH/src directory first
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: Installation successful! 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: Installation successful! 2019/12/18 15:38:39 You haven't installed it yet protobuf,Please install manually: https://github.com/protocolbuffers/protobuf/releases exit status 1 go get -u github.com/bilibili/kratos/tool/kratos-gen-bts genbts: Installation successful! Close: Ignore this method without declaration Ping: Ignore this method without declaration dao.bts.go: Build successful go get -u github.com/bilibili/kratos/tool/kratos-gen-mc genmc: Installation successful! mc.cache.go: Build successful 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: Installation successful! 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 //Project created successfully
It indicates that protobuf is not installed and needs to be installed manually. First, run demo to try.
2019 / 12 / 18 15:38:39 you haven't installed protobuf, please install it manually: 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
report errors:
cannot load github.com/shirou/gopsutil
It seems that the gopsutil package cannot be loaded
Just try it:
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
It's ready to run, but it doesn't run. The api path seems to be related to protobuf, so try installing a protobuf.
https://github.com/protocolbuffers/protobuf/releases
Download. I'm a windows system. Just add the bin directory environment variable in path.
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.
To ensure that protoc can execute, delete the demo directory and recreate it:
It's working this time
First try the path given by the document:
No, look at the source code:
blademaster should be the cutting of gin framework
See what the demo service does,
// 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) } }
Added / abc Group, registered / start route
// 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, } }
See the introduction, which roughly means to create a routing group for routes using the same middleware and path prefix.
Try the path
/abc/start
succeed: