Rabbit MQ url配置示例,更多配置细则请查看(需要科学上网)
或者:http://bit.ly/ks8MXK
AMQP::Client.parse_connection_uri("amqp://dev.rabbitmq.com") # => vhost is nil, so default ("/") will be used
AMQP::Client.parse_connection_uri("amqp://dev.rabbitmq.com/") # => vhost is an empty string
AMQP::Client.parse_connection_uri("amqp://dev.rabbitmq.com/%2Fvault") # => vhost is "/vault"
AMQP::Client.parse_connection_uri("amqp://dev.rabbitmq.com/production") # => vhost is "production"
AMQP::Client.parse_connection_uri("amqp://dev.rabbitmq.com/a.b.c") # => vhost is "a.b.c"
AMQP::Client.parse_connection_uri("amqp://dev.rabbitmq.com/foo/bar") # => ArgumentError
NSString *url5 = @"amqp://登录用户名:登录密码@ip地址或域名:端口号/虚拟主机名";
eg:
NSString *url5 = @"amqp://guest:guest@localhost:5672/Vhost";
代码很简单 基本上只要url配置对了,就可以了
发送
let conn = RMQConnection(uri: url5,delegate: RMQConnectionDelegateLogger()
conn.start()
let ch = conn.createChannel()
let q = ch.queue("test_queue_name") //随便起个就可以,与接收时保持一致即可
let message = "Hello iOS!"
guard let data = message.data(using: .utf8) else{
return
}
ch.defaultExchange().publish(data,routingKey: q.name)
print("[x] Send message:\(message)")
conn.close()
接收
let conn = RMQConnection(uri: url5, delegate: RMQConnectionDelegateLogger())
conn.start()
let ch = conn.createChannel()
let q = ch.queue("test_queue_name") //与发送时保持一致即可
print("[*] Waiting for messages.")
q.subscribe({(_ message: RMQMessage) -> Void in
print("[x] Received \(String(data: message.body, encoding: .utf8))")
conn.close()
})
Tips:
服务器给了相关配置之后一直没办法连接成功,尝试通过Android端接入,发现可以正常接入。
Android端配置如下:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("ip地址或域名");
factory.setVirtualHost("/");
factory.setPort(端口号);
factory.setUsername("用户名");
factory.setPassword("密码");
最后发现VirtualHost是"/",按照文档可以忽略,但实际上忽略无法连接成功
正确配置是
let url5 = "amqp://guest:guest@localhost:5672/%2F"
实际上"/"转义之后是%2F,改成这种格式之后就可以正常连接了
用户名,密码,domain,vhost 中如果包含特殊符号都需要经过转义之后再拼接到url中 才能正常访问
let uri = "amqp://\(mq_username):\(mq_password)@\(mq_domain):\(mq_port)/\(mq_vhost)"