24.シナリオの練習、基本インタフェース(ユーザ登録・インターフェース)

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);
}

鉛生成されたファイル

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

サービスの準備の例

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/etcd"
    "github.com/micro/go-plugins/registry/consul"
    "micro/Services"
    "micro/ServicesImpl"
)

func main() {
    consulReg := consul.NewRegistry(registry.Addrs("localhost:8500"))
    etcdReg := etcd.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()
}





おすすめ

転載: www.cnblogs.com/hualou/p/12142940.html