Go-Micro构建学生rpc服务并注册到consul
简介Go-Micro构建学生rpc服务并注册到consul
            1、目录结构
micro-student:项目目录
    service:服务目录
        controller:实现目录
        model:proto生成的目录
        proto:proto定义目录
    main.go:入口
    make_proto.bat:生成proto
    runAll.bat:运行多个服务
2、新建proto文件
文件名:models.proto,内容如下
syntax = "proto3";
package services;
//公共的模型
message StudentModel {
    int32 id = 1; //学生ID
    string name = 2; //学生名称
    string sex = 3; //性别
}
文件名:student_service.proto,内容如下
syntax = "proto3";
package services;
import "models.proto";
//学生列表请求
message StudentListRequest {
    int32 num = 1; //请求数量
}
//学生列表返回
message StudentListResponse {
    int32 status = 1; //状态
    repeated StudentModel data = 2; //学生模型数据
}
//学生列表请求方法
service StudentListService {
    rpc GetStudentListService (StudentListRequest) returns (StudentListResponse);
}
3、编写生成proto的批处理文件(make_proto.bat)
cd service/proto
protoc --go_out=../model models.proto
protoc --micro_out=../model --go_out=../model student_service.proto
cd ../../
执行批处理后,model目录下会生成对应的文件
4、编写服务的业务实现逻辑(controller目录下新建student_service.go文件)
package controller
import (
    "context"
    services "micro-api/service/model"
    "strconv"
)
type StudentService struct {
}
//业务实现
func (c *StudentService) GetStudentListService(ctx context.Context, req *services.StudentListRequest, rsp *services.StudentListResponse) error {
    var data []*services.StudentModel
    for i := 1; i <= int(req.Num); i++ {
        data = append(data, &services.StudentModel{
            Id:   int32(i),
            Name: "name_" + strconv.Itoa(i),
            Sex:  "男",
        })
    }
    rsp.Status = 200
    rsp.Data = data
    return nil
}
5、编写main.go
package main
import (
    "github.com/micro/go-micro"
    "github.com/micro/go-micro/registry"
    "github.com/micro/go-plugins/registry/consul"
    "micro-api/service/controller"
    services "micro-api/service/model"
)
func main() {
    register := consul.NewRegistry(func(options *registry.Options) {
        options.Addrs = []string{
            "192.168.1.171:8500",
            "192.168.1.177:8500",
            "192.168.1.178:8500",
        }
    })
    service := micro.NewService(
        micro.Name("student-service"),
        micro.Registry(register),
    )
    if err := services.RegisterStudentListServiceHandler(service.Server(), new(controller.StudentService)); err != nil {
    }
    service.Init()
    service.Run()
}
6、批量文件文件(runAll.bat)
@echo off
start cmd /k "cd/d E:\go\micro-student &&go run main.go --server_address=:50001"
start cmd /k "cd/d E:\go\micro-student &&go run main.go --server_address=:50002"
start cmd /k "cd/d E:\go\micro-student &&go run main.go --server_address=:50003"
Flutter是谷歌的移动UI框架,可以快速在iOS和Android上构建高质量的原生用户界面。 Flutter可以与现有的代码一起工作。在全世界,Flutter正在被越来越多的开发者和组织使用,并且Flutter是完全免费、开源的。它也是构建未来的Google Fuchsia应用的主要方式。
ip命令是iproute软件包中的一个强大的网络配置工具,用于显示和管理Linux系统的路由、网络设备、策略路由和隧道。Centos7推荐使用ip命令代替传统的ipconfig和route[该命令我们没有进行说明,需要学习的请参考网上资料]命令。
MongoDB聚合查询之分段分组查询统计
二叉树(Binary tree)是树形结构的一个重要类型。许多实际问题抽象出来的数据结构往往是二叉树形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显得特别重要。二叉树特点是每个结点最多只能有两棵子树,且有左右之分。
最近在使用Centrifugo的项目的时候,当连接数比较多的是时候,出现了“too many open files”的错误。本文主要记录如何排除和优化该问题。
快速生成表格
Electron页面跳转、浏览器打开链接和打开新窗口
Docker编译镜像出现:fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.12/main: temporary error (try again later)
WARNING: Ignoring APKINDEX.2c4ac24e.tar.gz: No such file or directory问题
在Mac电脑中,如何对Git的用户名和密码进行修改呢?起初不懂Mac,所以整了很久,本文将记录如何对这个进行操作,以便后期使用。
在使用Git的过程中,不想每次都输入用户名和密码去拉取代码,所以就需要保存这些信息,那么既然有保存了,就必须有清除功能。