Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Babex is a modern solution for communications between microservices

NotificationsYou must be signed in to change notification settings

babex-group/babex

Repository files navigation

GoDoc

The Babex allows you to make a chain of microservices on the fly with the help of RabbitMQ, Kafka, etc.

Docs

Adapters

Usage

For example, we create service which will add the number to counter. We will use the Kafka adapter.

package mainimport ("github.com/babex-group/babex""github.com/babex-group/babex-kafka""log""os""os/signal""encoding/json")funcmain() {a,err:=kafka.NewAdapter(kafka.Options{Name:"babex-sandbox"Topics: []string{"example-topic"},Addrs:  []string{"localhost:29092"},})iferr!=nil {log.Fatal(err)}s:=babex.NewService(a)defers.Close()s.Handler("example-topic","",func(msg*babex.Message)error {vardatastruct{Countint`json:"count"`}iferr:=json.Unmarshal(msg.Data,&data);err!=nil {msg.Ack()// The message has invalid format, skip it.returnerr}data.Count+=1fmt.Printf("count = %v\r\n",data.Count)returns.Next(msg,data,nil)// Next automatically use msg.Ack()})signals:=make(chan os.Signal,1)signal.Notify(signals,os.Interrupt)<-signals}

And publish the message to the topic "babex-sandbox":

{"data": {"count":0  },"chain": [    {"exchange":"example-topic"    }  ]}

Check logs:

$ count = 1

Excellent! Let's change the message:

{"data": {"count":0  },"chain": [    {"exchange":"example-topic"    },    {"exchange":"example-topic"    }  ]}

Check logs:

$ count = 1$ count = 2

The service receive two message via chain, and increment the count.


[8]ページ先頭

©2009-2025 Movatter.jp