[Code Page] Build your own golang framework step by step from scratch

Posted by sheckel on Mon, 17 Feb 2020 00:13:00 +0100

The goal of this framework is to be a generic framework, and I want it to be big and complete, so I can use it directly as a foundation template for other projects in the future, so I want to continue adding some functionality to it, just write some demo in.For this article, I'll add a queue feature.

nsq

There are many queues, I choose nsq.There are several concepts you need to know to use nsq:

  1. nsqd: Components responsible for maintaining queues, accepting message queuing and delivery;
  2. nsqlookupd: A component that manages the nsq cluster;
  3. The web management component of nsqadmin: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.nsq consumers consume by establishing a channel with nsqd or nsqlookupd and listening for messages in the channel.

After learning some basic concepts of nsq, let's set up the NSQ environment. Here's how to use docker-compose. Here's docker-compose.yaml for nsq.

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, let's simply verify that a message is published to nsq:

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

Browser access: http://localhost:4171/ We can also see the corresponding topic and generation.

Then let's change the code, old rules, and configure it first:

"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 in mq.go to start MQ consumers:

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 it:

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 complete code see: https://github.com/TomatoMr/awesomeframework.

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

Topics: Go network Docker curl github