GithubHelp home page GithubHelp logo

go-dbevent's Introduction

Go Database Eventing (Publish event using database)

Test

Go library to help publishing events along with transation data atomically

Features include:

Running example

Consumer Example

Start mysql server

./start_server.sh
package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"github.com/pongsatt/go-dbevent"
	"github.com/pongsatt/go-dbevent/driver"
)

// Data represent example json data
type Data struct {
	ID string
}

func main() {
	var nodeID string

	if len(os.Args) > 1 {
		nodeID = os.Args[1]
	} else {
		panic("nodeID required")
	}

	dbConfig := &dbevent.DBConfig{
		Host:     "127.0.0.1",
		Port:     3306,
		DBName:   "testdb",
		User:     "root",
		Password: "my-secret-pw",
	}

	mysqlDriver := driver.NewMySQLEventDriver(dbConfig, &driver.MySQLStoreConfig{NodeID: nodeID})

	eventStore := dbevent.NewStore(mysqlDriver)
	defer eventStore.Close()

	consumer := eventStore.NewConsumer("group1", &dbevent.ConsumerConfig{})
	defer consumer.Close()

	consumer.Consume(func(event *dbevent.Event) error {
		fmt.Printf("got event %d\n", event.ID)
		return nil
	})

	fmt.Println("Consumer ready")

	termChan := make(chan os.Signal)
	signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)

	<-termChan
	fmt.Println("Done")
}

Start consumer

go run example/consumer_highlevel/* node1

Producer Example

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"time"

	// we need it
	_ "github.com/go-sql-driver/mysql"
	"github.com/google/uuid"
	"github.com/pongsatt/go-dbevent"
	"github.com/pongsatt/go-dbevent/driver"
)

// Data represent example json data
type Data struct {
	ID string
}

func main() {
	eventNum := 1

	if len(os.Args) > 1 {
		eventNum, _ = strconv.Atoi(os.Args[1])
	}

	dbConfig := &dbevent.DBConfig{
		Host:     "127.0.0.1",
		Port:     3306,
		DBName:   "testdb",
		User:     "root",
		Password: "my-secret-pw",
	}

	mysqlDriver := driver.NewMySQLEventDriver(dbConfig, &driver.MySQLStoreConfig{})
	if err := mysqlDriver.Provision(); err != nil {
		panic(err)
	}

	defer mysqlDriver.Close()

	for i := 0; i < eventNum; i++ {
		now := time.Now()

		data := &Data{
			ID: uuid.NewString(),
		}

		b, _ := json.Marshal(data)
		event := &dbevent.Event{
			Type:          "testtype",
			AggregateType: "aggType",
			AggregateID:   "agg1",
			Data:          b,
			CreatedAt:     &now,
		}

		if err := mysqlDriver.Create(event); err != nil {
			panic(err)
		}
		fmt.Printf("event '%s' produced\n", data.ID)
	}
}

Run producer

go run example/producer/* 1

Consumer output

got event 1

go-dbevent's People

Contributors

pongsatt avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.