go微服務框架kratos學習筆記三(構建單獨的http或者grpc demo項目)
前面兩篇跑通了demo項目,和大概了解了kratos demo整體結構,本篇分別構建一個http和一個grpc微服務單獨測試。
grpc
先從grpc 開始, 上篇沒有測試grpc接口,這回來嘗試,直接跑起demo 服務。
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
:簡單了解了下kratos的grpc框架 不是直接使用的google的grpc,類比http也是對grpc接口做了定制包裝而成的。
不改gRPC源碼,基於接口進行包裝集成trace、log、prom等組件
打通自有服務注冊發現系統discovery
實現更平滑可靠的負載均衡算法
// 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
內容很多,我們下次再看,先直接基於grpc 調用 kratos 接口。
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服務, 看看需要改動哪些,跑起微服務。
創建一個只生成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接口。
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
發現,大致要改的地方其實並不多:
1、service 改proto定義的接口邏輯
2、重新wirei靜態分析
3、解決編譯報錯。