Giter VIP home page Giter VIP logo

urmq's Introduction

urmq(util of RabbitMQ)

使用 https://github.com/streadway/amqp操作MQ消息, 包括了重连及重发.

使用https://github.com/rs/zerolog操作日志, 每个日志都带有GUID, 方便分析异常.

封装成回调风格.

样例

package main

import (
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/michelia/urmq"
	zlog "github.com/rs/zerolog/log"
)

func main() {
	c := Config{
		Url:              "amqp://guest:[email protected]:5672",
		Exchange:         "test",
		Queue:            "test",
		BindKeys:         []string{"test.#"},
		SendExchange:     "test_send",
		SendExchangeKind: "fanout",
	}
	// 带session的log
	slog := zlog.With().
		Str("service", "rmq_test").
		Logger()
	rmq := NewRMQ(&slog, c)
	go func() {
		rmq.Connect() // 连接
		rmq.Handle(func(msgChan <-chan amqp.Delivery) {
			slog.Print("consumer")
			for d := range msgChan {
				// 操作接受到的消息d
				_ = d
				// ...
			}
			slog.Print("consumer all")
		})
		slog.Print("consumer")
	}()
	rmq.Publish(&slog, []byte(`{"msg":"debug"}`), "", "")
	// 优雅关闭
	sc := make(chan os.Signal, 1)
	signal.Notify(sc,
		syscall.SIGHUP,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGQUIT,
	)
	go func() {
		sig := <-sc
		slog.Print("准备关闭连接")
		rmq.Close()
		slog.Fatal().Interface("Signal", sig).Msg("主动关闭退出")
	}()

	select{}
}

参考

urmq's People

Contributors

michelia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 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.