Home

Awesome

Erda Infra

Go Reference License codecov

Translations: English | 简体中文

Erda Infra is a lightweight microservices framework implements by golang, which offers many useful modules and tools to help you quickly build a module-driven application.

Many Go projects are built using Erda Infra including:

Features

Concept

A configuration is used to determine whether all registered Providers are loaded, and the Hub initializes, starts, and closes the loaded Providers.

servicehub

Define Provider

Define a provider by implementing the servicehub.ProviderDefine interface, and register it through the servicehub.RegisterProvider function.

But, it is simpler to describe a provider through servicehub.Spec and register it through the servicehub.Register function.

Examples

Quick Start

Create a Provider

Step 1, Create a Provider

➜ gohub init -o helloworld
Input Service Provider Name: helloworld
➜ tree helloworld
helloworld
├── provider.go
└── provider_test.go

Step 2, Create 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:
`,
	})
}

Step 3, Run

➜ 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 )

Create HTTP/gRPC Service

These services can be called either remote or local Provider.

Step 1, Define the protocol, in the *.proto files, includes Message and Interface.

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;
}

Step 2, build protocol to codes

➜ 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

Step 3, implement the interface

➜ gohub protoc imp *.proto --imp_out=../server/helloworld
➜ tree ../server/helloworld
../server/helloworld
├── greeter.service.go
├── greeter.service_test.go
└── provider.go

Step 4, Create main.go and run it

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 )

Useful Providers

Many available providers have been packaged in this project, it can be found in the providers/ directory.

Under each module, there is an examples directory, which contains examples of the use of the module.

Tools

gohub is a CLI tool, which to help you quickly build a Provider. It can be installed as follows:

go get -u github.com/erda-project/erda-infra/tools/gohub

You can also use gohub through a Docker container.

➜ 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.

License

Erda Infra is under the Apache 2.0 license. See the LICENSE file for details.