项目作者: cloudchacho

项目描述 :
Messaging bus for micro-services that works on cloud native queues http://authedwig.rtfd.io
高级语言: Go
项目地址: git://github.com/cloudchacho/hedwig-go.git
创建时间: 2021-05-21T17:00:29Z
项目社区:https://github.com/cloudchacho/hedwig-go

开源协议:Apache License 2.0

下载


Hedwig Library for Go

Build Status
Go Report Card
Godoc
codecov

Hedwig is an inter-service communication bus that works on AWS and GCP, while keeping things pretty simple and straight
forward.

It allows validation of the message payloads before they are sent, helping to catch cross-component incompatibilities
early.

Hedwig allows separation of concerns between consumers and publishers so your services are loosely coupled, and the
contract is enforced by the message payload validation. Hedwig may also be used to build asynchronous APIs.

For intra-service messaging, see Taskhawk.

To learn more, read the docs.

Fan Out

Hedwig utilizes SNS for fan-out configuration. A publisher publishes messages on a topic. This message may be received by zero or more consumers. The publisher need not be aware of the consuming application. There are a variety of messages that may be published as such, but they generally fall into two buckets:

  • Asynchronous API Requests: Hedwig may be used to call APIs asynchronously. The contract is enforced by your infra-structure by connecting SNS topics to SQS queues, and payload is validated using the schema you define. Response is a delivered using a separate message if required.
  • Notifications: The most common use case is to notify other services/apps that may be interested in events. For example, your User Management app can publish a user.created message notification to all your apps. As publishers and consumers are loosely coupled, this separation of concerns is very effective in ensuring a stable eco-system.

Provisioning

Hedwig works on SQS and SNS as backing queues. Before you can publish/consume messages, you need to provision the
required infra. This may be done manually, or, preferably, using Terraform. Hedwig provides tools to make infra
configuration easier: see Terraform Google for further
details.

Quick Start

First, install the library:

  1. go get github.com/cloudchacho/hedwig-go

Create a protobuf schema and save as schema.proto:

  1. syntax = "proto2";
  2. package main;
  3. import "hedwig/protobuf/options.proto";
  4. option go_package = "example.com/hedwig;main";
  5. message SendEmailV1 {
  6. option (hedwig.message_options).major_version = 1;
  7. option (hedwig.message_options).minor_version = 0;
  8. option (hedwig.message_options).message_type = "email.send";
  9. string to = 1;
  10. string message = 1;
  11. }

Clone hedwig Options definition file and compile your schema:

  1. git clone github.com/cloudchacho/hedwig /usr/local/lib/protobuf/include/hedwig/
  2. protoc -I/usr/local/lib/protobuf/include -I. --go_out=. schema.proto

In publisher application, initialize the publisher:

  1. settings := aws.Settings {
  2. AWSAccessKey: <YOUR AWS KEY>,
  3. AWSAccountID: <YOUR AWS ACCOUNT ID>,
  4. AWSRegion: <YOUR AWS REGION>,
  5. AWSSecretKey: <YOUR AWS SECRET KEY>,
  6. AWSSessionToken: <YOUR AWS SESSION TOKEN>,
  7. }
  8. backend := aws.NewBackend(settings, nil)
  9. encoderDecoder := protobuf.NewMessageEncoderDecoder([]proto.Message{&SendEmailV1{}})
  10. routing := map[hedwig.MessageRouteKey]string{
  11. {
  12. MessageType: "email.send",
  13. MessageMajorVersion: 1,
  14. }: "send_email",
  15. }
  16. publisher := hedwig.NewPublisher(backend, encoderDecoder, encoderDecoder, routing)

And finally, send a message:

  1. headers := map[string]string{}
  2. msg, err := hedwig.NewMessage("email.send", "1.0", headers, data, "myapp")
  3. if err != nil {
  4. return err
  5. }
  6. err := publisher.Publish(context.Background(), msg)

In consumer application, define your callback:

  1. // Handler
  2. func HandleSendEmail(ctx context.Context, msg *hedwig.Message) error {
  3. to := msg.data.(*SendEmailV1).GetTo()
  4. // actually send email
  5. }

And start the consumer:

  1. settings := aws.Settings {
  2. AWSAccessKey: <YOUR AWS KEY>,
  3. AWSAccountID: <YOUR AWS ACCOUNT ID>,
  4. AWSRegion: <YOUR AWS REGION>,
  5. AWSSecretKey: <YOUR AWS SECRET KEY>,
  6. AWSSessionToken: <YOUR AWS SESSION TOKEN>,
  7. }
  8. backend := aws.NewBackend(settings, nil)
  9. encoderDecoder := protobuf.NewMessageEncoderDecoder([]proto.Message{&SendEmailV1{}})
  10. registry := hedwig.CallbackRegistry{{"email.send", 1}: HandleSendEmail}
  11. consumer := hedwig.NewConsumer(backend, encoderDecoder, nil, registry)
  12. err := consumer.ListenForMessages(context.Background(), hedwig.ListenRequest{})

For more complete code, see examples.

Development

Prerequisites

Install go1.11.x

Getting Started

  1. $ cd ${GOPATH}/src/github.com/cloudchacho/hedwig-go
  2. $ go build

Running Tests

  1. $ make test

Getting Help

We use GitHub issues for tracking bugs and feature requests.

  • If it turns out that you may have found a bug, please open an issue

Release notes

v0.8

  • Cleaned up interfaces to be more idiomatic Go

v0.7

  • Initial version