Go微服务框架Kratos安装
.pb.go.bm.go--proto
// generate all
kratos tool protoc api.proto
// generate gRPC
kratos tool protoc --grpc api.proto
// generate BM HTTP
kratos tool protoc --bm api.proto
// generate swagger
kratos tool protoc --swagger api.proto
api.pb.go/api.bm.go/api.swagger.json

1.安装protoc二进制文件

下载地址:https://github.com/google/protobuf/releases
下载后解压如下

进入到解压后的文件夹,执行如下命令:

sudo mv include/google /usr/local/include/
kratos@kratos:~/protoc-3.11.2-linux-x86_64$ sudo mv include/google /usr/local/include/
[sudo] kratos 的密码:

2.代理设置

以下设置是临时设置,只在当前命令窗口管用,若新开命令窗口,还要在设置。也可以直接在系统中设置(自行网上搜索怎么设置)
1)、打开 go111module

export GO111MODULE=on

2)、以下代理建议用第一个

1、export GOPROXY=https://goproxy.cn/
2、export GOPROXY=https://goproxy.io/
3、export GOPROXY=https://mirrors.aliyun.com/goproxy/
4、export GOPROXY=https://athens.azurefd.net
go env

3.安装protobuf库文件

go get -u github.com/golang/protobuf/proto
kratos@kratos:~$ go get -u github.com/golang/protobuf/proto
go: finding github.com/golang/protobuf v1.3.2
go: downloading github.com/golang/protobuf v1.3.2
go: extracting github.com/golang/protobuf v1.3.2

4.安装goprotobuf插件

go get github.com/golang/protobuf/protoc-gen-go
kratos@kratos:~$ go get github.com/golang/protobuf/protoc-gen-go
kratos@kratos:~$ 

5.安装gogoprotobuf插件和依赖

//gogo
go get github.com/gogo/protobuf/protoc-gen-gogo
kratos@kratos:~$ go get github.com/gogo/protobuf/protoc-gen-gogo
go: finding github.com/gogo/protobuf v1.3.1
go: downloading github.com/gogo/protobuf v1.3.1
go: extracting github.com/gogo/protobuf v1.3.1
//gofast
go get github.com/gogo/protobuf/protoc-gen-gofast
kratos@kratos:~$ go get github.com/gogo/protobuf/protoc-gen-gofast
kratos@kratos:~$ 
//依赖
go get github.com/gogo/protobuf/proto
go get github.com/gogo/protobuf/gogoproto
kratos@kratos:~$ go get github.com/gogo/protobuf/proto
kratos@kratos:~$ go get github.com/gogo/protobuf/gogoproto
kratos@kratos:~$ 

6.安装框架依赖

# grpc (或者git clone https://github.com/grpc/grpc-go 然后复制到google.golang.org/grpc)
go get -u google.golang.org/grpc
kratos@kratos:~$ go get -u google.golang.org/grpc
go: finding google.golang.org/grpc v1.26.0
go: downloading google.golang.org/grpc v1.26.0
go: extracting google.golang.org/grpc v1.26.0
go: downloading golang.org/x/net v0.0.0-20190311183353-d8887717615a
go: downloading google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55
go: downloading golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
go: extracting golang.org/x/net v0.0.0-20190311183353-d8887717615a
go: downloading golang.org/x/text v0.3.0
go: extracting golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
go: extracting google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55
go: extracting golang.org/x/text v0.3.0
go: finding golang.org/x/sys latest
go: finding golang.org/x/net latest
go: finding google.golang.org/genproto latest
go: finding golang.org/x/text v0.3.2
go: downloading golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553
go: downloading golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e
go: downloading google.golang.org/genproto v0.0.0-20191230161307-f3c370f40bfb
go: extracting golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e
go: extracting golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553
go: downloading golang.org/x/text v0.3.2
go: extracting google.golang.org/genproto v0.0.0-20191230161307-f3c370f40bfb
go: extracting golang.org/x/text v0.3.2
# genproto (或者git clone https://github.com/google/go-genproto 然后复制到google.golang.org/genproto)
go get google.golang.org/genproto/...
kratos@kratos:~$ go get google.golang.org/genproto/...
go: finding google.golang.org/genproto latest
go: downloading google.golang.org/grpc v1.19.0
go: downloading honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099
go: downloading golang.org/x/tools v0.0.0-20190226205152-f727befe758c
go: downloading golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961
go: downloading golang.org/x/exp v0.0.0-20190121172915-509febef88a4
go: extracting golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961
go: extracting honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099
go: extracting google.golang.org/grpc v1.19.0
go: downloading golang.org/x/net v0.0.0-20190213061140-3a22650c66bd
go: downloading github.com/BurntSushi/toml v0.3.1
go: downloading golang.org/x/sys v0.0.0-20180830151530-49385e6e1522
go: extracting golang.org/x/exp v0.0.0-20190121172915-509febef88a4
go: extracting github.com/BurntSushi/toml v0.3.1
go: extracting golang.org/x/tools v0.0.0-20190226205152-f727befe758c
go: extracting golang.org/x/sys v0.0.0-20180830151530-49385e6e1522
go: extracting golang.org/x/net v0.0.0-20190213061140-3a22650c66bd
go: finding google.golang.org/grpc v1.19.0
go: finding golang.org/x/net v0.0.0-20190213061140-3a22650c66bd
go: finding golang.org/x/sys v0.0.0-20180830151530-49385e6e1522
go: finding golang.org/x/text v0.3.0

7.安装kratos tool

go get -u github.com/bilibili/kratos/tool/kratos
kratos@kratos:~$ go get -u github.com/bilibili/kratos/tool/kratos
go: finding github.com/bilibili/kratos v0.3.1
go: downloading github.com/bilibili/kratos v0.3.1
go: extracting github.com/bilibili/kratos v0.3.1
go: downloading github.com/urfave/cli v1.22.1
go: downloading github.com/fatih/color v1.7.0
go: extracting github.com/fatih/color v1.7.0
go: downloading github.com/mattn/go-isatty v0.0.10
go: downloading github.com/mattn/go-colorable v0.1.4
go: extracting github.com/urfave/cli v1.22.1
go: downloading github.com/cpuguy83/go-md2man v1.0.10
go: extracting github.com/mattn/go-colorable v0.1.4
go: extracting github.com/cpuguy83/go-md2man v1.0.10
go: extracting github.com/mattn/go-isatty v0.0.10
go: downloading github.com/cpuguy83/go-md2man/v2 v2.0.0
go: downloading golang.org/x/sys v0.0.0-20191105231009-c1f44814a5cd
go: extracting github.com/cpuguy83/go-md2man/v2 v2.0.0
go: downloading github.com/russross/blackfriday v1.5.2
go: extracting golang.org/x/sys v0.0.0-20191105231009-c1f44814a5cd
go: extracting github.com/russross/blackfriday v1.5.2
go: downloading github.com/russross/blackfriday/v2 v2.0.1
go: extracting github.com/russross/blackfriday/v2 v2.0.1
go: downloading github.com/shurcooL/sanitized_anchor_name v1.0.0
go: extracting github.com/shurcooL/sanitized_anchor_name v1.0.0
go: finding golang.org/x/sys latest
go: finding github.com/fatih/color v1.8.0
go: finding github.com/mattn/go-isatty v0.0.11
go: finding github.com/urfave/cli v1.22.2
go: finding github.com/cpuguy83/go-md2man/v2 v2.0.0
go: finding github.com/shurcooL/sanitized_anchor_name v1.0.0
go: finding github.com/mattn/go-colorable v0.1.4
go: finding github.com/russross/blackfriday/v2 v2.0.1
go: downloading github.com/urfave/cli v1.22.2
go: downloading github.com/fatih/color v1.8.0
go: extracting github.com/fatih/color v1.8.0
go: downloading github.com/mattn/go-isatty v0.0.11
go: extracting github.com/mattn/go-isatty v0.0.11
go: extracting github.com/urfave/cli v1.22.2

若go目录下没有src目录,则创建src目录

进入到之前解压后的文件夹,执行如下命令:

mv bin/protoc /usr/local/bin/
kratos@kratos:~/protoc-3.11.2-linux-x86_64$ sudo mv bin/protoc /usr/local/bin/
[sudo] kratos 的密码:
cd $GOPATH/src
kratos new kratos-demo  或者  kratos new kratos-demo --proto
新建kratos脚手架

kratos new是快速创建一个项目的命令,执行如下:

kratos new kratos-demo

即可快速在当前目录生成一个叫kratos-demo的项目。此外还支持指定owner和path,如下:

kratos new kratos-demo -o YourName -d YourPath
kratos new
kratos new kratos-demo -o YourName -d YourPath --proto
kratos new
kratos@kratos:~/go/src$ kratos new kratos-demo --proto
go get -u github.com/bilibili/kratos/tool/kratos-gen-project
go: downloading github.com/gobuffalo/packr/v2 v2.7.1
go: extracting github.com/gobuffalo/packr/v2 v2.7.1
go: downloading github.com/gobuffalo/envy v1.7.1
go: downloading github.com/sirupsen/logrus v1.4.2
go: downloading github.com/gobuffalo/packd v0.3.0
go: downloading github.com/gobuffalo/logger v1.0.1
go: extracting github.com/gobuffalo/logger v1.0.1
go: extracting github.com/gobuffalo/packd v0.3.0
go: downloading golang.org/x/crypto v0.0.0-20191105034135-c7e5f84aec59
go: extracting github.com/sirupsen/logrus v1.4.2
go: downloading github.com/konsorten/go-windows-terminal-sequences v1.0.2
go: extracting github.com/gobuffalo/envy v1.7.1
go: downloading github.com/joho/godotenv v1.3.0
go: downloading github.com/rogpeppe/go-internal v1.5.0
go: extracting github.com/konsorten/go-windows-terminal-sequences v1.0.2
go: extracting github.com/joho/godotenv v1.3.0
go: extracting github.com/rogpeppe/go-internal v1.5.0
go: extracting golang.org/x/crypto v0.0.0-20191105034135-c7e5f84aec59
go: finding golang.org/x/crypto latest
go: finding github.com/gobuffalo/packd v0.3.0
go: finding golang.org/x/sys latest
go: finding github.com/konsorten/go-windows-terminal-sequences v1.0.2
go: finding github.com/rogpeppe/go-internal v1.5.1
go: finding github.com/sirupsen/logrus v1.4.2
go: finding github.com/gobuffalo/logger v1.0.3
go: finding github.com/joho/godotenv v1.3.0
go: finding github.com/gobuffalo/envy v1.8.1
go: finding github.com/gobuffalo/packr/v2 v2.7.1
go: downloading github.com/gobuffalo/logger v1.0.3
go: downloading github.com/gobuffalo/envy v1.8.1
go: extracting github.com/gobuffalo/logger v1.0.3
go: extracting github.com/gobuffalo/envy v1.8.1
go: downloading golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876
go: downloading github.com/rogpeppe/go-internal v1.5.1
go: extracting github.com/rogpeppe/go-internal v1.5.1
go: extracting golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876
genproject: 安装成功!
go: finding github.com/bilibili/kratos master
go: downloading github.com/bilibili/kratos v0.3.2-0.20191224125553-6e1180f53a8e
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.20191224125553-6e1180f53a8e
go: downloading golang.org/x/net v0.0.0-20191011234655-491137f69257
go: downloading github.com/fsnotify/fsnotify v1.4.7
go: downloading gopkg.in/go-playground/validator.v9 v9.29.1
go: downloading google.golang.org/genproto v0.0.0-20191216205247-b31c10ee225f
go: downloading github.com/gogo/protobuf v1.3.0
go: downloading github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2
go: downloading github.com/prometheus/client_golang v1.1.0
go: extracting github.com/fsnotify/fsnotify v1.4.7
go: downloading github.com/go-sql-driver/mysql v1.4.1
go: extracting golang.org/x/net v0.0.0-20191011234655-491137f69257
go: extracting gopkg.in/go-playground/validator.v9 v9.29.1
go: downloading github.com/go-playground/universal-translator v0.16.0
go: extracting github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2
go: downloading github.com/leodido/go-urn v1.1.0
go: downloading gopkg.in/yaml.v2 v2.2.2
go: extracting github.com/prometheus/client_golang v1.1.0
go: downloading github.com/pkg/errors v0.8.1
go: downloading github.com/prometheus/common v0.6.0
go: extracting github.com/pkg/errors v0.8.1
go: extracting github.com/leodido/go-urn v1.1.0
go: downloading github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90
go: extracting github.com/go-playground/universal-translator v0.16.0
go: extracting github.com/go-sql-driver/mysql v1.4.1
go: extracting gopkg.in/yaml.v2 v2.2.2
go: extracting github.com/prometheus/common v0.6.0
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/shirou/gopsutil v2.19.6+incompatible
go: downloading github.com/go-playground/locales v0.12.1
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
go: downloading github.com/prometheus/procfs v0.0.3
go: extracting github.com/gogo/protobuf v1.3.0
go: extracting github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90
go: extracting github.com/prometheus/procfs v0.0.3
go: extracting github.com/beorn7/perks v1.0.1
go: extracting github.com/matttproud/golang_protobuf_extensions v1.0.1
go: extracting github.com/shirou/gopsutil v2.19.6+incompatible
go: extracting google.golang.org/genproto v0.0.0-20191216205247-b31c10ee225f
go: extracting github.com/go-playground/locales v0.12.1
go get -u github.com/bilibili/kratos/tool/kratos-protoc
protoc: 安装成功!
2020/01/07 14:32:34 go get -u github.com/bilibili/kratos/tool/protobuf/protoc-gen-bm
go: downloading github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726
go: extracting github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726
go: finding github.com/pkg/errors v0.8.1
go: finding google.golang.org/genproto latest
go: finding github.com/siddontang/go latest
2020/01/07 14:32:37 protoc --proto_path=/home/kratos/go/src --proto_path=/home/kratos/go/pkg/mod/github.com/bilibili/kratos@v0.3.2-0.20191224125553-6e1180f53a8e/third_party --proto_path=/home/kratos/go/src/kratos-demo/api --bm_out=:. api.proto
2020/01/07 14:32:37 protoc --proto_path=/home/kratos/go/src --proto_path=/home/kratos/go/pkg/mod/github.com/bilibili/kratos@v0.3.2-0.20191224125553-6e1180f53a8e/third_party --proto_path=/home/bqpbqt/go/src/kratos-demo/api --gofast_out=plugins=grpc:. api.proto
2020/01/07 14:32:37 generate api.proto success.
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 github.com/google/subcommands v1.0.1
go: downloading golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b
go: downloading github.com/pmezard/go-difflib v1.0.0
go: extracting github.com/google/subcommands v1.0.1
go: extracting github.com/pmezard/go-difflib v1.0.0
go: extracting golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b
go: finding golang.org/x/tools latest
go: finding github.com/google/subcommands v1.0.1
go: finding github.com/pmezard/go-difflib v1.0.0
go: downloading golang.org/x/tools v0.0.0-20200107050322-53017a39ae36
go: extracting golang.org/x/tools v0.0.0-20200107050322-53017a39ae36
wire: 安装成功!
wire: go [list -e -json -compiled=true -test=false -export=false -deps=true -find=false -tags=wireinject -- .]: exit status 2: go: finding gopkg.in/go-playground/validator.v9 v9.29.1
	go: finding github.com/gogo/protobuf v1.3.0
	go: finding github.com/prometheus/client_golang v1.1.0
	go: finding github.com/go-playground/universal-translator v0.16.0
	go: finding github.com/BurntSushi/toml v0.3.1
	go: finding google.golang.org/grpc v1.24.0
	go: finding github.com/fsnotify/fsnotify v1.4.7
	go: finding github.com/go-playground/locales v0.12.1
	go: finding gopkg.in/yaml.v2 v2.2.2
	go: finding github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2
	go: finding github.com/leodido/go-urn v1.1.0
	go: finding github.com/beorn7/perks v1.0.1
	go: finding github.com/go-sql-driver/mysql v1.4.1
	go: finding golang.org/x/sys v0.0.0-20191105231009-c1f44814a5cd
	go: finding github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90
	go: finding github.com/prometheus/common v0.6.0
	go: finding golang.org/x/net v0.0.0-20191011234655-491137f69257
	go: finding github.com/prometheus/procfs v0.0.3
	go: finding github.com/shirou/gopsutil v2.19.6+incompatible
	go: finding github.com/matttproud/golang_protobuf_extensions v1.0.1
	# runtime/cgo
	exec: "gcc": executable file not found in $PATH
	
wire: generate failed
exit status 1
Project: kratos-demo
OnlyGRPC: false
OnlyHTTP: false
Directory: /home/kratos/go/src/kratos-demo

项目创建成功.
wire: generate failedkratos-demokratos-demokratos-demo
kratos new kratos-demo --proto
kratos@kratos:~/go/src$ kratos new kratos-demo --proto
go: finding github.com/bilibili/kratos master
2020/01/07 14:34:43 protoc --proto_path=/home/kratos/go/src --proto_path=/home/kratos/go/pkg/mod/github.com/bilibili/kratos@v0.3.2-0.20191224125553-6e1180f53a8e/third_party --proto_path=/home/kratos/go/src/kratos-demo/api --bm_out=:. api.proto
2020/01/07 14:34:43 protoc --proto_path=/home/kratos/go/src --proto_path=/home/kratos/go/pkg/mod/github.com/bilibili/kratos@v0.3.2-0.20191224125553-6e1180f53a8e/third_party --proto_path=/home/kratos/go/src/kratos-demo/api --gofast_out=plugins=grpc:. api.proto
2020/01/07 14:34:43 generate api.proto success.
Close: 无声明 忽略此方法
Ping: 无声明 忽略此方法
dao.bts.go: 生成成功
mc.cache.go: 生成成功
wire: kratos-demo/internal/di: wrote /home/kratos/go/src/kratos-demo/internal/di/wire_gen.go
Project: kratos-demo
OnlyGRPC: false
OnlyHTTP: false
Directory: /home/kratos/go/src/kratos-demo

项目创建成功.

8.运行

kratos run
cd kratos-demo
kratos run
kratos@kratos:~/go/src/kratos-demo$ kratos run
INFO 01/07-14:51:27.953 /home/kratos/go/src/kratos-demo/cmd/main.go:19 abc start
2020/01/07 14:51:27 start watch filepath: /home/kratos/go/src/kratos-demo/configs
INFO 01/07-14:51:27.954 /home/kratos/go/pkg/mod/github.com/bilibili/kratos@v0.3.2-0.20191224125553-6e1180f53a8e/pkg/net/http/blademaster/server.go:98 blademaster: start http listen addr: 0.0.0.0:8000
[warden] config is Deprecated, argument will be ignored. please use -grpc flag or GRPC env to configure warden server.
INFO 01/07-14:51:27.954 /home/kratos/go/pkg/mod/github.com/bilibili/kratos@v0.3.2-0.20191224125553-6e1180f53a8e/pkg/net/rpc/warden/server.go:329 warden: start grpc listen addr: [::]:9000
go build
cd kratos-demo/cmd
go build
./cmd -conf ../configs
kratos@kratos:~/go/src/kratos-demo$ cd cmd/
kratos@kratos:~/go/src/kratos-demo/cmd$ ls
main.go
kratos@kratos:~/go/src/kratos-demo/cmd$ go build
kratos@kratos:~/go/src/kratos-demo/cmd$ ls
cmd  main.go
kratos@kratos:~/go/src/kratos-demo/cmd$ ./cmd -conf ../configs
INFO 01/07-15:00:46.302 /home/kratos/go/src/kratos-demo/cmd/main.go:19 abc start
INFO 01/07-15:00:46.302 /home/kratos/go/pkg/mod/github.com/bilibili/kratos@v0.3.2-0.20191224125553-6e1180f53a8e/pkg/net/http/blademaster/server.go:98 blademaster: start http listen addr: 0.0.0.0:8000
[warden] config is Deprecated, argument will be ignored. please use -grpc flag or GRPC env to configure warden server.
INFO 01/07-15:00:46.302 /home/kratos/go/pkg/mod/github.com/bilibili/kratos@v0.3.2-0.20191224125553-6e1180f53a8e/pkg/net/rpc/warden/server.go:329 warden: start grpc listen addr: [::]:9000
2020/01/07 15:00:46 start watch filepath: ../configs
http://localhost:8000/abc/starthttp://192.168.3.179:8000/abc/start