README
Asjard
Asjard是一个用Go语言实现的由protobuf和配置驱动的微服务框架
安装
go get github.com/asjard/asjard@latest
protobuf编译命令安装
# rest 代码生成命令
go install github.com/asjard/asjard/cmd/protoc-gen-go-rest
# rest -> grpc gateway代码生成命令
go install github.com/asjard/asjard/cmd/protoc-gen-go-rest2grpc-gw
快速开始
更多示例请参考asjard-example
syntax = "proto3";
package api.v1.readme;
option go_package = "github.com/asjard/examples/protobuf/api/readmepb";
import "github.com/asjard/protobuf/http.proto";
import "google/protobuf/empty.proto";
service Examples {
// 注释,描述这个接口的作用
rpc Say(HelloReq) returns (HelloReq) {
option (asjard.api.http) = {
post : "/region/{region_id}/project/{project_id}/user/{user_id}"
};
option (asjard.api.http) = {
get : "/region/{region_id}/project/{project_id}/user/{user_id}"
};
};
// sse请求
rpc Log(google.protobuf.Empty) returns (google.protobuf.Empty) {
option (asjard.api.http) = {
get : "/log"
};
};
// grpc请求
rpc Call(HelloReq) returns (HelloReq) {};
}
message HelloReq {
message Obj {
int32 field_int = 1;
string field_str = 2;
}
message Configs {
string timeout = 1;
string field_in_different_file_under_same_section = 2;
string another_field_in_different_file_under_same_section = 3;
string key_in_different_sourcer = 4;
}
message Instance {
string id = 1;
string name = 2;
uint32 system_code = 3;
string version = 4;
map<string, string> metadata = 5;
}
enum Kind {
K_A = 0;
K_B = 1;
}
// 区域ID
string region_id = 1;
// 项目ID
string project_id = 2;
// 用户ID
int64 user_id = 3;
// 对象
Obj obj = 6;
// 对象列表
repeated Obj objs = 7;
// 配置
Configs configs = 8;
// 分页
int32 page = 9;
// 每页大小
int32 size = 10;
// 排序
string sort = 11;
// 布尔类型
optional bool ok = 12;
// 可选枚举参数
Kind kind = 15;
bytes bytes_value = 17;
// openapi 会把这个字段解析为字符串
uint64 uint64_value = 18;
Instance instance = 23;
创建配置
详细配置可参考这里
test_key: test_file_value
timeout: 5m
按需生成
protoc --go_out=${GOPATH}/src -I${GOPATH}/src -I. readme.proto
# 生成grpc需要的文件
protoc --go-grpc_out=${GOPATH}/src -I${GOPATH}/src -I. readme.proto
# 生成rest需要的文件, rest依赖grpc生成的文件
protoc --go-rest_out=${GOPATH}/src -I${GOPATH}/src -I. readme.proto
# 生成rest转grpc网关代码,依赖rest生成的文件
protoc --go-rest2grpc-gw_out=${GOPATH}/src -I${GOPATH}/src -I. readme.proto
启动
ASJARD_CONF_DIR=${PWD}/conf go run main.go
# 或者编译后执行
go build -o example main.go && ./example
## 请求rest接口
curl 127.0.0.1:6030/api/v1/examples/region/region-1/project/project-1/user/1234
输出内容:
{
"code": 0,
"err_code": 0,
"status": 0,
"system": 0,
"success": true,
"message": "",
"prompt": "",
"doc": "",
"request_id": "",
"request_method": "/api.v1.readme.Examples/Say",
"data": {
"@type": "type.googleapis.com/api.v1.readme.HelloReq",
"region_id": "region-1",
"project_id": "project-1",
"user_id": "1234",
"obj": null,
"objs": [],
"configs": {
"timeout": "",
"field_in_different_file_under_same_section": "",
"another_field_in_different_file_under_same_section": "",
"key_in_different_sourcer": "test_file_value"
},
"page": 0,
"size": 20,
"sort": "created_at",
"kind": "K_A",
"bytes_value": "",
"uint64_value": "0",
"instance": {
"id": "50a07851-1e6c-45b6-b87a-e945212e62e4",
"name": "readme",
"system_code": 100,
"version": "1.0.0",
"metadata": {}
}
}
}
更多信息请参考文档
Benchmark



三方库
下面是一些本框架中用到的开源库
库
描述
http协议
http路由管理
grpc协议
protobuf协议
熔断/降级
配置文件监听
prometheus监控上报
etcd连接
数据库连接
redis连接
yaml解析
本地缓存
openapiv3文档生成
配置类型转换
日志防爆
License
最后更新于