项目作者: LeKovr

项目描述 :
Stream messages from PG / NATS / File channel to another PG / NATS / File channel
高级语言: Go
项目地址: git://github.com/LeKovr/mqbridge.git
创建时间: 2017-12-24T00:18:57Z
项目社区:https://github.com/LeKovr/mqbridge

开源协议:MIT License

下载


mqbridge

Stream messages from PG/NATS/File channel to another PG/NATS/File channel

Go Reference
GitHub Release
GitHub license

codecov
Test Coverage
Maintainability
GoCard

Data flow

Sending string from source to destination is a very primitive task. But it’s solution might include some usefull technics. MQBridge uses this case for attempting to be example of doing things right.

Message queue systems supported

type producer consumer
file tail(file) println file, data
pg listen ‘event’ select func(data)
nats Subscribe(channel) Publish(channel, data)

Installation

  • Linux: just download & run. See Latest release
  • Docker: docker pull ghcr.io/lekovr/mqbridge

Plugins

mqbridge has 2 build modes - standalone (default) and plugin mode.

To activate plugin mode, run

  1. make plugin-on
  2. make run

You will see Loading plugin in program output (using make run).

To deactivate plugin mode, run

  1. make clean plugin-off

Application config

  1. $ ./mqbridge -h
  2. Usage:
  3. mqbridge [OPTIONS]
  4. Application Options:
  5. --version Show version and exit
  6. --debug Show debug data
  7. MQ Bridge Options:
  8. --delim= Bridge definition delimiter (default: ,)
  9. --path_format= Path format (default: ./%s.so)
  10. --point= Endpoints connect string in form 'tag[:plugin[://dsn]]' (default: io:file)
  11. --bridge= Bridge in form 'in_tag:in_channel:out_tag[:out_channel]' (default: io:src.txt,io:dst.txt)
  12. Help Options:
  13. -h, --help Show this help message

Connect strings

  • file - ${TAG):file://
  • pg - ${TAG}:pg:postgres://user:pass@host:port/db?sslmode=disable
  • nats - ${TAG}:nats:nats://user:pass@host:port

Where ${TAG} is the name of endpoint given in config.

Usage

Producers

mqbridge uses the following as data source:

  • file - tail files named as in_channel
  • pg - listen in_channel
  • nats - subscribe to in_channel

Consumers

mqbridge sends received messages as

  • file - add lines to file named as out_channel
  • pg - calls sql select out_channel(data)
  • nats - publish message to out_channel

pg usage sample

This sample shows how to setup pg -> pg bridge.

  1. Setup pg consumer (db1) for out_channel = bridge (see function name)
  1. create table mqbridge_data (line text);
  2. create or replace function bridge(a text) returns void language plpgsql as
  3. $_$
  4. begin insert into mqbridge_data (line) values(a); end
  5. $_$;
  1. Run mqbridge
  1. ./mqbridge --bridge in:event,out:bridge \
  2. --point in:pg:postgres://op:op@localhost:5432/db0?sslmode=disable \
  3. --point out:pg:postgres://op:op@localhost:5432/db1?sslmode=disable
  1. Run at pg producer db (db0)
  1. notify event, '{"test": 1972}';
  1. See results in consumer db (db1)
  1. select * from mqbridge_data ;
  2. line
  3. ----------------
  4. {"test": 1972}

See also: Examples

Developer notes

This project solves quite simple task, but it shows some more important things named tests.
There are the following tests presented here:

Makefile

Project contains Makefile with the following targets:

  1. $ make
  2. mqbridge Makefile: Stream messages from PG/NATS/File channel to another PG/NATS/File channel
  3. Compile operations
  4. lint run `golint` and `golangci-lint`
  5. vet run `go vet`
  6. test run tests
  7. test-docker run tests that use services from docker-compose.yml
  8. test-docker-self run tests that run docker themselves
  9. cov-html show package coverage in html (make cov-html PKG=counter)
  10. build build app
  11. run build and run in foreground
  12. Plugin support
  13. plugin-on enable plugin mode (this command changes source files)
  14. plugin-off disable plugin mode (this command changes source files back)
  15. Prepare distros
  16. build-standalone build like docker image from scratch
  17. buildall build app for all platforms
  18. dist create disro files
  19. Docker operations
  20. up start service in container
  21. down stop service
  22. docker-build build docker image
  23. docker-clean remove docker image & temp files
  24. dc run $(CMD) via docker-compose
  25. Other
  26. clean clean generated files
  27. update-godoc update docs at pkg.go.dev
  28. update-ghcr update latest docker image tag at ghcr.io
  29. help list Makefile targets (this is default target)

Project stats

Here is the data from cloc

Real code (it does the real job)

Language files blank comment code
Go 12 97 63 677

Made by

  1. cloc --not-match-f=test.go --md . | grep -E "Lang|:--|Go"

All sources

Language files blank comment code
Go 23 200 105 1167
YAML 4 22 12 237
make 2 84 99 205
Markdown 2 68 0 200
Dockerfile 1 7 4 13
SQL 1 1 0 5
XML 1 0 0 1
———— ———— ———— ———— ————
SUM: 34 382 220 1828

Made by

  1. cloc --md .

License

The MIT License (MIT), see LICENSE.

Copyright (c) 2017-2021 Aleksey Kovrizhkin lekovr+mqbridge@gmail.com