rabbitmq
来源:3-6 RabbitMQ工作模式---Simple模式(下)
szuxxy
2019-12-16
这边rabbitmq出了点问题但不知道是哪里的问题。
GOROOT=C:Go #gosetup
GOPATH=G:lanuage_categorygo #gosetup
C:Goingo.exe build -o C:UsersanmutAppDataLocalTemp___go_build_SimplePublish_go.exe G:/lanuage_category/go/src/rabbitmq20191121/SimplePublish.go #gosetup
C:UsersanmutAppDataLocalTemp___go_build_SimplePublish_go.exe #gosetup
err是:dial tcp 129.211.78.6:5672: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.,出错的位置是:连接connection失败panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x1 addr=0x14 pc=0x5bdedc]
goroutine 1 [running]:
sync.(*Mutex).Lock(...)
C:/Go/src/sync/mutex.go:74
github.com/streadway/amqp.(*Connection).allocateChannel(0x0, 0x0, 0x0, 0x0)
G:/lanuage_category/go/pkg/mod/github.com/streadway/amqp@v0.0.0-20190827072141-edfb9018d271/connection.go:596 +0x5c
github.com/streadway/amqp.(*Connection).openChannel(0x0, 0x692240, 0xc000086140, 0x651efe)
G:/lanuage_category/go/pkg/mod/github.com/streadway/amqp@v0.0.0-20190827072141-edfb9018d271/connection.go:626 +0x39
github.com/streadway/amqp.(*Connection).Channel(...)
G:/lanuage_category/go/pkg/mod/github.com/streadway/amqp@v0.0.0-20190827072141-edfb9018d271/connection.go:653
rabbitmq20181121/RabbitMq.NewRabbitMQSimple(0x64d63c, 0x2, 0xc00007bf50)
G:/lanuage_category/go/src/rabbitmq20191121/RabbitMq/RabbitMq.go:53 +0x178
main.main()
G:/lanuage_category/go/src/rabbitmq20191121/SimplePublish.go:9 +0x41
Process finished with exit code 2
RabbitMq.go的代码
package RabbitMq
import (
"fmt"
"github.com/streadway/amqp"
"log"
)
//连接信息
const MQURL = "amqp://du:du@129.211.78.6:5672/dudevirtualhost"
//RabbitMQ结构体
type RabbitMQ struct {
//连接
conn *amqp.Connection
channel *amqp.Channel
//队列
QueueName string
//交换机名称
ExChange string
//绑定的key名称
Key string
//连接的信息上面已经定义好了
MqUrl string
}
//创建结构体实例参数队列名称、交换机名称和bind的key也就是几个大写的除去定义好的常量信息
func NewRabbitMQ(queueName string, exChange string, key string) *RabbitMQ {
return &RabbitMQ{QueueName: queueName, ExChange: exChange, Key: key, MqUrl: MQURL}
}
//关闭conn和chanel的方法
func (r *RabbitMQ) Destory() {
r.channel.Close()
r.conn.Close()
}
//错误的函数处理
func (r *RabbitMQ) failOnErr(err error, message string) {
if err != nil {
fmt.Printf("err是:%s,出错的位置是:%s", err, message)
}
}
//创建简单模式下的实例只需要queueName这个参数其中exchange是默认的key则不需要。
func NewRabbitMQSimple(queueName string) *RabbitMQ {
rabbitmq := NewRabbitMQ(queueName, "", "")
var err error
//获取参数connection
rabbitmq.conn, err = amqp.Dial(rabbitmq.MqUrl)
rabbitmq.failOnErr(err, "连接connection失败")
//获取channel参数
rabbitmq.channel, err = rabbitmq.conn.Channel()
rabbitmq.failOnErr(err, "获取channel参数失败")
return rabbitmq
}
//直接模式,生产者.
func (r *RabbitMQ) PublishSimple(message string) {
//第一步申请队列如不存在则自动创建之存在则路过。
_, err := r.channel.QueueDeclare(
r.QueueName,
false,
false,
false,
false,
nil,
)
if err != nil {
fmt.Printf("创建连接队列失败%s", err)
}
//第二步发送消息到队列中
r.channel.Publish(
r.ExChange,
r.QueueName,
false,
false,
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(message),
})
}
func (r *RabbitMQ) ConsumeSimple() {
//第一步,申请队列,如果队列不存在则自动创建,存在则跳过
q, err := r.channel.QueueDeclare(
r.QueueName,
//是否持久化
false,
//是否自动删除
false,
//是否具有排他性
false,
//是否阻塞处理
false,
//额外的属性
nil,
)
if err != nil {
fmt.Println(err)
}
//第二步,接收消息
msgs, err := r.channel.Consume(
q.Name,
"", //用来区分多个消费者
true, //是否自动应答,告诉我已经消费完了
false,
false, //若设置为true,则表示为不能将同一个connection中发送的消息传递给这个connection中的消费者.
false, //消费队列是否设计阻塞
nil,
)
if err != nil {
fmt.Printf("消费者接收消息出现问题:%s", err)
}
//启用协程处理消息
go func() {
for d := range msgs {
log.Printf("接收到了消息:%s", d.Body)
}
}()
}
rabbitmq也可以正常访问的
写回答
1回答
-
这个问题好像是你连不上rabbitmq,具体排查rabbitmq服务器端口是否能正常访问,账号是否正常
032019-12-16
相似问题