Play N kinds of enterprise-level solutions, laugh proud distributed development
** Download address: Baidu Cloud disk **
** Distributed: programmer eternal proposition. Brother Liao has launched a rare Java distributed course on the whole network to teach you the secrets of distributed training, help you build a perfect distributed knowledge system, and help you “hard skills + soft power” to the next level. The course includes distributed ID, distributed Session, distributed task scheduling, distributed flow limiting, sub-database sub-table, distributed transaction six topics, cases cover users, reports, seconds kill, orders and other classic scenarios, enterprises commonly used distributed actual combat cases & solutions, complete package for you! **
** Suitable for the crowd Java engineers interested in distributed students technical reserve requirements familiar with SpringBoot/Http protocol familiar with Mysql/familiar with Docker understanding Redis**
1. Root row message sending and receiving
Simple model, using tacit exchange, send to specified queue, 1 to 1 send receive. Sender –>(acquiesce to Exchange)–>Queue–> receiver
Import java.io.IOException; import java.io.IOException; import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class SendDemo {
private final static String QUEUE_NAME = “hello”;
public static void main(String[] argv) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(“localhost”); Connection connection = null; try { connection = factory.newConnection(); Channel channel = connection.createChannel(); QueueDeclare (QUEUE_NAME, false, false, false, null); String message = “Hello World!” ; BasicPublish (“”, QUEUE_NAME, null, message.getBytes(” utF-8 “)); // Queue channel.basicPublish(“”, QUEUE_NAME, null, message.getBytes(” utF-8 “)); System.out.println(” [x] Sent ‘” + message + “‘”); channel.close(); connection.close(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); }}}
Import java.io.IOException; import java.io.IOException; import java.io. import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.GetResponse;
Public class RecvPull {private final static String QUEUE_NAME = private final static String QUEUE_NAME = private final static String QUEUE_NAME = private final static String QUEUE_NAME = private final static String QUEUE_NAME = private final static String QUEUE_NAME = private final static String QUEUE_NAME = “hello”;
public static void main(String[] argv) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(“localhost”); Connection connection = null; try { connection = factory.newConnection(); Channel channel = connection.createChannel(); QueueDeclare (QUEUE_NAME, false, false, false, null); System.out.println(” [*] Waiting for messages. To exit press CTRL+C”); While (true) {try {thread.sleep (3000); } catch (InterruptedException e) { e.printStackTrace(); } GetResponse response = channel.basicGet(QUEUE_NAME, true); if (response == null) { continue; } String responseStr = new String(response.getBody()); System.out.println(“responseStr: ” + responseStr); } } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); }}}
Import java.io.IOException; import java.io.IOException; import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
** @date 2019年3月8日 3:31:56 */ Public class RecvPush {private final static String QUEUE_NAME = “hello”;
public static void main(String[] argv) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(“localhost”); Connection connection = null; try { connection = factory.newConnection(); Channel channel = connection.createChannel(); QueueDeclare (QUEUE_NAME, false, false, false, null); System.out.println(” [*] Waiting for messages. To exit press CTRL+C”); 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 + “‘”); }}; BasicConsume (QUEUE_NAME, true, consumer); // Start consume the message channel.basicConsume(QUEUE_NAME, true, consumer);
} catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); }}}