Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8d9928d

Browse files
author
刘军
committed
init
0 parents commit 8d9928d

File tree

23 files changed

+761
-0
lines changed

23 files changed

+761
-0
lines changed

‎.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
node_modules
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/

‎README.md

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
---
2+
layout: post
3+
title: "java/MQ/install rabbitmq on debian and how to using it"
4+
date: 2017年06月23日 10:07:03 +0800
5+
comments: true
6+
categories:
7+
---
8+
9+
# install rabbitmq-server
10+
11+
1. add sources.list
12+
13+
```shell
14+
echo 'deb http://www.rabbitmq.com/debian/ testing main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list
15+
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -
16+
```
17+
18+
2. update & upgrade System
19+
20+
```shell
21+
apt-get update
22+
apt-get upgrade
23+
```
24+
25+
3. install rabbitmq-server
26+
27+
```shell
28+
apt-get install rabbitmq-server
29+
```
30+
31+
# add a new administrator & delete guest
32+
33+
1. list default user list
34+
35+
```sh
36+
rabbitmqctl list_users
37+
```
38+
39+
2.list default vhosts
40+
41+
```sh
42+
rabbitmqctl list_vhosts
43+
```
44+
45+
3. add a new user: liujun & set password & add to administrator group
46+
47+
```shell
48+
rabbitmqctl add_user liujun **********
49+
rabbitmqctl set_permissions -p / liujun ".*" ".*" ".*"
50+
rabbitmqctl set_user_tags liujun administrator
51+
rabbitmqctl list_users
52+
```
53+
54+
55+
# enable plugin: amqp_client rabbitmq_jms_topic_exchange rabbitmq_management rabbitmq_stomp rabbitmq_web_mqtt rabbitmq_web_stomp sockjs
56+
57+
```shell
58+
cd /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.10
59+
sbin/rabbitmq-plugins enable --offline --online amqp_client rabbitmq_jms_topic_exchange rabbitmq_management rabbitmq_stomp rabbitmq_web_mqtt rabbitmq_web_stomp sockjs
60+
/etc/init.d/rabbitmq-server restart
61+
netstat -ant | grep 672
62+
63+
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN
64+
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN
65+
tcp6 0 0 :::5672 :::* LISTEN
66+
```
67+
68+
# add a empty rabbitmq.config
69+
70+
```shell
71+
echo []. > /etc/rabbitmq/rabbitmq.config
72+
```
73+
74+
# restart rabbitmq-server
75+
76+
```shell
77+
/etc/init.d/rabbitmq-server restart
78+
```
79+
# config rabbitmq in spring
80+
81+
1. create a websocket message broker
82+
83+
```java
84+
@Configuration
85+
@EnableWebSocketMessageBroker
86+
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
87+
88+
@Override
89+
public void configureMessageBroker(MessageBrokerRegistry config) {
90+
config.enableStompBrokerRelay("/topic/");
91+
config.setApplicationDestinationPrefixes("/app");
92+
}
93+
94+
@Override
95+
public void registerStompEndpoints(StompEndpointRegistry registry) {
96+
registry.addEndpoint("/quakesep").withSockJS();//endpoint
97+
}
98+
}
99+
```
100+
101+
2. create a controller & template
102+
103+
```java
104+
@Controller
105+
public class DashboardController {
106+
@RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
107+
public String index(){
108+
return "dashboard/index";
109+
}
110+
}
111+
```
112+
113+
```html
114+
<!DOCTYPE html>
115+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
116+
<head>
117+
<meta charset="UTF-8"/>
118+
<title></title>
119+
120+
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" href="../../static/css/bootstrap.min.css"/>
121+
<link rel="stylesheet" th:href="@{/css/bootstrap-theme.css}" href="../../static/css/bootstrap-theme.css"/>
122+
<link rel="stylesheet" th:href="@{/css/application.css}" href="../../static/css/application.css"/>
123+
</head>
124+
<body>
125+
126+
<script th:src="@{/js/jquery-1.7.2.js}" src="../../js/jquery-1.7.2.js"></script>
127+
<script th:src="@{/js/moment.min.js}" src="../../js/moment.min.js"></script>
128+
129+
</body>
130+
</html>
131+
```
132+
133+
3. create gateway xml & rabbit config
134+
135+
```xml
136+
<?xml version="1.0" encoding="UTF-8"?>
137+
<beans xmlns="http://www.springframework.org/schema/beans"
138+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
139+
xmlns:int="http://www.springframework.org/schema/integration"
140+
xmlns:int-http="http://www.springframework.org/schema/integration/http"
141+
xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp"
142+
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
143+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
144+
http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd
145+
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
146+
147+
<import resource="rabbit-context.xml"/>
148+
<!--<int:inbound-channel-adapter channel="quakeinfotrigger" expression="''">-->
149+
<!--<int:poller fixed-delay="1000"/>-->
150+
<!--</int:inbound-channel-adapter>-->
151+
152+
<!--<int:channel id="quakeinfo"/>-->
153+
154+
<!--<int:channel id="quakeinfotrigger"/>-->
155+
156+
<!--<int-http:outbound-gateway id="quakerHttpGateway"-->
157+
<!--request-channel="quakeinfotrigger"-->
158+
<!--url="http://localhost:8180/stomp/quake/json"-->
159+
<!--http-method="GET"-->
160+
<!--expected-response-type="java.lang.String"-->
161+
<!--charset="UTF-8"-->
162+
<!--reply-channel="quakeinfo">-->
163+
<!--</int-http:outbound-gateway>-->
164+
<!--<int-amqp:outbound-channel-adapter amqp-template="amqpTemplate" channel="quakeinfo"/>-->
165+
</beans>
166+
```
167+
168+
```xml
169+
<?xml version="1.0" encoding="UTF-8"?>
170+
<beans xmlns="http://www.springframework.org/schema/beans"
171+
172+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
173+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
174+
175+
<rabbit:connection-factory id="connectionFactory"
176+
username="liujun" host="localhost" password="*********" port="5672"/>
177+
<rabbit:template id="amqpTemplate"
178+
connection-factory="connectionFactory"
179+
exchange="amq.topic"
180+
routing-key="quakes.all"
181+
channel-transacted="true"/>
182+
<rabbit:admin connection-factory="connectionFactory"/>
183+
184+
<rabbit:topic-exchange name="amq.topic"/>
185+
186+
</beans>
187+
```
188+
189+
190+
4. config application
191+
192+
```java
193+
@ComponentScan
194+
@EnableAutoConfiguration
195+
@ImportResource("classpath:httpgateway.xml")
196+
public class Main {
197+
public static void main(String[] args) {
198+
SpringApplication.run(Main.class, args);
199+
}
200+
}
201+
```
202+
203+
204+
# send message to rabbitmq using topic
205+
206+
because I using node js to send message into broker & i using amqplib, so:
207+
208+
```shell
209+
npm i amqplib --save
210+
```
211+
212+
```javascript
213+
var amqp = require('./node_modules/amqplib/callback_api');
214+
215+
amqp.connect('amqp://liujun:*************@localhost', function(err, conn) {
216+
conn.createChannel(function(err, ch) {
217+
if (err){
218+
setTimeout(function() { conn.close(); process.exit(0) }, 500);
219+
}
220+
221+
setInterval(function(){
222+
pub(ch);
223+
}, 1000);
224+
});
225+
226+
function pub(ch){
227+
var ex = 'amq.topic';
228+
var key = 'quakes.all';
229+
var msg = new Date().getTime().toString();
230+
231+
ch.assertExchange(ex, 'topic', {durable: true});
232+
ch.publish(ex, key, new Buffer(msg));
233+
234+
report(key, msg);
235+
}
236+
237+
function report(key, msg){
238+
console.log(" [x] Sent %s:'%s'", key, msg);
239+
}
240+
241+
242+
});
243+
```
244+
245+
# subscribe topic with socketjs
246+
247+
inject stomp.js & sockjs into html template:
248+
249+
```html
250+
<script th:src="@{/js/sockjs-0.3.4.js}" src="../../js/sockjs-0.3.4.js"></script>
251+
<script th:src="@{/js/stomp.js}" src="../../js/stomp.js"></script>
252+
253+
```
254+
255+
```javascript
256+
/*<![CDATA[*/
257+
$(document).ready(function() {
258+
connect();
259+
});
260+
var stompClient = null;
261+
262+
263+
function connect() {
264+
var socket = new SockJS('/quakesep');//连接endpoint
265+
stompClient = Stomp.over(socket);
266+
stompClient.connect({}, function(frame) {
267+
console.log('Connected: ' + frame);
268+
stompClient.subscribe('/topic/quakes.all', function(message){
269+
showQuakeInfo(message.body);
270+
});
271+
});
272+
}
273+
274+
275+
function showQuakeInfo(quakeCollectionJson) {
276+
var quakeCollection = $.parseJSON(quakeCollectionJson);
277+
var name = quakeCollection.name
278+
console.log(name);
279+
}
280+
/*]]>*/
281+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /