Windows下RabbitMQ安装及配置流程

记录下本人在win10环境下安装RabbitMQ的步骤

第一步:下载并安装erlang

      • 下载完是这么个东西:

  • windows 

启动:net stop RabbitMQ && net start RabbitMQ

安装成功之后:可访问http://localhost:15672 

参考项目:https://www.cnblogs.com/ericli-ericli/p/5902270.html

  练手rabbit项目: https://github.com/masnun/gopher-and-rabbit

项目代码 

消费者 consumer/main.go


package main

import (
"encoding/json"
"github.com/masnun/gopher-and-rabbit"
"github.com/streadway/amqp"
"log"
"os"
)

func handleError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}

}

func main() {
conn, err := amqp.Dial(gopher_and_rabbit.Config.AMQPConnectionURL)
handleError(err, "Can't connect to AMQP")
defer conn.Close()

amqpChannel, err := conn.Channel()
handleError(err, "Can't create a amqpChannel")

defer amqpChannel.Close()

queue, err := amqpChannel.QueueDeclare("add", true, false, false, false, nil)
handleError(err, "Could not declare `add` queue")

err = amqpChannel.Qos(1, 0, false)
handleError(err, "Could not configure QoS")

messageChannel, err := amqpChannel.Consume(
queue.Name,
"",
false,
false,
false,
false,
nil,
)
handleError(err, "Could not register consumer")

stopChan := make(chan bool)

go func() {
log.Printf("Consumer ready, PID: %d", os.Getpid())
for d := range messageChannel {
log.Printf("Received a message: %s", d.Body)

addTask := &gopher_and_rabbit.AddTask{}

err := json.Unmarshal(d.Body, addTask)

if err != nil {
log.Printf("Error decoding JSON: %s", err)
}

log.Printf("Result of %d + %d is : %d", addTask.Number1, addTask.Number2, addTask.Number1+addTask.Number2)

if err := d.Ack(false); err != nil {
log.Printf("Error acknowledging message : %s", err)
} else {
log.Printf("Acknowledged message")
}

}
}()

// Stop for program termination
<-stopChan

生产者 publisher/main.go

package main

import (
    "encoding/json"
    "github.com/masnun/gopher-and-rabbit"
    "github.com/streadway/amqp"
    "log"
    "math/rand"
    "time"
)

func handleError(err error, msg string) {
    if err != nil {
        log.Fatalf("%s: %s", msg, err)
    }

}

func main() {
    conn, err := amqp.Dial(gopher_and_rabbit.Config.AMQPConnectionURL)
    handleError(err, "Can't connect to AMQP")
    defer conn.Close()

    amqpChannel, err := conn.Channel()
    handleError(err, "Can't create a amqpChannel")

    defer amqpChannel.Close()

    queue, err := amqpChannel.QueueDeclare("add", true, false, false, false, nil)
    handleError(err, "Could not declare `add` queue")

    rand.Seed(time.Now().UnixNano())

    addTask := gopher_and_rabbit.AddTask{Number1: rand.Intn(999), Number2: rand.Intn(999)}
    body, err := json.Marshal(addTask)
    if err != nil {
        handleError(err, "Error encoding JSON")
    }

    err = amqpChannel.Publish("", queue.Name, false, false, amqp.Publishing{
        DeliveryMode: amqp.Persistent,
        ContentType:  "text/plain",
        Body:         body,
    })

    if err != nil {
        log.Fatalf("Error publishing message: %s", err)
    }

    log.Printf("AddTask: %d+%d", addTask.Number1, addTask.Number2)

}

 

shared.go

package gopher_and_rabbit

type Configuration struct {
    AMQPConnectionURL string
}

type AddTask struct {
    Number1 int
    Number2 int
}

var Config = Configuration{
    AMQPConnectionURL: "amqp://guest:guest@localhost:5672/",
}

 

posted on 2021-06-11 17:19  kevin_yang123  阅读(274)  评论(0编辑  收藏  举报