amqproxy 是由cloudamqp公司开源的(此公司专门搞rabbitmq 服务的)开发基于crystal(一个小众语言,但是很不错),
同时今天也转了一片此公司的文章 https://www.cloudamqp.com/blog/2019-05-29-maintaining-long-lived-connections-with-AMQProxy.html
以下是一个简单的环境搭建以及测试
环境准备
- docker-compose文件
集成了prometheus 监控以及amqproxy,包含了两个实例
version: "3"
services:
vmstorage:
image: victoriametrics/vmstorage
ports:
- 8482:8482
- 8400:8482
- 8401:8482
volumes:
- ./strgdata:/storage
command:
- "--storageDataPath=/storage"
vmagent:
image: victoriametrics/vmagent
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- 8429:8429
command:
- -promscrape.config=/etc/prometheus/prometheus.yml
- -remoteWrite.url=http://vminsert:8480/insert/1/prometheus
vminsert:
image: victoriametrics/vminsert
command:
- "--storageNode=vmstorage:8400"
ports:
- 8480:8480
vmselect:
image: victoriametrics/vmselect
command:
- "--storageNode=vmstorage:8401"
ports:
- 8481:8481
grafana:
image: grafana/grafana
ports:
- 3000:3000
rabbitmq:
build: ./
hostname: 1-rabbit
ports:
- "5672:5672"
- "15672:15672"
- "15692:15692"
environment:
- "RABBITMQ_DEFAULT_USER=dalong"
- "RABBITMQ_DEFAULT_PASS=dalong"
- "RABBITMQ_NODENAME=rabbit@1-rabbit"
rabbitmq2:
build: ./
hostname: 2-rabbit
ports:
- "5674:5672"
- "15674:15672"
- "15694:15692"
environment:
- "RABBITMQ_DEFAULT_USER=dalong"
- "RABBITMQ_DEFAULT_PASS=dalong"
- "RABBITMQ_NODENAME=rabbit@2-rabbit"
amqpproxy:
image: dalongrong/amqpproxy:master
ports:
- "5673:5673"
environment:
- "AMQP_URL=amqp://rabbitmq2:5672"
- prometheus 配置
基于vmagent
global:
scrape_interval: 5s
evaluation_interval: 5s
scrape_configs:
- job_name: 'rabbitmq-without'
static_configs:
- targets: ['rabbitmq:15692']
- job_name: 'rabbitmq-with-amqproxy'
static_configs:
- targets: ['rabbitmq2:15692'
- Dockerfile
添加了prometheus 支持
FROM rabbitmq:3.8.5-management-alpine
RUN rabbitmq-plugins enable --offline rabbitmq_prometheus
- amqproxy 配置
这个很简单,基于官方的dockerfile 构建的,主要是AMQP_URL(注意此curl 不需要用户名以及密码)
environment:
- "AMQP_URL=amqp://rabbitmq2:5672"
代码集成使用
- nodejs 代码
package.json
{
"name": "app",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"amqplib": "^0.5.6"
},
"scripts": {
"mq:start1": "node app.js",
"mq:start2": "node app2.js",
"run-all":"npm-run-all --parallel mq:**"
},
"devDependencies": {
"npm-run-all": "^4.1.5"
}
}
app.js 代码
var q = 'tasks';
var open = require('amqplib').connect('amqp://dalong:dalong@127.0.0.1:5672');
// Publisher
open.then(function(conn) {
return conn.createChannel();
}).then(function(ch) {
ch.assertQueue(q).then(function(ok) {
for (var i = 0; i <50000;i++){
ch.sendToQueue(q, Buffer.from('something to do'));
}
});
}).catch(console.warn);
// Consumer
open.then(function(conn) {
return conn.createChannel();
}).then(function(ch) {
return ch.assertQueue(q).then(function(ok) {
return ch.consume(q, function(msg) {
if (msg !== null) {
console.log(msg.content.toString());
ch.ack(msg);
}
});
});
}).catch(console.warn);
说明
具体的使用和普通rabbitmq 链接是一样的,具体amqproxy的优点可以参考上边的链接,关于promehteus 的集成使用,可以直接添加官方的fdashboard
参考资料
https://www.cloudamqp.com/blog/2019-05-29-maintaining-long-lived-connections-with-AMQProxy.html
https://github.com/rongfengliang/rabbitmq-amqproxy-prometheus-learning
https://grafana.com/orgs/rabbitmq