编写Models.proto
syntax = "proto3";
package services;
import "google/protobuf/timestamp.proto"; //使用第三方proto支持时间类型的参数
message UserModel {
int32 user_id = 1;
string user_name = 2;
string user_pwd = 3;
google.protobuf.Timestamp user_date=4;
}
编写UserService.proto
syntax = "proto3";
package services;
import "Models.proto";
message RegResponse {
string status = 1;
string message = 2;
}
service UserService{
rpc UserReg(UserModel) returns(RegResponse);
}
生成pb文件
protoc --go_out=../ Models.proto # Models文件只有单独的模型,可以不加--micro_out
protoc --micro_out=../ --go_out=../ UserService.proto
protoc-go-inject-tag -input=../Models.pb.go
protoc-go-inject-tag -input=../UserService.pb.go
编写Service实例
syntax = "proto3";
package Services;
import "Models.proto";
message RegResponse {
string status = 1;
string message = 2;
}
service UserService{
rpc UserReg(UserModel) returns(RegResponse);
}
启动服务
package main
import (
"github.com/micro/go-micro"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/registry/etcdv3"
"github.com/micro/go-plugins/registry/consul"
"micro/Services"
"micro/ServicesImpl"
)
func main() {
consulReg := consul.NewRegistry(registry.Addrs("localhost:8500"))
etcdReg := etcdv3.NewRegistry(registry.Addrs("106.12.72.181:23791"))
myservice := micro.NewService(
micro.Name("api.xiahualou.com.test"),
micro.Address(":8001"),
micro.Registry(etcdReg),
micro.Registry(consulReg),
)
Services.RegisterUserServiceHandler(myservice.Server(), new(ServicesImpl.UserService))
myservice.Run()
}