RABBITMQ release and subscriber in distributed environment

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.