Erda Infra 一套轻量级 Go 微服务框架,包含大量现成的模块和工具,能够快速构建起以模块化驱动的应用程序。
一些 Go 项目基于该框架进行构建: * Erda
所有已注册的 Provider 通过 一份配置来确定是否 加载,由 Hub 对已加载的 Provider 的进行初始化、启动、关闭等。

通过实现 servicehub.ProviderDefine 接口来定义一个模块,并 通过 servicehub.RegisterProvider 函数进行注册。
但更简单的是通过 servicehub.Spec 来描述一个模块,并 通过 servicehub.Register 函数进行注册。
第一步,创建模块
➜ gohub init -o helloworld
Input Service Provider Name: helloworld
➜ # 以上命令创建了一个模块的模版代码,文件如下:
➜ tree helloworld
helloworld
├── provider.go
└── provider_test.go
第二步,创建 main.go
package main
import (
"github.com/erda-project/erda-infra/base/servicehub"
_ "./helloworld" // your package import path
)
func main() {
servicehub.Run(&servicehub.RunOptions{
Content: `
helloworld:
`,
})
}
第三步,运行程序
➜ go run main.go
INFO[2021-04-13 13:17:36.416] message: hi module=helloworld
INFO[2021-04-13 13:17:36.416] provider helloworld initialized
INFO[2021-04-13 13:17:36.416] signals to quit: [hangup interrupt terminated quit]
INFO[2021-04-13 13:17:36.426] provider helloworld running ...
INFO[2021-04-13 13:17:39.429] do something... module=helloworld
Hello World ( helloworld/ | main.go )
这些服务既可以被远程调用,也可以被本地模块调用。
第一步,在 *.proto 文件中定义协议 (消息结构 和 接口)
syntax = "proto3";
package erda.infra.example;
import "google/api/annotations.proto";
option go_package = "github.com/erda-project/erda-infra/examples/service/protocol/pb";
// the greeting service definition.
service GreeterService {
// say hello
rpc SayHello (HelloRequest) returns (HelloResponse) {
option (google.api.http) = {
get: "/api/greeter/{name}",
};
}
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
bool success = 1;
string data = 2;
}
第二步,编译生成接口 和 客户端代码
➜ gohub protoc protocol *.proto
➜ tree
.
├── client
│ ├── client.go
│ └── provider.go
├── greeter.proto
└── pb
├── greeter.form.pb.go
├── greeter.http.pb.go
├── greeter.pb.go
├── greeter_grpc.pb.go
└── register.services.pb.go
第三步,实现协议接口
➜ gohub protoc imp *.proto --imp_out=../server/helloworld
➜ tree ../server/helloworld
../server/helloworld
├── greeter.service.go
├── greeter.service_test.go
└── provider.go
第四步,创建 main.go 启动程序
main.go
package main
import (
"os"
"github.com/erda-project/erda-infra/base/servicehub"
// import all providers
_ "github.com/erda-project/erda-infra/examples/service/server/helloworld"
_ "github.com/erda-project/erda-infra/providers"
)
func main() {
hub := servicehub.New()
hub.Run("server", "server.yaml", os.Args...)
}
server.yaml
# optional
http-server:
addr: ":8080"
grpc-server:
addr: ":7070"
service-register:
# expose services and interface
erda.infra.example:
Service ( Protocol | Implementation | Server | Caller | Client )
该项目中已经封装了许多可用的模块,在 providers/ 目录下可以找到。
每一个模块下面,都有一个 examples 目录,包含了该模块的使用例子。
gohub 是一个能够帮助您快速构建模块的命令行工具,可以通过如下方式安装:
go get -u github.com/erda-project/erda-infra/tools/gohub
也可以通过 Docker 容器来使用以下工具:
➜ docker run --rm -ti -v $(pwd):/go \
registry.cn-hangzhou.aliyuncs.com/dice/erda-tools:1.0 gohub
Usage:
gohub [flags]
gohub [command]
Available Commands:
help Help about any command
init Initialize a provider with name
pkgpath Print the absolute path of go package
protoc ProtoBuf compiler tools
tools Tools
version Print the version number
Flags:
-h, --help help for gohub
Use "gohub [command] --help" for more information about a command.
Erda Infra is under the Apache 2.0 license. See the LICENSE file for details.
$ claude mcp add erda-infra \
-- python -m otcore.mcp_server <graph>