本文采用mule的组件进行rabbitmq消息的发布实验。
1、参考文档
官方参考示例:示例地址
2、准备工具
名称 | 版本 |
---|---|
AnypointStudio | 6.4.4 |
mule-standalone | 3.8.5 |
AMQP Connector | 下载地址 |
3、外部准备条件
RabbitMQ服务器:启动并开放相应端口;
队列和交换器:创建队列(sales_queue),创建交换器(sales_exchange);
通过RabbitMQ服务器创建队列,名为sales_queue,创建交换器,名为sales_exchange,并将该队列绑定到该交换器上。
4、mule工程
新建mule工程,通过拖拽右侧工具栏组件的方式搭建出完整的工作流,如图:
该工作流的配置文件为:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:amqp="http://www.mulesoft.org/schema/mule/amqp" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/amqp http://www.mulesoft.org/schema/mule/amqp/current/mule-amqp.xsd">
<amqp:connector doc:name="AMQP Connector" name="AMQP_Connector" validateConnections="true"/>
<http:listener-config doc:name="HTTP Listener Configuration" host="localhost" name="HTTP_Listener_Configuration" port="8081"/>
<amqp:connector name="AMQP_0_9_Connector" validateConnections="true" doc:name="AMQP-0-9 Connector"/>
<flow name="json-to-rabbitmqFlow">
<http:listener allowedMethods="POST" config-ref="HTTP_Listener_Configuration" doc:name="Recieve HTTP request" path="/"/>
<logger message="#[message.inboundProperties.'http.query.params'.toString()]" level="INFO" doc:name="Logger"/>
<set-payload value="#[message.inboundProperties.'http.query.params'.toString()]" doc:name="Set Payload"/>
<amqp:outbound-endpoint doc:name="Send to AMQP queue" exchange-pattern="request-response" exchangeName="sales_exchange" queueName="sales_queue" responseTimeout="10000" connector-ref="AMQP_0_9_Connector" exchangeType="direct"/>
</flow>
</mule>
其中,http消息源的配置为:
Logger组件的Message属性设置为:#[message.inboundProperties.'http.query.params'.toString()]
Set Payload组件的Value属性设置为:#[message.inboundProperties.'http.query.params'.toString()]
AMQP组件的配置:
注意要加上amqp和rabbitmq服务器的连接配置:
5、运行测试
启动mule工程:选中项目,右键Run As,选择Mule Application;
发送http请求:使用postman发送post请求到http://localhost:8081,并发送一个json数组作为消息内容,具体如图:
从RabbitMQ查看到队列以存入消息,消息为:
6、完整的消息收发实验
6.1mule工程
6.2配置文件
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:redis="http://www.mulesoft.org/schema/mule/redis" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:amqp="http://www.mulesoft.org/schema/mule/amqp" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/amqp http://www.mulesoft.org/schema/mule/amqp/current/mule-amqp.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/redis http://www.mulesoft.org/schema/mule/redis/current/mule-redis.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<amqp:connector name="AMQP_0_9_Connector" validateConnections="true" doc:name="AMQP-0-9 Connector"/>
<redis:config name="Redis__Configuration" doc:name="Redis: Configuration"/>
<flow name="GetMessageFromQueue">
<amqp:inbound-endpoint queueName="test" queueDurable="true" responseTimeout="10000" exchange-pattern="request-response" connector-ref="AMQP_0_9_Connector" doc:name="AMQP-0-9"/>
<logger message="接收到的消息是:#[message]" level="INFO" doc:name="Logger"/>
<set-payload doc:name="Set Payload"/>
</flow>
<flow name="SendMessageToQueue">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" allowedMethods="POST" doc:name="HTTP"/>
<logger message="要发送的消息是:#[message.inboundProperties.'http.query.params'.toString()]" level="INFO" doc:name="Logger"/>
<set-payload value="#[message.inboundProperties.'http.query.params'.toString()]" doc:name="Set Payload"/>
<amqp:outbound-endpoint queueName="test" queueDurable="true" responseTimeout="10000" connector-ref="AMQP_0_9_Connector" doc:name="AMQP-0-9"/>
<set-payload value="#[message.inboundProperties.'http.query.params'.toString()]" doc:name="Set Payload"/>
</flow>
</mule>
6.3日志采集
项目中的日志使用的是log4j2框架,将日志分三种形式导出:推送到socket的4560端口;打印到控制台;写入文件夹。
log4j2.xml的配置文件为:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Socket name="Socket" host="127.0.0.1" port="4560">
<JsonLayout compact="true" eventEol="true" />
<SerializedLayout />
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %C{2} (%F:%L) %L- %msg%n" />
</Socket>
<Console name="STDOUT" target="SYSTEM_OUT">
<!-- 输出格式 布局-->
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
<File name="MyFile" fileName="F:/data/logs/log4j2test.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %C{2} (%F:%L) %L- %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Socket"/>
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
6.4配置ELK日志采集框架
ELK日志框架的搭建示例:查看网址
其中logstash的配置文件log4j2.conf为
input {
tcp {
port => 4560
codec => json
}
}
filter {
date {
match => [ "timeMillis", "UNIX_MS" ]
}
}
output {
stdout {
codec => rubydebug
}
elasticsearch {
action => "index"
codec => plain { charset => "UTF-8" }
hosts => "localhost:9200"
index => "logstash-%{+YYYY.MM.dd}"
}
}
7、测试运行项目
启动该mule工程后可从日志查看到完成了消息的收发。
注意:这个工程发送的队列是test,需要在客户端新建该队列。