[Code articles] Build your own golang framework step by step from scratch (7)

The goal of this framework is to be a general framework. I hope it is large and comprehensive, and it can be used as a basic template for other projects in the future. In this article, I will add a queue function.

nsq

There are many kinds of queues, I choose nsq. Use nsq need to know the following concepts:

  1. nsqd: the component responsible for maintaining the queue, accepting message queuing and delivery;
  2. nsqlookupd: manage the components of the nsq cluster;
  3. nsqadmin: web management component of nsq;
  4. topic: a collection of messages. To generate a message, you need to specify which topic the message belongs to;
  5. channel: a copy of the queue message. The nsq consumer establishes a channel with nsqd or nsqlookupd, and listens for messages in the channel to achieve consumption.

After understanding some basic concepts of nsq, we first set up the nsq environment. Here we still use docker-compose.

version: '3'
services:
  nsqlookupd:
    image: nsqio/nsq
    command: /nsqlookupd
    networks:
      - nsq-network
    hostname: nsqlookupd
    ports:
      - "4161:4161"
      - "4160:4160"
  nsqd:
    image: nsqio/nsq
    command: /nsqd --lookupd-tcp-address=nsqlookupd:4160 --broadcast-address=nsqd
    depends_on:
      - nsqlookupd
    hostname: nsqd
    networks:
      - nsq-network
    ports:
      - "4151:4151"
      - "4150:4150"
  nsqadmin:
    image: nsqio/nsq
    command: /nsqadmin --lookupd-http-address=nsqlookupd:4161
    depends_on:
      - nsqlookupd
    hostname: nsqadmin
    ports:
      - "4171:4171"
    networks:
      - nsq-network

networks:
  nsq-network:
    driver: bridge

After starting the nsq service, we simply verify and publish a message to nsq:

curl -d 'hello awesome' 'http://127.0.0.1:4151/pub?topic=awesome'

Browser visit: http: // localhost: 4171 / , we can also see the corresponding topic and generation.
nsqadmin.png

Then we modify the code, the old rules, first change the configuration:

"nsq_config":
  "topic": "awesome"
  "channel": "ch"
  "nsqlookupd_addr": "127.0.0.1:4161"
type NsqConfig struct {
	Topic          string `yaml:"topic"`
	Channel        string `yaml:"channel"`
	NsqLookupdAddr string `yaml:"nsqlookupd_addr"`
}

Then add a function to start the mq consumer in mq.go:

func StartMqServer() {
	conf := nsq.NewConfig()
	q, _ := nsq.NewConsumer(config.GetConfig().NsqConfig.Topic, config.GetConfig().NsqConfig.Channel, conf)
	q.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
		//do something when you receive a message
		logger.GetLogger().Info("receive", zap.String(config.GetConfig().NsqConfig.Topic, string(message.Body)))
		return nil
	}))
	err := q.ConnectToNSQLookupd(config.GetConfig().NsqConfig.NsqLookupdAddr)
	if err != nil {
		logger.GetLogger().Error("connect to nsqlookupd failed.", zap.Error(err))
		os.Exit(-1)
	}
}

Also in the entry file, start it:

go mq.StartMqServer()

Finally, we write a producer, generate a message, and test:

func TestProducer(t *testing.T)  {
	config := nsq.NewConfig()
	p, err := nsq.NewProducer("127.0.0.1:4150", config)

	if err != nil {
		log.Panic(err)
	}

	err = p.Publish("awesome", []byte("hello awesome"))
	if err != nil {
		log.Panic(err)
	}
}

For the complete code, please see: https://github.com/TomatoMr/awesomeframework .


Welcome to pay attention to my public number: onepunchgo, leave me a message.

image

Published 20 original articles · Likes0 · Visits 764

Guess you like

Origin blog.csdn.net/qq_31362439/article/details/104339693