Suppose RABBITMQ is configured with a cluster, and client connection Rabbitmq-server is implemented by LVS, but generally it is not recommended to do LB. In a distributed system environment, due to the non-predictability of the nodes, the use of the Spring AMQP template is not configured to be flexible enough to meet the needs of elastic expansion, so the more convenient way is through rabbitmq native Java Client for subscription and publication. In our scenario, some nodes need to be both Publisher and subscriber for elastic scaling without additional configuration. Take the fanout type as an example, as follows:
Publishing side:
/**
* @Title: Send.java
* @Package COM.CYL.RABBITMQ
* @Description: TODO (describe what the file does in a sentence)
* @author [email protected]
* @date April 25, 2016 12:52:59
* @version V1.0
*/
Package COM.CYL.RABBITMQ;
Import java.io.IOException;
Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;
Import Com.rabbitmq.client.ConnectionFactory;
/**
* @author Zjhua
*
*/
public class Send {
public static void Main (string[] args) throws IOException {
ConnectionFactory factory = new ConnectionFactory ();
Factory.sethost ("localhost");
Connection Connection;
Connection = Factory.newconnection ();
Channel channel = Connection.createchannel ();
Channel.exchangedeclare ("Fanout_random", "fanout");
String message = "Hello World";
for (int i=0;i<10000;i++) {
Channel.basicpublish ("Fanout_random", "", NULL, (Message + i). GetBytes ());
try {
Thread.Sleep (5000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
System.out.println ("[X] Sent '" + Message + "'");
Channel.close ();
Connection.close ();
}
}
Subscribers:
/**
* @Title: Reqv.java
* @Package COM.CYL.RABBITMQ
* @Description: TODO (describe what the file does in a sentence)
* @author [email protected]
* @date April 25, 2016 12:56:33
* @version V1.0
*/
Package COM.CYL.RABBITMQ;
Import java.io.IOException;
Import com.rabbitmq.client.*;
/**
* @author Zjhua
*
*/
public class Reqv {
public static void Main (string[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory ();
Factory.sethost ("localhost");
Connection Connection = Factory.newconnection ();
Channel channel = Connection.createchannel ();
Channel.exchangedeclare ("Fanout_random", "fanout");
String queuename = Channel.queuedeclare (). Getqueue (); - for certain scenarios, such as cache synchronization, a queue using Exclusive/auto-delete is more appropriate
Channel.queuebind (QueueName, "Fanout_random", "" ");
Consumer Consumer = new Defaultconsumer (channel) {
@Override
public void Handledelivery (String consumertag, Envelope Envelope,
Amqp. Basicproperties properties, byte[] body) throws IOException {
String message = new String (Body, "UTF-8");
System.out.println ("[X] Received '" + Message + "'");
}
};
Channel.basicconsume (QueueName, true, consumer);
}
}
If you want to act as both a subscriber and a publisher, configure a listener event when the container starts, including the subscriber logic. The publishing side serves as the base service for the business subsystem.
RABBITMQ release and subscriber in distributed environment