zoukankan      html  css  js  c++  java
  • 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);
    }

    生成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()
    }
    




  • 相关阅读:
    git查看远程仓库地址命令
    sublime 安装插件GitGutter报错,git binary cannot be found等等
    sublime 中 pyv8 binary 报错怎么处理?
    经典语录(个人喜欢)
    css水平垂直居中对齐方式
    github怎么退出组织和删除自己创建的组织
    js学习篇1--数组
    js学习篇--数组按升序降序排列
    tp5.1 swoole 实现异步处理
    验证sll证书与密钥
  • 原文地址:https://www.cnblogs.com/hualou/p/12142940.html
Copyright © 2011-2022 走看看