golang_go-micro整合各種mq

語言: CN / TW / HK

NATS

介紹

NATS是一個開源、輕量級、高效能的分散式訊息中介軟體,實現了高可伸縮性和優雅的Publish/Subscribe模型,使用Golang語言開發。NATS的開發哲學認為高質量的QoS應該在客戶端構建,故只建立了Request-Reply,不提供 1.持久化 2.事務處理 3.增強的交付模式 4.企業級佇列。

NATS訊息傳遞模型

NATS支援各種訊息傳遞模型,包括:

釋出訂閱(Publish Subscribe)

請求回覆(Request Reply)

佇列訂閱(Queue Subscribers )

提供的功能:

純粹的釋出訂閱模型(Pure pub-sub)

伺服器叢集(Cluster mode server)

自動精簡訂閱者(Auto-pruning of subscribers)

基於文字協議(Text-based protocol)

多服務質量保證(Multiple qualities of service - QoS)

釋出訂閱(Publish Subscribe)

NATS將publish/subscribe訊息分發模型實現為一對多通訊,釋出者在Subject上傳送訊息,並且監聽該Subject在任何活動的訂閱者都會收到該訊息

整合go-micro

consumer:

package main

import (
    "fmt"
    "github.com/micro/go-micro/broker"
    "github.com/micro/go-plugins/broker/nats"
    nat "github.com/nats-io/nats.go"
    "log"
)

var (
    topic = "go.micro.topic.foo"
)

// Example of a shared subscription which receives a subset of messages
func sharedSub() {
    _, err := broker.Subscribe(topic, func(p broker.Event) error {
        fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header)
        return nil
    }, broker.Queue("consumer"))
    if err != nil {
        fmt.Println(err)
    }
}

// Example of a subscription which receives all the messages
func sub() {
    _, err := broker.Subscribe(topic, func(p broker.Event) error {
        fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header)
        return nil
    })
    if err != nil {
        fmt.Println(err)
    }
}

func main() {

    na := nats.NewBroker(nats.Options(nat.GetDefaultOptions()))

    if err := na.Init(); err != nil {
        log.Fatalf("Broker Init error: %v", err)
    }

    if err := na.Connect(); err != nil {
        log.Fatalf("Broker Connect error: %v", err)
    }
    sub()
    select {}
}

producer:

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/micro/go-micro/broker"
    "github.com/micro/go-plugins/broker/nats"
    nat "github.com/nats-io/nats.go"
)

var (
    topic = "go.micro.topic.foo"
)

func pub() {
    tick := time.NewTicker(time.Second)
    i := 0
    for _ = range tick.C {
        msg := &broker.Message{
            Header: map[string]string{
                "id": fmt.Sprintf("%d", i),
            },
            Body: []byte(fmt.Sprintf("%d: %s", i, time.Now().String())),
        }
        if err := broker.Publish(topic, msg); err != nil {
            log.Printf("[pub] failed: %v", err)
        } else {
            fmt.Println("[pub] pubbed message:", string(msg.Body))
        }
        i++
    }
}

func main() {

    na := nats.NewBroker(nats.Options(nat.GetDefaultOptions()))

    if err := na.Init(); err != nil {
        log.Fatalf("Broker Init error: %v", err)
    }

    if err := na.Connect(); err != nil {
        log.Fatalf("Broker Connect error: %v", err)
    }

    pub()
}

NATS Streaming

介紹

NATS由於不能保證訊息的投遞正確性和存在其他的缺點,NATS Streaming就孕育而生.他是一個由NATS提供支援的資料流系統,採用Go語言編寫,NATS Streaming與核心NATS平臺無縫嵌入,擴充套件和互操作.除了核心NATS平臺的功能外,他還提供了以下功能:

NATS Streaming特徵

增強訊息協議(Enhanced message protocol)

訊息/事件持久化(Message/event persistence)

至少一次資料傳輸(At-least-once-delivery)

Publisher限速(Publisher rate limiting)

Subscriber速率匹配(Rate matching/limiting per subscriber)

按主題重發訊息(Historical message replay by subject)

持續訂閱(Durable subscriptions)

整合go-micro

consumer:

package main

import (
    "fmt"
    "github.com/micro/go-micro/broker"
    "github.com/micro/go-plugins/broker/stan"
    stancf "github.com/nats-io/stan.go"
    "log"
)

var (
    topic = "go.micro.topic.foo"
)

// Example of a shared subscription which receives a subset of messages
func sharedSub() {
    _, err := broker.Subscribe(topic, func(p broker.Event) error {
        fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header)
        return nil
    }, broker.Queue("consumer"))
    if err != nil {
        fmt.Println(err)
    }
}

// Example of a subscription which receives all the messages
func sub() {
    _, err := broker.Subscribe(topic, func(p broker.Event) error {
        fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header)
        return nil
    })
    if err != nil {
        fmt.Println(err)
    }
}

func main() {

    na := stan.NewBroker(stan.ClusterID("test-cluster"), stan.Options(stancf.GetDefaultOptions()))

    if err := na.Init(); err != nil {
        log.Fatalf("Broker Init error: %v", err)
    }

    if err := na.Connect(); err != nil {
        log.Fatalf("Broker Connect error: %v", err)
    }
    sub()
    select {}
}

producer:

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/micro/go-micro/broker"
    "github.com/micro/go-plugins/broker/stan"
    stancf "github.com/nats-io/stan.go"
)

var (
    topic = "go.micro.topic.foo"
)

func pub() {
    tick := time.NewTicker(time.Second)
    i := 0
    for _ = range tick.C {
        msg := &broker.Message{
            Header: map[string]string{
                "id": fmt.Sprintf("%d", i),
            },
            Body: []byte(fmt.Sprintf("%d: %s", i, time.Now().String())),
        }
        if err := broker.Publish(topic, msg); err != nil {
            log.Printf("[pub] failed: %v", err)
        } else {
            fmt.Println("[pub] pubbed message:", string(msg.Body))
        }
        i++
    }
}

func main() {

    na := stan.NewBroker(stan.ClusterID("test-cluster"), stan.Options(stancf.GetDefaultOptions()))

    if err := na.Init(); err != nil {
        log.Fatalf("Broker Init error: %v", err)
    }

    if err := na.Connect(); err != nil {
        log.Fatalf("Broker Connect error: %v", err)
    }

    pub()
}

命令列選擇mq

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/micro/go-micro/broker"
    "github.com/micro/go-micro/config/cmd"
    _ "github.com/micro/go-plugins/broker/nats" //匯入該包,
)

var (
    topic = "go.micro.topic.foo"
)

func pub() {
    tick := time.NewTicker(time.Second)
    i := 0
    for _ = range tick.C {
        msg := &broker.Message{
            Header: map[string]string{
                "id": fmt.Sprintf("%d", i),
            },
            Body: []byte(fmt.Sprintf("%d: %s", i, time.Now().String())),
        }
        if err := broker.Publish(topic, msg); err != nil {
            log.Printf("[pub] failed: %v", err)
        } else {
            fmt.Println("[pub] pubbed message:", string(msg.Body))
        }
        i++
    }
}

func sub() {
    _, err := broker.Subscribe(topic, func(p broker.Event) error {
        fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header)
        return nil
    })
    if err != nil {
        fmt.Println(err)
    }
}

func main() {
    cmd.Init()

    if err := broker.Init(); err != nil {
        log.Fatalf("Broker Init error: %v", err)
    }
    if err := broker.Connect(); err != nil {
        log.Fatalf("Broker Connect error: %v", err)
    }

    go pub()
    go sub()

    <-time.After(time.Second * 10)
}

啟動時新增: --broker=啟動的mq名稱 --broker_address=ip地址

總結:

基本上只需要更改外掛既可切換 mq,可以在github.com/micro/go-plugins/broker包下找到自己需要的mq,匯入使用既可

"github.com/micro/go-plugins/broker/stan"
分享到: