Proto文件编写
-
工具下载
//基本工具 go get github.com/micro/protobuf/{proto,protoc-gen-go} go get github.com/micro/protoc-gen-micro //生成命令 protoc --go_out=[dir] --micro_out=[dir] [xx.proto] //pb文件tag注入 go get github.com/favadi/protoc-go-inject-tag //在需要使用tag注入的字段上加入 // @inject_tag: valid:"ip" => 如: // @inject_tag: json:"ip",form:"ip" //生成命令 protoc-go-inject-tag -input=./xxx.pb.go
-
proto文件编写
mkdir -p Services/protos cd Services/protos vim Models.proto -- syntax = "proto3"; package Services; message ProdModel{ // @inject_tag: json:"pid",form:"pid" int32 ProdID = 1; // @inject_tag: json:"pname",form:"pname" string ProdName = 2; } -- vim ProdService.proto -- syntax = "proto3"; package Services; import "Models.proto"; message ProdRequest { // @inject_tag: json:"size",form:"size" int32 Size = 1; } message ProdListResponse{ repeated ProdModel data = 1; } service ProdService { rpc GetProdList (ProdRequest) returns (ProdListResponse); } -- cd ../.. vim gen.sh -- cd Services/protos protoc --go_out=../ Models.proto protoc --go_out=../ --micro_out=../ ProdService.proto protoc-go-inject-tag -input=../Models.pb.go protoc-go-inject-tag -input=../ProdService.pb.go --
-
docker consul单机配置
docker pull consul docker run --name consul1 -d -p 8500:8500 -p 8300:8300 -p 8301:8301 -p 8302:8302 -p 8600:8600 consul agent -server -bootstrap-expect=1 -ui -bind=0.0.0.0 -client=0.0.0.0
-
编写商品RPC服务
新建工程,将步骤2文件拷贝到工程目录
-
获取package
go get github.com/micro/go-micro go get github.com/micro/go-plugins
-
编写服务代码(集中到一个文件中 main.go)
package main import ( "ProdRpcServer/Services" "context" "github.com/micro/go-micro" "github.com/micro/go-micro/registry" "github.com/micro/go-plugins/registry/consul" "log" "strconv" ) type ProdServiceImp struct { } func (this *ProdServiceImp) GetProdList(ctx context.Context, req *Services.ProdRequest, resp *Services.ProdListResponse) error { resp.Data = make([]*Services.ProdModel, 0) for i := int32(0); i < req.Size; i++ { resp.Data = append(resp.Data, &Services.ProdModel{ ProdID: 100 + i, ProdName: "product" + strconv.Itoa(int(100+i)), }) } return nil } func main() { const ( kConsulAddr = "127.0.0.1:8500" kServerName = "prodserver" ) reg := consul.NewRegistry(registry.Addrs(kConsulAddr)) s := micro.NewService( micro.Name(kServerName), micro.Registry(reg), ) err := Services.RegisterProdServiceHandler(s.Server(), new(ProdServiceImp)) if err != nil { log.Fatal(err) } //解析命令行参数进行配置 s.Init() s.Run() }
-
编写商品http服务,调用商品rpc服务
新建工程,讲步骤2文件拷贝到工程目录
-
获取package
go get github.com/micro/go-micro go get github.com/micro/go-plugins //gin github.com/gin-gonic/gin
-
编写服务代码(集中到main.go文件中)
package main import ( "ProdHttpServer/Services" "context" "fmt" "github.com/gin-gonic/gin" "github.com/micro/go-micro" "github.com/micro/go-micro/registry" "github.com/micro/go-micro/web" "github.com/micro/go-plugins/registry/consul" "net/http" ) func main() { const ( kConsulAddr = "127.0.0.1:8500" kRpcServerName = "prodserver" kWebServerName = "httpprodserver" ) reg := consul.NewRegistry(registry.Addrs(kConsulAddr)) s := micro.NewService( micro.Name(kRpcServerName+".client"), micro.Registry(reg), ) prodService := Services.NewProdService(kRpcServerName, s.Client()) //gin router := gin.Default() v1Group := router.Group("/v1") { v1Group.POST("/prod", func(ctx *gin.Context) { var req Services.ProdRequest err := ctx.Bind(&req) fmt.Println("request:", req.Size) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{ "info": err.Error(), }) return } resp, err := prodService.GetProdList(context.Background(), &req) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{ "info": err.Error(), }) return } ctx.JSON(http.StatusOK, gin.H{ "data": resp.Data, }) }) } webs := web.NewService( web.Name(kWebServerName), web.Registry(reg), web.Address(":8081"), web.Handler(router), ) webs.Init() webs.Run() }
-
使用PostMan进行测试