コード例 #1
0
func main() {
	log.Printf("AMQP URL: %s", config.Conf.AMQPUrl())
	conn, err := amqp.Dial(config.Conf.AMQPUrl())
	utils.FailOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	ch, err := conn.Channel()
	utils.FailOnError(err, "Failed to open a channel")
	defer ch.Close()

	q, err := ch.QueueDeclare(
		"hello", // name
		false,   // durable
		false,   // delete when usused
		false,   // exclusive
		false,   // no-wait
		nil,     // arguments
	)
	utils.FailOnError(err, "Failed to declare a queue")

	msgs, err := ch.Consume(
		q.Name, // queue
		"",     // consumer
		true,   // auto-ack
		false,  // exclusive
		false,  // no-local
		false,  // no-wait
		nil,    // args
	)
	utils.FailOnError(err, "Failed to register a consumer")

	forever := make(chan bool)

	go func() {
		for d := range msgs {
			log.Printf("Received a message: %s", d.Body)
		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}
コード例 #2
0
func main() {
	conf := config.Conf

	client := redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%d", conf.REDIS_HOST, conf.REDIS_PORT),
		Password: conf.REDIS_PASS,
		DB:       conf.REDIS_DB,
	})

	pong, err := client.Ping().Result()
	fmt.Println(pong, err)

	err = client.Set("key", 123, 0).Err()
	utils.FailOnError(err, "Cannot set key to value in Redis")

	client.IncrBy("key", 256)

	val, err := client.Get("key").Result()
	utils.FailOnError(err, "Cannot read key from Redis")
	fmt.Println("key", val)
}
コード例 #3
0
func main() {
	log.Printf("AMQP URL: %s", config.Conf.AMQPUrl())
	conn, err := amqp.Dial(config.Conf.AMQPUrl())
	utils.FailOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	ch, err := conn.Channel()
	utils.FailOnError(err, "Failed to open a channel")
	defer ch.Close()

	q, err := ch.QueueDeclare(
		"hello", // name
		false,   // durable
		false,   // delete when unused
		false,   // exclusive
		false,   // no-wait
		nil,     // arguments
	)
	utils.FailOnError(err, "Failed to declare a queue")

	data := map[string]string{
		"order_id": "1",
		"amount":   "10.00"}

	message := Message{"Update", data}
	body, err := json.Marshal(message)
	utils.FailOnError(err, "Failed to dump message to json")

	err = ch.Publish(
		"",     // exchange
		q.Name, // routing key
		false,  // mandatory
		false,  // immediate
		amqp.Publishing{
			ContentType: "application/json",
			Body:        body,
		})
	log.Printf(" [x] Sent %s", body)
	utils.FailOnError(err, "Failed to publish a message")
}