前面两篇跑通了demo项目,和大概了解了kratos demo总体结构,本篇分别构建一个http和一个grpc微服务单独测试。git
grpc
先从grpc 开始, 上篇没有测试grpc接口,这回来尝试,直接跑起demo 服务。github
kratos new rpcdemo --grpc
kratos run INFO 12/26-20:49:08.933 I:/VSProject/kratos/rpcdemo/cmd/main.go:19 rpcdemo start 2019/12/26 20:49:08 start watch filepath: I:\VSProject\kratos\rpcdemo\configs [warden] config is Deprecated, argument will be ignored. please use -grpc flag or GRPC env to configure warden server. INFO 12/26-20:49:08.953 I:/VSProject/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
warden
不改gRPC源码,基于接口进行包装集成trace、log、prom等组件
打通自有服务注册发现系统discovery
实现更平滑可靠的负载均衡算法json
// New new a grpc server. func New(svc pb.DemoServer) (ws *warden.Server, err error) { var ( cfg warden.ServerConfig ct paladin.TOML ) if err = paladin.Get("grpc.toml").Unmarshal(&ct); err != nil { return } if err = ct.Get("Server").UnmarshalTOML(&cfg); err != nil { return } ws = warden.NewServer(&cfg) pb.RegisterDemoServer(ws.Server(), svc) ws, err = ws.Start() return }
warden
package main import ( "fmt" pb "cli/api" "golang.org/x/net/context" "google.golang.org/grpc" ) const ( Address = "127.0.0.1:50052" ) func main() { conn, err := grpc.Dial("127.0.0.1:9000", grpc.WithInsecure()) if err != nil { fmt.Println(err) } defer conn.Close() c := pb.NewDemoClient(conn) req := new(pb.HelloReq) req.Name = "kratos grpc" r, err := c.SayHelloURL(context.Background(), req) if err != nil { fmt.Println(err) return } fmt.Println(r.Content) }
http
前面内容都是基于kratos 本来demo的使用, 如今咱们本身随便定义一个liveroom.proto的bm服务, 看看须要改动哪些,跑起微服务。app
建立一个只生成bm代码的项目liveroom负载均衡
kratos new liveroom -d C:\项目路径 --http
I:/VSProject/kratos/liveroom/cmd/main.go:19 liveroom start 2019/12/26 20:13:59 start watch filepath: I:\VSProject\kratos\liveroom\configs INFO 12/26-20:13:59.446 I:/VSProject/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
但这只是前两章的demo项目,接着删掉api 路径下的pb.go和bm.go,定义本身的api.proto框架
syntax = "proto3"; import "github.com/gogo/protobuf/gogoproto/gogo.proto"; import "google/protobuf/empty.proto"; import "google/api/annotations.proto"; // package 命名使用 {appid}.{version} 的方式, version 形如 v1, v2 .. package liveroom.service.v1; // NOTE: 最后请删除这些无用的注释 (゜-゜)つロ option go_package = "api"; option (gogoproto.goproto_getters_all) = false; service Liveroom { rpc Create (Req) returns (Resp); rpc Delete (Req) returns (Resp); rpc Get(Req) returns (Resp) { option (google.api.http) = { get:"/live-room/get" }; }; } message Req { string name = 1 [(gogoproto.moretags)='form:"name" validate:"required"']; } message Resp { string Content = 1 [(gogoproto.jsontag) = 'content']; }
go generate
go generate go get -u github.com/bilibili/kratos/tool/kratos-protoc protoc: 安装成功! 2019/12/26 20:26:44 protoc --proto_path=I:\VSProject\go/src --proto_path=I:\VSProject\go/pkg/mod/github.com/bilibili/kratos@v0.3.2-0.20191224125553-6e1180f53a8e/third_party --proto_path=I:\VSProject\kratos\liveroom\api --bm_out=:. api.proto api.proto:7:1: warning: Import google/protobuf/empty.proto is unused. 2019/12/26 20:26:44 protoc --proto_path=I:\VSProject\go/src --proto_path=I:\VSProject\go/pkg/mod/github.com/bilibili/kratos@v0.3.2-0.20191224125553-6e1180f53a8e/third_party --proto_path=I:\VSProject\kratos\liveroom\api --gofast_out=plugins=grpc:. api.proto api.proto:7:1: warning: Import google/protobuf/empty.proto is unused. 2019/12/26 20:26:44 generate api.proto success.
client.go 的newclient()接口也须要重定义。
接着service 层 业务逻辑层 从新定义咱们的接口实现
func (s *Service) Create(ctx context.Context, req *pb.Req) (reply *pb.Resp, err error) { reply = &pb.Resp{ Content: "Create " + req.Name, } fmt.Printf("Create %s", req.Name) return } func (s *Service) Delete(ctx context.Context, req *pb.Req) (reply *pb.Resp, err error) { reply = &pb.Resp{ Content: "Delete " + req.Name, } fmt.Printf("Delete %s", req.Name) return } func (s *Service) Get(ctx context.Context, req *pb.Req) (reply *pb.Resp, err error) { reply = &pb.Resp{ Content: "Get " + req.Name, } fmt.Printf("Get %s", req.Name) return }
依赖注入层修改wire.go
从新生成静态分析文件wire_gen.go。
go generate
>go generate go get -u github.com/google/wire/cmd/wire go: finding golang.org/x/tools latest wire: 安装成功! wire: liveroom/internal/di: wrote I:\VSProject\kratos\liveroom\internal\di\wire_gen.go
解决编译问题,bm微服务就起来了。
kratos run INFO 12/26-20:39:48.725 I:/VSProject/kratos/liveroom/cmd/main.go:19 liveroom start 2019/12/26 20:39:48 start watch filepath: I:\VSProject\kratos\liveroom\configs INFO 12/26-20:39:48.755 I:/VSProject/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
发现,大体要改的地方其实并很少: 一、service 改proto定义的接口逻辑 二、从新wirei静态分析 三、解决编译报错。