目录
- 前言
- 1,关于kratos
- 2,使用 kratos new kratos-demo 一键创建项目
- 3,解决gRPC 编译问题
- 4,只创建HTTP服务
前言
本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/109139648
未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys
1,关于kratos
Kratos是bilibili开源的一套Go微服务框架,包含大量微服务相关框架及工具。
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文档;
项目地址,:
https://github.com/go-kratos/kratos
工具地址:
2,使用 kratos new kratos-demo 一键创建项目
首先需要安装 golang ,这个不用说。
这里需要设置下goproxy。
https://goproxy.cn/
$ echo "export GO111MODULE=on" >> ~/.bashrc
$ echo "export GOPROXY=https://goproxy.cn" >> ~/.bashrc
$ source ~/.bashrc
然后再执行
GO111MODULE=on && go get -u github.com/go-kratos/kratos/tool/kratos
cd $GOPATH/src
kratos new kratos-demo
配置好代理之后嗖嗖的下载,比阿里云的源好用。没有404情况。
$ kratos new kratos-demo
go get -u github.com/go-kratos/kratos/tool/kratos-gen-project@v0.5.0
go: finding github.com v0.5.0
go: finding github.com/go-kratos/kratos/tool/kratos-gen-project v0.5.0
go: finding github.com/go-kratos v0.5.0
go: finding github.com/go-kratos/kratos/tool v0.5.0
go: finding golang.org/x/sys latest
go: finding golang.org/x/crypto latest
go: finding github.com/joho/godotenv v1.3.0
go: finding github.com/gobuffalo/envy v1.9.0
go: downloading github.com/gobuffalo/packr/v2 v2.8.0
go: extracting github.com/gobuffalo/packr/v2 v2.8.0
go: downloading github.com/gobuffalo/packd v1.0.0
go: downloading github.com/markbates/oncer v1.0.0
go: downloading github.com/markbates/errx v1.1.0
go: downloading github.com/karrick/godirwalk v1.15.3
go: downloading github.com/sirupsen/logrus v1.7.0
go: downloading github.com/markbates/safe v1.0.1
go: downloading github.com/gobuffalo/logger v1.0.3
go: extracting github.com/sirupsen/logrus v1.7.0
go: extracting github.com/markbates/errx v1.1.0
go: extracting github.com/markbates/oncer v1.0.0
go: extracting github.com/markbates/safe v1.0.1
go: extracting github.com/karrick/godirwalk v1.15.3
go: extracting github.com/gobuffalo/logger v1.0.3
go: downloading golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
go: extracting github.com/gobuffalo/packd v1.0.0
go: extracting golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
go: finding github.com/markbates/oncer v1.0.0
go: finding github.com/karrick/godirwalk v1.16.1
go: finding github.com/markbates/errx v1.1.0
go: finding github.com/markbates/safe v1.0.1
go: downloading github.com/karrick/godirwalk v1.16.1
go: extracting github.com/karrick/godirwalk v1.16.1
genproject: 安装成功!
go: finding github.com/go-kratos/kratos master
# get https://goproxy.cn/github.com/go-kratos/kratos/@v/master.info
# get https://goproxy.cn/github.com/go-kratos/kratos/@v/master.info: 200 OK (6.058s)
# get https://goproxy.cn/github.com/go-kratos/kratos/@v/v0.5.1-0.20201016144826-bc827de8b153.mod
# get https://goproxy.cn/golang.org/x/net/@v/v0.0.0-20190628185345-da137c7871d7.mod
# get https://goproxy.cn/golang.org/x/net/@v/v0.0.0-20190628185345-da137c7871d7.mod: 200 OK (0.038s)
# get https://goproxy.cn/sumdb/sum.golang.org/supported
# get https://goproxy.cn/github.com/go-kratos/kratos/@v/v0.5.1-0.20201016144826-bc827de8b153.mod: 200 OK (0.040s)
# get https://goproxy.cn/sumdb/sum.golang.org/supported: 200 OK (0.023s)
# get https://goproxy.cn/sumdb/sum.golang.org/lookup/golang.org/x/net@v0.0.0-20190628185345-da137c7871d7
# get https://goproxy.cn/sumdb/sum.golang.org/lookup/github.com/go-kratos/kratos@v0.5.1-0.20201016144826-bc827de8b153
# get https://goproxy.cn/sumdb/sum.golang.org/lookup/github.com/go-kratos/kratos@v0.5.1-0.20201016144826-bc827de8b153: 200 OK (0.034s)
# get https://goproxy.cn/sumdb/sum.golang.org/tile/8/0/x006/749
# get https://goproxy.cn/sumdb/sum.golang.org/lookup/golang.org/x/net@v0.0.0-20190628185345-da137c7871d7: 200 OK (0.054s)
# get https://goproxy.cn/sumdb/sum.golang.org/tile/8/0/x006/749: 200 OK (0.024s)
# get https://goproxy.cn/sumdb/sum.golang.org/tile/8/0/156
# get https://goproxy.cn/sumdb/sum.golang.org/tile/8/0/x006/741
# get https://goproxy.cn/sumdb/sum.golang.org/tile/8/0/x006/741: 200 OK (0.026s)
# get https://goproxy.cn/sumdb/sum.golang.org/tile/8/0/156: 200 OK (0.047s)
go: downloading github.com/go-kratos/kratos v0.5.1-0.20201016144826-bc827de8b153
# get https://goproxy.cn/github.com/go-kratos/kratos/@v/v0.5.1-0.20201016144826-bc827de8b153.zip
# get https://goproxy.cn/github.com/@v/list
# get https://goproxy.cn/github.com/google/wire/@v/list
# get https://goproxy.cn/github.com/google/@v/list
go: downloading google.golang.org/grpc v1.28.1
go: downloading github.com/golang/protobuf v1.3.5
# get https://goproxy.cn/google.golang.org/grpc/@v/v1.28.1.zip
# get https://goproxy.cn/github.com/golang/protobuf/@v/v1.3.5.zip
# get https://goproxy.cn/github.com/go-kratos/kratos/@v/v0.5.1-0.20201016144826-bc827de8b153.zip: 200 OK (0.049s)
# get https://goproxy.cn/github.com/golang/protobuf/@v/v1.3.5.zip: 200 OK (0.834s)
# get https://goproxy.cn/google.golang.org/grpc/@v/v1.28.1.zip: 200 OK (0.834s)
# get https://goproxy.cn/github.com/google/wire/@v/list: 200 OK (0.854s)
# get https://goproxy.cn/github.com/@v/list: 404 Not Found (0.857s)
# get https://goproxy.cn/github.com/google/@v/list: 404 Not Found (0.854s)
go: finding github.com/google/wire v0.4.0
# get https://goproxy.cn/github.com/google/wire/@v/v0.4.0.info
# get https://goproxy.cn/github.com/google/wire/@v/v0.4.0.info: 200 OK (0.387s)
go: downloading github.com/google/wire v0.4.0
# get https://goproxy.cn/github.com/google/wire/@v/v0.4.0.zip
# get https://goproxy.cn/github.com/google/wire/@v/v0.4.0.zip: 200 OK (0.367s)
# get https://goproxy.cn/sumdb/sum.golang.org/lookup/github.com/google/wire@v0.4.0
go: extracting github.com/golang/protobuf v1.3.5
# get https://goproxy.cn/sumdb/sum.golang.org/lookup/github.com/google/wire@v0.4.0: 200 OK (0.094s)
# get https://goproxy.cn/sumdb/sum.golang.org/tile/8/0/x006/748
# get https://goproxy.cn/sumdb/sum.golang.org/tile/8/0/x006/748: 200 OK (0.028s)
# get https://goproxy.cn/sumdb/sum.golang.org/tile/8/0/x002/080
go: extracting google.golang.org/grpc v1.28.1
go: extracting github.com/go-kratos/kratos v0.5.1-0.20201016144826-bc827de8b153
# get https://goproxy.cn/sumdb/sum.golang.org/tile/8/0/x002/080: 200 OK (0.093s)
go: extracting github.com/google/wire v0.4.0
go: downloading golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
# get https://goproxy.cn/golang.org/x/net/@v/v0.0.0-20200324143707-d3edc9973b7e.zip
go: downloading github.com/shirou/gopsutil v2.19.11+incompatible
# get https://goproxy.cn/github.com/shirou/gopsutil/@v/v2.19.11+incompatible.zip
go: downloading google.golang.org/genproto v0.0.0-20200402124713-8ff61da6d932
# get https://goproxy.cn/google.golang.org/genproto/@v/v0.0.0-20200402124713-8ff61da6d932.zip
# get https://goproxy.cn/golang.org/x/net/@v/v0.0.0-20200324143707-d3edc9973b7e.zip: 200 OK (0.041s)
go: downloading github.com/prometheus/client_golang v1.5.1
# get https://goproxy.cn/github.com/prometheus/client_golang/@v/v1.5.1.zip
go: downloading gopkg.in/go-playground/validator.v9 v9.29.1
# get https://goproxy.cn/gopkg.in/go-playground/validator.v9/@v/v9.29.1.zip
go: downloading github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2
# get https://goproxy.cn/github.com/dgryski/go-farm/@v/v0.0.0-20190423205320-6a90982ecee2.zip
go: downloading github.com/BurntSushi/toml v0.3.1
# get https://goproxy.cn/github.com/%21burnt%21sushi/toml/@v/v0.3.1.zip
# get https://goproxy.cn/github.com/shirou/gopsutil/@v/v2.19.11+incompatible.zip: 200 OK (0.184s)
# get https://goproxy.cn/google.golang.org/genproto/@v/v0.0.0-20200402124713-8ff61da6d932.zip: 200 OK (0.182s)
# get https://goproxy.cn/gopkg.in/go-playground/validator.v9/@v/v9.29.1.zip: 200 OK (0.154s)
# get https://goproxy.cn/github.com/prometheus/client_golang/@v/v1.5.1.zip: 200 OK (0.176s)
# get https://goproxy.cn/github.com/dgryski/go-farm/@v/v0.0.0-20190423205320-6a90982ecee2.zip: 200 OK (0.140s)
# get https://goproxy.cn/github.com/%21burnt%21sushi/toml/@v/v0.3.1.zip: 200 OK (0.193s)
go: extracting github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2
go: extracting github.com/BurntSushi/toml v0.3.1
go: downloading github.com/go-sql-driver/mysql v1.4.1
# get https://goproxy.cn/github.com/go-sql-driver/mysql/@v/v1.4.1.zip
go: extracting gopkg.in/go-playground/validator.v9 v9.29.1
go: extracting github.com/prometheus/client_golang v1.5.1
# get https://goproxy.cn/github.com/go-sql-driver/mysql/@v/v1.4.1.zip: 200 OK (0.116s)
go: downloading github.com/go-playground/universal-translator v0.16.0
# get https://goproxy.cn/github.com/go-playground/universal-translator/@v/v0.16.0.zip
go: downloading github.com/prometheus/common v0.9.1
# get https://goproxy.cn/github.com/prometheus/common/@v/v0.9.1.zip
go: downloading github.com/prometheus/procfs v0.0.11
# get https://goproxy.cn/github.com/prometheus/procfs/@v/v0.0.11.zip
go: extracting github.com/shirou/gopsutil v2.19.11+incompatible
go: downloading github.com/beorn7/perks v1.0.1
# get https://goproxy.cn/github.com/beorn7/perks/@v/v1.0.1.zip
go: downloading github.com/leodido/go-urn v1.1.0
# get https://goproxy.cn/github.com/leodido/go-urn/@v/v1.1.0.zip
go: extracting github.com/go-sql-driver/mysql v1.4.1
# get https://goproxy.cn/github.com/go-playground/universal-translator/@v/v0.16.0.zip: 200 OK (0.240s)
# get https://goproxy.cn/github.com/prometheus/common/@v/v0.9.1.zip: 200 OK (0.234s)
# get https://goproxy.cn/github.com/prometheus/procfs/@v/v0.0.11.zip: 200 OK (0.233s)
go: extracting golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
# get https://goproxy.cn/github.com/beorn7/perks/@v/v1.0.1.zip: 200 OK (0.219s)
# get https://goproxy.cn/github.com/leodido/go-urn/@v/v1.1.0.zip: 200 OK (0.194s)
go: downloading github.com/prometheus/client_model v0.2.0
# get https://goproxy.cn/github.com/prometheus/client_model/@v/v0.2.0.zip
# get https://goproxy.cn/github.com/prometheus/client_model/@v/v0.2.0.zip: 200 OK (0.206s)
go: extracting github.com/go-playground/universal-translator v0.16.0
go: downloading github.com/go-playground/locales v0.12.1
# get https://goproxy.cn/github.com/go-playground/locales/@v/v0.12.1.zip
go: extracting github.com/beorn7/perks v1.0.1
go: extracting github.com/leodido/go-urn v1.1.0
go: extracting github.com/prometheus/common v0.9.1
go: extracting github.com/prometheus/procfs v0.0.11
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
# get https://goproxy.cn/github.com/matttproud/golang_protobuf_extensions/@v/v1.0.1.zip
go: extracting github.com/prometheus/client_model v0.2.0
# get https://goproxy.cn/github.com/go-playground/locales/@v/v0.12.1.zip: 200 OK (0.234s)
# get https://goproxy.cn/github.com/matttproud/golang_protobuf_extensions/@v/v1.0.1.zip: 200 OK (0.190s)
go: extracting github.com/matttproud/golang_protobuf_extensions v1.0.1
go: extracting github.com/go-playground/locales v0.12.1
go: extracting google.golang.org/genproto v0.0.0-20200402124713-8ff61da6d932
# get https://goproxy.cn/github.com/google/wire/@v/v0.4.0.mod
# get https://goproxy.cn/github.com/google/wire/@v/v0.4.0.mod: 200 OK (0.042s)
# get https://goproxy.cn/golang.org/x/tools/@v/v0.0.0-20190422233926-fe54fb35175b.mod
# get https://goproxy.cn/github.com/google/subcommands/@v/v1.0.1.mod
# get https://goproxy.cn/golang.org/x/tools/@v/v0.0.0-20190422233926-fe54fb35175b.mod: 200 OK (0.032s)
# get https://goproxy.cn/sumdb/sum.golang.org/lookup/golang.org/x/tools@v0.0.0-20190422233926-fe54fb35175b
# get https://goproxy.cn/github.com/google/subcommands/@v/v1.0.1.mod: 200 OK (0.049s)
# get https://goproxy.cn/sumdb/sum.golang.org/lookup/github.com/google/subcommands@v1.0.1
# get https://goproxy.cn/sumdb/sum.golang.org/lookup/golang.org/x/tools@v0.0.0-20190422233926-fe54fb35175b: 200 OK (0.085s)
# get https://goproxy.cn/sumdb/sum.golang.org/lookup/github.com/google/subcommands@v1.0.1: 200 OK (0.104s)
kratos tool protoc --grpc --bm api.proto
go get -u github.com/go-kratos/kratos/tool/kratos-protoc@v0.5.0
go: finding github.com v0.5.0
go: finding github.com/go-kratos/kratos/tool/kratos-protoc v0.5.0
go: finding github.com/go-kratos/kratos/tool v0.5.0
go: finding github.com/go-kratos v0.5.0
protoc: 安装成功!
snap install --classic protobuf
error: access denied (try with sudo)
exit status 1
exit status 1
kratos tool genbts
go get -u github.com/go-kratos/kratos/tool/kratos-gen-bts@v0.5.0
go: finding github.com v0.5.0
go: finding github.com/go-kratos/kratos/tool/kratos-gen-bts v0.5.0
go: finding github.com/go-kratos v0.5.0
go: finding github.com/go-kratos/kratos/tool v0.5.0
genbts: 安装成功!
Close: 无声明 忽略此方法
Ping: 无声明 忽略此方法
dao.bts.go: 生成成功
kratos tool genmc
go get -u github.com/go-kratos/kratos/tool/kratos-gen-mc@v0.5.0
go: finding github.com/go-kratos/kratos/tool/kratos-gen-mc v0.5.0
go: finding github.com/go-kratos v0.5.0
go: finding github.com v0.5.0
go: finding github.com/go-kratos/kratos/tool v0.5.0
genmc: 安装成功!
mc.cache.go: 生成成功
kratos tool wire
go get -u github.com/google/wire/cmd/wire
go: downloading github.com/google/subcommands v1.0.1
go: downloading golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b
go: extracting github.com/google/subcommands v1.0.1
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.2.0
go: downloading github.com/google/subcommands v1.2.0
go: downloading golang.org/x/tools v0.0.0-20201017001424-6003fad69a88
go: extracting github.com/google/subcommands v1.2.0
go: extracting golang.org/x/tools v0.0.0-20201017001424-6003fad69a88
go: downloading golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
go: downloading golang.org/x/mod v0.3.0
go: extracting golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
go: extracting golang.org/x/mod v0.3.0
go: finding golang.org/x/xerrors latest
go: finding golang.org/x/mod v0.3.0
wire: 安装成功!
wire: /media/test/NewDisk1/go/src/kratos-demo/internal/service/service.go:15:33: first argument to Bind must be a pointer to an interface type; found <nil>
wire: kratos-demo/internal/di: generate failed
wire: at least one generate failure
exit status 1
kratos tool wire
wire: kratos-demo/internal/dao: wrote /media/test/NewDisk1/go/src/kratos-demo/internal/dao/wire_gen.go
Project: kratos-demo
OnlyGRPC: false
OnlyHTTP: false
Directory: /home/test/newDisk1/go/src/kratos-demo项目创建成功.
3,解决gRPC 编译问题
安装工具:
sudo apt install protobuf-compiler-grpc执行:kratos tool protoc --grpc --bm api.proto
报错,估计安装的版本不行,对不上。$ protoc --version
libprotoc 3.0.0
grpc 没有弄明白,先创建一个简单的HTTP服务:
4,只创建HTTP服务
可以增加参数 --http 只创建一个http 服务,然后还是依赖些 grpc的生成的代码。
$ kratos new kratos-demo --http
go: finding github.com/go-kratos/kratos master
# get https://goproxy.cn/github.com/go-kratos/kratos/@v/master.info
# get https://goproxy.cn/github.com/go-kratos/kratos/@v/master.info: 200 OK (8.222s)
# get https://goproxy.cn/github.com/@v/list
# get https://goproxy.cn/github.com/google/wire/@v/list
# get https://goproxy.cn/github.com/google/@v/list
# get https://goproxy.cn/github.com/@v/list: 404 Not Found (0.033s)
# get https://goproxy.cn/github.com/google/@v/list: 404 Not Found (0.142s)
# get https://goproxy.cn/github.com/google/wire/@v/list: 200 OK (0.482s)
kratos tool protoc --grpc --bm api.proto
2020/10/17 23:34:57 protoc --proto_path=/media/test/NewDisk1/go/src --proto_path=/media/test/NewDisk1/go/pkg/mod/github.com/go-kratos/kratos@v0.5.1-0.20201016144826-bc827de8b153/third_party --proto_path=/media/test/NewDisk1/go/src/kratos-demo/api --bm_out=:. api.proto
api.proto: File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
exit status 1
exit status 1
kratos tool genbts
Close: 无声明 忽略此方法
Ping: 无声明 忽略此方法
dao.bts.go: 生成成功
kratos tool genmc
mc.cache.go: 生成成功
kratos tool wire
wire: /media/test/NewDisk1/go/src/kratos-demo/internal/service/service.go:15:33: first argument to Bind must be a pointer to an interface type; found <nil>
wire: kratos-demo/internal/di: generate failed
wire: at least one generate failure
exit status 1
kratos tool wire
wire: kratos-demo/internal/dao: wrote /media/test/NewDisk1/go/src/kratos-demo/internal/dao/wire_gen.go
Project: kratos-demo
OnlyGRPC: false
OnlyHTTP: true
Directory: /home/test/newDisk1/go/src/kratos-demo项目创建成功.
本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/109139648
博主地址是:https://blog.csdn.net/freewebsys