GoFlow

GoFlow 是一个高性能、可扩展且分布式的 Workflow 框架,允许以有向无环图(DAG)的形式编程定义分布式工作流。它支持将任务分配到多个工作节点上,并通过 Redis 进行负载均衡。GoFlow 提供了丰富的功能,包括工作流的定义、任务的并行执行、监控以及可视化仪表板
  • 特点:
    • 支持单节点和多节点工作流。
    • 支持分支和聚合操作。
    • 可以水平扩展,通过增加实例来分发负载
  • 适用场景:适用于需要分布式任务处理和复杂工作流的场景

https://github.com/trustmaster/goflow

 

package main

import (
	"fmt"
	"github.com/trustmaster/goflow"
)

// Greeter sends greetings
type Greeter struct {
	Name           <-chan string // input port
	Res            chan<- string // output port
}

// Process incoming data
func (c *Greeter) Process() {
	// Keep reading incoming packets
	for name := range c.Name {
		greeting := fmt.Sprintf("Hello, %s!", name)
		// Send the greeting to the output port
		c.Res <- greeting
	}
}

// Printer prints its input on screen
type Printer struct {
	Line <-chan string // inport
}

// Process prints a line when it gets it
func (c *Printer) Process() {
	for line := range c.Line {
		fmt.Println(line)
	}
}

// NewGreetingApp defines the app graph
func NewGreetingApp() *goflow.Graph {
	n := goflow.NewGraph()
	// Add processes to the network
	n.Add("greeter", new(Greeter))
	n.Add("printer", new(Printer))
	// Connect them with a channel
	n.Connect("greeter", "Res", "printer", "Line")
	// Our net has 1 inport mapped to greeter.Name
	n.MapInPort("In", "greeter", "Name")
	return n
}

func main() {
	// Create the network
	net := NewGreetingApp()
	// We need a channel to talk to it
	in := make(chan string)
	net.SetInPort("In", in)
	// Run the net
	wait := goflow.Run(net)
	// Now we can send some names and see what happens
	in <- "John"
	in <- "Boris"
	in <- "Hanna"
	// Send end of input
	close(in)
	// Wait until the net has completed its job
	<-wait
}

 

 

posted on 2025-03-13 17:21  ExplorerMan  阅读(256)  评论(0)    收藏  举报

导航