{"id":19736409,"url":"https://github.com/erda-project/erda-infra","last_synced_at":"2025-05-16T07:05:47.518Z","repository":{"id":38146319,"uuid":"344676433","full_name":"erda-project/erda-infra","owner":"erda-project","description":"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","archived":false,"fork":false,"pushed_at":"2024-11-18T03:41:35.000Z","size":1272,"stargazers_count":167,"open_issues_count":3,"forks_count":66,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-12T04:47:00.391Z","etag":null,"topics":["framework","go","golang","grpc","http","microservice","protobuf"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/erda-project.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-05T02:57:03.000Z","updated_at":"2025-03-23T08:31:10.000Z","dependencies_parsed_at":"2024-03-07T08:46:42.563Z","dependency_job_id":"c3c5af57-06fb-4647-b5c5-eff2983d003e","html_url":"https://github.com/erda-project/erda-infra","commit_stats":null,"previous_names":["erda-project/infra"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erda-project%2Ferda-infra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erda-project%2Ferda-infra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erda-project%2Ferda-infra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erda-project%2Ferda-infra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/erda-project","download_url":"https://codeload.github.com/erda-project/erda-infra/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254485062,"owners_count":22078767,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["framework","go","golang","grpc","http","microservice","protobuf"],"created_at":"2024-11-12T01:07:04.307Z","updated_at":"2025-05-16T07:05:42.506Z","avatar_url":"https://github.com/erda-project.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Erda Infra\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/erda-project/erda-infra.svg)](https://pkg.go.dev/github.com/erda-project/erda-infra)\n[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)\n[![codecov](https://codecov.io/gh/erda-project/erda-infra/branch/develop/graph/badge.svg?token=SVROJLY8UK)](https://codecov.io/gh/erda-project/erda-infra)\n\nTranslations: [English](README.md) | [简体中文](README_zh.md)\n\nErda 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.\n\nMany Go projects are built using Erda Infra including:\n* [Erda](https://github.com/erda-project/erda)\n\n## Features\n* modular design to drive the implementation of the application system, and each module is pluggable.\n* each module is configurable, supports setting defaults、reading from files (YAML, HCL,、JSON、TOML、.env file)、environment、flags.\n* manage the lifecycle of the module, includes initialization, startup, and shutdown.\n* manage dependencies between modules.\n* support Dependency Injection between modules.\n* offers many commonly modules that can be used directly.\n* support define APIs and models in protobuf file to expose both gRPC and HTTP APIs.\n* offers tools to help you quickly build a module.\n* etc.\n\n## Concept\n* Service, represents a function.\n* Provider, service provider, equivalent to module, provide some services. It can also depend on other services, witch provide by other provider.\n* ProviderDefine, describe a provider, includes provider's name, constructor function of provider, services, etc. Register it by *servicehub.RegisterProvider* function.\n* Hub, is a container for all providers, and manage the life cycle of all loaded providers.\n\nA configuration is used to determine whether all registered Providers are loaded, and the Hub initializes, starts, and closes the loaded Providers.\n\n![servicehub](./docs/servicehub.jpg)\n\n## Define Provider\nDefine a provider by implementing the *servicehub.ProviderDefine* interface, and register it through the *servicehub.RegisterProvider* function.\n\nBut, it is simpler to describe a provider through *servicehub.Spec* and register it through the *servicehub.Register* function.\n\n[Examples](./base/servicehub/examples)\n\n## Quick Start\n### Create a Provider\n**Step 1**, Create a Provider\n```sh\n➜ gohub init -o helloworld\nInput Service Provider Name: helloworld\n➜ tree helloworld\nhelloworld\n├── provider.go\n└── provider_test.go\n```\n\n**Step 2**, Create *main.go*\n```go\npackage main\n\nimport (\n\t\"github.com/erda-project/erda-infra/base/servicehub\"\n\t_ \"./helloworld\" // your package import path\n)\n\nfunc main() {\n\tservicehub.Run(\u0026servicehub.RunOptions{\n\t\tContent: `\nhelloworld:\n`,\n\t})\n}\n```\n\n**Step 3**, Run\n```sh\n➜ go run main.go\nINFO[2021-04-13 13:17:36.416] message: hi                                   module=helloworld\nINFO[2021-04-13 13:17:36.416] provider helloworld initialized              \nINFO[2021-04-13 13:17:36.416] signals to quit: [hangup interrupt terminated quit] \nINFO[2021-04-13 13:17:36.426] provider helloworld running ...              \nINFO[2021-04-13 13:17:39.429] do something...                               module=helloworld\n```\n[Hello World](./examples/example) \\( [helloworld/](./examples/example/helloworld) | [main.go](./examples/example/main.go) \\)\n\n### Create HTTP/gRPC Service\nThese services can be called either remote or local Provider.\n\n**Step 1**, Define the protocol, in the *.proto files, includes Message and Interface.\n```protobuf\nsyntax = \"proto3\";\n\npackage erda.infra.example;\nimport \"google/api/annotations.proto\";\noption go_package = \"github.com/erda-project/erda-infra/examples/service/protocol/pb\";\n\n// the greeting service definition.\nservice GreeterService {\n  // say hello\n  rpc SayHello (HelloRequest) returns (HelloResponse)  {\n    option (google.api.http) = {\n      get: \"/api/greeter/{name}\",\n    };\n  }\n}\n\nmessage HelloRequest {\n  string name = 1;\n}\n\nmessage HelloResponse {\n  bool success = 1;\n  string data = 2;\n}\n```\n\n**Step 2**, build protocol to codes \n```sh\n➜ gohub protoc protocol *.proto \n➜ tree \n.\n├── client\n│   ├── client.go\n│   └── provider.go\n├── greeter.proto\n└── pb\n    ├── greeter.form.pb.go\n    ├── greeter.http.pb.go\n    ├── greeter.pb.go\n    ├── greeter_grpc.pb.go\n    └── register.services.pb.go\n```\n\n**Step 3**, implement the interface\n```sh\n➜ gohub protoc imp *.proto --imp_out=../server/helloworld\n➜ tree ../server/helloworld\n../server/helloworld\n├── greeter.service.go\n├── greeter.service_test.go\n└── provider.go\n```\n\n**Step 4**, Create *main.go* and run it\n\n*main.go*\n```\npackage main\n\nimport (\n\t\"os\"\n\n\t\"github.com/erda-project/erda-infra/base/servicehub\"\n\n\t// import all providers\n\t_ \"github.com/erda-project/erda-infra/examples/service/server/helloworld\"\n\t_ \"github.com/erda-project/erda-infra/providers\"\n)\n\nfunc main() {\n\thub := servicehub.New()\n\thub.Run(\"server\", \"server.yaml\", os.Args...)\n}\n```\n\n*server.yaml*\n```yaml\n# optional\nhttp-server:\n    addr: \":8080\"\ngrpc-server:\n    addr: \":7070\"\nservice-register:\n# expose services and interface\nerda.infra.example:\n```\n\n[Service](./examples/service) \\( [Protocol](./examples/service/protocol) | [Implementation](./examples/service/server/helloworld) | [Server](./examples/service/server) | [Caller](./examples/service/caller) | [Client](./examples/service/client)  \\)\n\n## Useful Providers\nMany available providers have been packaged in this project, it can be found in the [providers/](./providers) directory.\n\nUnder each module, there is an examples directory, which contains examples of the use of the module.\n\n* elasticsearch, provide elasticsearch client APIs, and it easy to write batch data.\n* etcd, provide etcd client APIs.\n* etcd-mutex, distributed lock implemented by etcd.\n* grpcserver, start a grpc server.\n* grpcclient, provide gRPC client, and manage client's connection.\n* health, provide health check API, and can register some health check function into this provider.\n* httpserver, provide an HTTP server, support any form of handle function, interceptor, parameter binding, parameter verification, etc.\n* i18n, provide internationalization support, manage i18n files, support templates.\n* kafka, provide kafka sdk, and easy to produce and consume messages.\n* kubernetes, provide kubernetes client APIs.\n* mysql, provide mysql client APIs.\n* pprof, expose pprof HTTP APIs By httpserver.\n* redis, provide redis client APIs.\n* zk-master-election, provide interface about master-slave election, it is implemented by zookeeper.\n* zookeeper, provide zookeeper client.\n* cassandra, provide cassandra APIs, and easy to write batch data\n* serviceregister, use it to register services to expose gRPC and HTTP APIs.\n\n# Tools\n*gohub* is a CLI tool, which to help you quickly build a Provider. It can be installed as follows:\n```sh\ngo get -u github.com/erda-project/erda-infra/tools/gohub\n```\n\nYou can also use *gohub* through a Docker container.\n```sh\n➜ docker run --rm -ti -v $(pwd):/go \\\n    registry.cn-hangzhou.aliyuncs.com/dice/erda-tools:1.0 gohub                                                                \nUsage:\n  gohub [flags]\n  gohub [command]\n\nAvailable Commands:\n  help        Help about any command\n  init        Initialize a provider with name\n  pkgpath     Print the absolute path of go package\n  protoc      ProtoBuf compiler tools\n  tools       Tools\n  version     Print the version number\n\nFlags:\n  -h, --help   help for gohub\n\nUse \"gohub [command] --help\" for more information about a command.\n```\n\n## License\nErda Infra is under the Apache 2.0 license. See the [LICENSE](/LICENSE) file for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferda-project%2Ferda-infra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferda-project%2Ferda-infra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferda-project%2Ferda-infra/lists"}