改造:分布式调度

协议

protobuf3

syntax = "proto3";

import "google/api/annotations.proto";

message JobName {
    string name = 1;
}

message Ignore {
}

message Job {
    string name = 1;
    string command = 2;
    string cronExpr = 3;
}

message Rsp {
    int32 retCode = 1;
    string retMsg = 2;
}

message RspWithJob {
    Job job = 1;
    Rsp rsp = 2;
}

message RspWithJobList {
    repeated Job jobList = 1;
    Rsp rsp = 2;
}

message JobLog {
    string jobName = 1;
    string command = 2;
    string err = 3;
    string output = 4;
    int64 planTime = 5;
    int64 scheduleTime = 6;
    int64 startTime = 7;
    int64 endTime = 8;
}

message ListLogReq {
    string name = 1;
    int32 skip = 2;
    int32 limit = 3;
}

message RspWithLogList {
    repeated JobLog logList = 1;
    Rsp rsp = 2;
}

message RspWithWorkerList {
    repeated string workerList = 1;
    Rsp rsp = 2;
}

service CronTab {
    rpc Save (Job) returns (RspWithJob) {
        option (google.api.http) = {
            post: "/job/save"
            body: "*"
        };
    };
    rpc Delete (JobName) returns (RspWithJob) {
        option (google.api.http) = {
            post: "/job/delete"
            body: "*"
        };
    };
    rpc KillJob (JobName) returns (Rsp) {
        option (google.api.http) = {
            post: "/job/kill"
            body: "*"
        };
    };
    rpc ListJobs (Ignore) returns (RspWithJobList) {
        option (google.api.http) = {
            post: "/job/list"
            body: "*"
        };
    };
    rpc ListLogs (ListLogReq) returns (RspWithLogList) {
        option (google.api.http) = {
            post: "/job/log"
            body: "*"
        };
    };
    rpc ListWorks (Ignore) returns (RspWithWorkerList) {
        option (google.api.http) = {
            post: "/worker/list"
            body: "*"
        };
    };
}

使用gofast编译

使用 gogo/protobuf 编译protoc --gofast_out=plugins=grpc:. helloworld.proto

编译报错

解决方案:配置 Go 环境变量(当然在这之前你要确保是否安装了插件)

gRPC:实现接口

Main

认证:签名

提供Http接口

双向认证

Gateway:同时提供RPC和Http接口

https://segmentfault.com/a/1190000013339403

https://segmentfault.com/a/1190000013408485

https://segmentfault.com/a/1190000013513469

为了方便代码编写,我们将/go/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.14.6/third_party/googleapis/google 拷贝到当前项目下(得到crontab/google)

安装

http

文档:基于Swagger

安装

生成

执行成功会在当前目录下生成crontab.swagger.json文件(在本项目中需要将该文件生成到cmd目录下)

下载Swagger UI文件

Swagger提供可视化的API管理平台,就是Swagger UI

我们将其源码下载下来,并将其dist目录下的所有文件拷贝到我们项目中的$GOPATH/src/grpc-hello-world/third_party/swagger-ui

Swagger UI转换为Go源代码

转换

在项目下新建pkg/ui/data/swagger目录(不创建也可以,执行命令会自动创建),回到$GOPATH/src/github.com/1005281342/crontab/下,执行命令

检查

回到pkg/ui/data/swagger目录,检查是否存在datafile.go文件

Swagger UI文件服务器(对外提供服务)

在这一步,我们需要使用与其配套的go-bindata-assetfs

它能够使用go-bindata所生成Swagger UIGo代码,结合net/http对外提供服务

安装

编写main

测试

查看数据 http://127.0.0.1:8081/swagger/crontab.swagger.json

测试地址 http://127.0.0.1:8081/swagger-ui/

(注意默认填写有地址,需要替换成 http://127.0.0.1:8081/swagger/crontab.swagger.json)

最后更新于

这有帮助吗?