Heim  >  Artikel  >  Backend-Entwicklung  >  Fall (IV) – KisFlow-Golang Stream Real – KisFlow in Message Queue (MQ)-Anwendungen

Fall (IV) – KisFlow-Golang Stream Real – KisFlow in Message Queue (MQ)-Anwendungen

WBOY
WBOYOriginal
2024-07-18 03:44:47793Durchsuche

Case (IV) - KisFlow-Golang Stream Real- KisFlow in Message Queue (MQ) Applications

Github: https://github.com/aceld/kis-flow
Dokument: https://github.com/aceld/kis-flow/wiki


Teil1-Übersicht
Teil 2.1 – Projektaufbau / Grundmodule
Teil 2.2 – Projektaufbau / Grundmodule
Teil3-Datenstrom
Teil 4 – Funktionsplanung
Teil5-Stecker
Teil 6 – Konfigurationsimport und -export
Teil 7 – KisFlow-Aktion
Teil 8-Cache/Params Daten-Caching und Datenparameter
Teil 9 – Mehrere Kopien von Flow
Teil 10 – Prometheus-Metrikstatistik
Teil 11 – Adaptive Registrierung von FaaS-Parametertypen basierend auf Reflexion


Fall 1 – Schnellstart
Case2-Flow Parallelbetrieb
Fall 3 – Anwendung von KisFlow in Multi-Goroutinen
Case4-KisFlow in Message Queue (MQ)-Anwendungen

Laden Sie die KisFlow-Quelle herunter

$go get github.com/aceld/kis-flow

KisFlow-Entwicklerdokumentation

KisFlow mit Kafka

Beispielquellcode

https://github.com/aceld/kis-flow-usage/tree/main/12-with_kafka

In diesem Beispiel verwenden wir github.com/segmentio/kafka-go als Kafka-Client-SDK eines Drittanbieters (Entwickler können andere Kafka Go-Tools auswählen).

package main

import (
    "context"
    "fmt"
    "github.com/aceld/kis-flow/file"
    "github.com/aceld/kis-flow/kis"
    "github.com/segmentio/kafka-go"
    "sync"
    "time"
)

func main() {
    ctx := context.Background()

    // Load Configuration from file
    if err := file.ConfigImportYaml("conf/"); err != nil {
        panic(err)
    }

    // Get the flow
    flowOrg := kis.Pool().GetFlow("CalStuAvgScore")
    if flowOrg == nil {
        panic("flowOrg is nil")
    }

    // Create a new Kafka reader
    reader := kafka.NewReader(kafka.ReaderConfig{
        Brokers:     []string{"localhost:9092"},
        Topic:       "SourceStuScore",
        GroupID:     "group1",
        MinBytes:    10e3,                   // 10KB
        MaxBytes:    10e6,                   // 10MB
        MaxWait:     500 * time.Millisecond, // Maximum wait time
        StartOffset: kafka.FirstOffset,
    })

    defer reader.Close()

    var wg sync.WaitGroup
    for i := 0; i < 5; i++ { // Use 5 consumers to consume in parallel
        wg.Add(1)
        go func() {
            // Fork a new flow for each consumer
            flowCopy := flowOrg.Fork(ctx)

            defer wg.Done()
            for {
                // Read a message from Kafka
                message, err := reader.ReadMessage(ctx)
                if err != nil {
                    fmt.Printf("error reading message: %v\n", err)
                    break
                }

                // Commit the message to the flow
                _ = flowCopy.CommitRow(string(message.Value))

                // Run the flow
                if err := flowCopy.Run(ctx); err != nil {
                    fmt.Println("err: ", err)
                    return
                }
            }
        }()
    }

    wg.Wait()

    return
}

func init() {
    // Register functions
    kis.Pool().FaaS("VerifyStu", VerifyStu)
    kis.Pool().FaaS("AvgStuScore", AvgStuScore)
    kis.Pool().FaaS("PrintStuAvgScore", PrintStuAvgScore)
}

KisFlow mit Nsq

Beispielquellcode:

https://github.com/aceld/kis-flow-usage/tree/main/13-with_nsq

Dieser KisFlow-Verbraucher verwendet github.com/nsqio/go-nsq als SDK eines Drittanbieters.

package main

import (
    "context"
    "fmt"
    "github.com/aceld/kis-flow/file"
    "github.com/aceld/kis-flow/kis"
    "github.com/nsqio/go-nsq"
)

func main() {
    ctx := context.Background()

    // Load Configuration from file
    if err := file.ConfigImportYaml("conf/"); err != nil {
        panic(err)
    }

    // Get the flow
    flowOrg := kis.Pool().GetFlow("CalStuAvgScore")
    if flowOrg == nil {
        panic("flowOrg is nil")
    }

    // Create a new NSQ consumer
    config := nsq.NewConfig()
    config.MaxInFlight = 5

    consumer, err := nsq.NewConsumer("SourceStuScore", "channel1", config)
    if err != nil {
        panic(err)
    }

    consumer.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
        // Fork a new flow for each message
        flowCopy := flowOrg.Fork(ctx)

        // Commit the message to the flow
        _ = flowCopy.CommitRow(string(message.Body))

        // Run the flow
        if err := flowCopy.Run(ctx); err != nil {
            fmt.Println("err: ", err)
            return err
        }

        return nil
    }))

    err = consumer.ConnectToNSQLookupd("localhost:4161")
    if err != nil {
        panic(err)
    }

    defer consumer.Stop()

    select {}
}

func init() {
    // Register functions
    kis.Pool().FaaS("VerifyStu", VerifyStu)
    kis.Pool().FaaS("AvgStuScore", AvgStuScore)
    kis.Pool().FaaS("PrintStuAvgScore", PrintStuAvgScore)
}

KisFlow mit RocketMQ

Beispielquellcode:

https://github.com/aceld/kis-flow-usage/tree/main/14-with_rocketmq

Verwendung von github.com/apache/rocketmq-client-go als RocketMQ-Consumer-SDK.

package main

import (
    "context"
    "fmt"
    "github.com/aceld/kis-flow/file"
    "github.com/aceld/kis-flow/kis"
    "github.com/apache/rocketmq-client-go/v2"
    "github.com/apache/rocketmq-client-go/v2/consumer"
    "github.com/apache/rocketmq-client-go/v2/primitive"
)

func main() {
    // Load Configuration from file
    if err := file.ConfigImportYaml("conf/"); err != nil {
        panic(err)
    }

    // Get the flow
    myFlow := kis.Pool().GetFlow("CalStuAvgScore")
    if myFlow == nil {
        panic("myFlow is nil")
    }

    // Create a new RocketMQ consumer
    c, err := rocketmq.NewPushConsumer(
        consumer.WithGroupName("group1"),
        consumer.WithNameServer([]string{"localhost:9876"}),
    )
    if err != nil {
        panic(err)
    }

    err = c.Subscribe("SourceStuScore", consumer.MessageSelector{}, func(ctx context.Context, msgs ...*primitive.MessageExt) (consumer.ConsumeResult, error) {

        for _, msg := range msgs {
            // Commit the message to the flow
            _ = myFlow.CommitRow(string(msg.Body))

        }

        // Run the flow
        if err := myFlow.Run(ctx); err != nil {
            fmt.Println("err: ", err)
            return consumer.ConsumeRetryLater, err
        }

        return consumer.ConsumeSuccess, nil
    })
    if err != nil {
        panic(err)
    }

    err = c.Start()
    if err != nil {
        panic(err)
    }

    defer c.Shutdown()

    select {}
}

Autor: Aceld
GitHub: https://github.com/aceld

KisFlow Open Source-Projektadresse: https://github.com/aceld/kis-flow

Dokument: https://github.com/aceld/kis-flow/wiki


Teil1-Übersicht
Teil 2.1 – Projektaufbau / Grundmodule
Teil 2.2 – Projektaufbau / Grundmodule
Teil3-Datenstrom
Teil 4 – Funktionsplanung
Teil5-Stecker
Teil 6 – Konfigurationsimport und -export
Teil 7 – KisFlow-Aktion
Teil 8-Cache/Params Daten-Caching und Datenparameter
Teil 9 – Mehrere Kopien von Flow
Teil 10 – Prometheus-Metrikstatistik
Teil 11 – Adaptive Registrierung von FaaS-Parametertypen basierend auf Reflexion


Fall 1 – Schnellstart
Case2-Flow Parallelbetrieb
Fall 3 – Anwendung von KisFlow in Multi-Goroutinen
Case4-KisFlow in Message Queue (MQ)-Anwendungen

Das obige ist der detaillierte Inhalt vonFall (IV) – KisFlow-Golang Stream Real – KisFlow in Message Queue (MQ)-Anwendungen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn