276 lines
8.6 KiB
HTML
276 lines
8.6 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="generator" content="Asciidoctor 1.5.8">
|
|
<title>Partitioning with the RabbitMQ Binder</title>
|
|
<link rel="stylesheet" href="css/spring.css">
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
|
|
|
|
<style>
|
|
.hidden {
|
|
display: none;
|
|
}
|
|
|
|
.switch {
|
|
border-width: 1px 1px 0 1px;
|
|
border-style: solid;
|
|
border-color: #7a2518;
|
|
display: inline-block;
|
|
}
|
|
|
|
.switch--item {
|
|
padding: 10px;
|
|
background-color: #ffffff;
|
|
color: #7a2518;
|
|
display: inline-block;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.switch--item:not(:first-child) {
|
|
border-width: 0 0 0 1px;
|
|
border-style: solid;
|
|
border-color: #7a2518;
|
|
}
|
|
|
|
.switch--item.selected {
|
|
background-color: #7a2519;
|
|
color: #ffffff;
|
|
}
|
|
</style>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"></script>
|
|
<script type="text/javascript">
|
|
function addBlockSwitches() {
|
|
$('.primary').each(function() {
|
|
primary = $(this);
|
|
createSwitchItem(primary, createBlockSwitch(primary)).item.addClass("selected");
|
|
primary.children('.title').remove();
|
|
});
|
|
$('.secondary').each(function(idx, node) {
|
|
secondary = $(node);
|
|
primary = findPrimary(secondary);
|
|
switchItem = createSwitchItem(secondary, primary.children('.switch'));
|
|
switchItem.content.addClass('hidden');
|
|
findPrimary(secondary).append(switchItem.content);
|
|
secondary.remove();
|
|
});
|
|
}
|
|
|
|
function createBlockSwitch(primary) {
|
|
blockSwitch = $('<div class="switch"></div>');
|
|
primary.prepend(blockSwitch);
|
|
return blockSwitch;
|
|
}
|
|
|
|
function findPrimary(secondary) {
|
|
candidate = secondary.prev();
|
|
while (!candidate.is('.primary')) {
|
|
candidate = candidate.prev();
|
|
}
|
|
return candidate;
|
|
}
|
|
|
|
function createSwitchItem(block, blockSwitch) {
|
|
blockName = block.children('.title').text();
|
|
content = block.children('.content').first().append(block.next('.colist'));
|
|
item = $('<div class="switch--item">' + blockName + '</div>');
|
|
item.on('click', '', content, function(e) {
|
|
$(this).addClass('selected');
|
|
$(this).siblings().removeClass('selected');
|
|
e.data.siblings('.content').addClass('hidden');
|
|
e.data.removeClass('hidden');
|
|
});
|
|
blockSwitch.append(item);
|
|
return {'item': item, 'content': content};
|
|
}
|
|
|
|
$(addBlockSwitches);
|
|
</script>
|
|
|
|
</head>
|
|
<body class="book toc2 toc-left">
|
|
<div id="header">
|
|
<div id="toc" class="toc2">
|
|
<div id="toctitle">Table of Contents</div>
|
|
<ul class="sectlevel1">
|
|
<li><a href="#_partitioning_with_the_rabbitmq_binder">Partitioning with the RabbitMQ Binder</a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div id="content">
|
|
<div class="sect1">
|
|
<h2 id="_partitioning_with_the_rabbitmq_binder"><a class="link" href="#_partitioning_with_the_rabbitmq_binder">Partitioning with the RabbitMQ Binder</a></h2>
|
|
<div class="sectionbody">
|
|
<div class="paragraph">
|
|
<p>RabbitMQ does not support partitioning natively.</p>
|
|
</div>
|
|
<div class="paragraph">
|
|
<p>Sometimes, it is advantageous to send data to specific partitions — for example, when you want to strictly order message processing, all messages for a particular customer should go to the same partition.</p>
|
|
</div>
|
|
<div class="paragraph">
|
|
<p>The <code>RabbitMessageChannelBinder</code> provides partitioning by binding a queue for each partition to the destination exchange.</p>
|
|
</div>
|
|
<div class="paragraph">
|
|
<p>The following Java and YAML examples show how to configure the producer:</p>
|
|
</div>
|
|
<div class="listingblock">
|
|
<div class="title">Producer</div>
|
|
<div class="content">
|
|
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@SpringBootApplication
|
|
@EnableBinding(Source.class)
|
|
public class RabbitPartitionProducerApplication {
|
|
|
|
private static final Random RANDOM = new Random(System.currentTimeMillis());
|
|
|
|
private static final String[] data = new String[] {
|
|
"abc1", "def1", "qux1",
|
|
"abc2", "def2", "qux2",
|
|
"abc3", "def3", "qux3",
|
|
"abc4", "def4", "qux4",
|
|
};
|
|
|
|
public static void main(String[] args) {
|
|
new SpringApplicationBuilder(RabbitPartitionProducerApplication.class)
|
|
.web(false)
|
|
.run(args);
|
|
}
|
|
|
|
@InboundChannelAdapter(channel = Source.OUTPUT, poller = @Poller(fixedRate = "5000"))
|
|
public Message<?> generate() {
|
|
String value = data[RANDOM.nextInt(data.length)];
|
|
System.out.println("Sending: " + value);
|
|
return MessageBuilder.withPayload(value)
|
|
.setHeader("partitionKey", value)
|
|
.build();
|
|
}
|
|
|
|
}</code></pre>
|
|
</div>
|
|
</div>
|
|
<div class="listingblock">
|
|
<div class="title">application.yml</div>
|
|
<div class="content">
|
|
<pre class="highlightjs highlight"><code class="language-yaml hljs" data-lang="yaml"> spring:
|
|
cloud:
|
|
stream:
|
|
bindings:
|
|
output:
|
|
destination: partitioned.destination
|
|
producer:
|
|
partitioned: true
|
|
partition-key-expression: headers['partitionKey']
|
|
partition-count: 2
|
|
required-groups:
|
|
- myGroup</code></pre>
|
|
</div>
|
|
</div>
|
|
<div class="admonitionblock note">
|
|
<table>
|
|
<tr>
|
|
<td class="icon">
|
|
<i class="fa icon-note" title="Note"></i>
|
|
</td>
|
|
<td class="content">
|
|
<div class="paragraph">
|
|
<p>The configuration in the prececing example uses the default partitioning (<code>key.hashCode() % partitionCount</code>).
|
|
This may or may not provide a suitably balanced algorithm, depending on the key values.
|
|
You can override this default by using the <code>partitionSelectorExpression</code> or <code>partitionSelectorClass</code> properties.</p>
|
|
</div>
|
|
<div class="paragraph">
|
|
<p>The <code>required-groups</code> property is required only if you need the consumer queues to be provisioned when the producer is deployed.
|
|
Otherwise, any messages sent to a partition are lost until the corresponding consumer is deployed.</p>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
<div class="paragraph">
|
|
<p>The following configuration provisions a topic exchange:</p>
|
|
</div>
|
|
<div class="imageblock">
|
|
<div class="content">
|
|
<img src="./images/part-exchange.png" alt="part exchange">
|
|
</div>
|
|
</div>
|
|
<div class="paragraph">
|
|
<p>The following queues are bound to that exchange:</p>
|
|
</div>
|
|
<div class="imageblock">
|
|
<div class="content">
|
|
<img src="./images/part-queues.png" alt="part queues">
|
|
</div>
|
|
</div>
|
|
<div class="paragraph">
|
|
<p>The following bindings associate the queues to the exchange:</p>
|
|
</div>
|
|
<div class="imageblock">
|
|
<div class="content">
|
|
<img src="./images/part-bindings.png" alt="part bindings">
|
|
</div>
|
|
</div>
|
|
<div class="paragraph">
|
|
<p>The following Java and YAML examples continue the previous examples and show how to configure the consumer:</p>
|
|
</div>
|
|
<div class="listingblock">
|
|
<div class="title">Consumer</div>
|
|
<div class="content">
|
|
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@SpringBootApplication
|
|
@EnableBinding(Sink.class)
|
|
public class RabbitPartitionConsumerApplication {
|
|
|
|
public static void main(String[] args) {
|
|
new SpringApplicationBuilder(RabbitPartitionConsumerApplication.class)
|
|
.web(false)
|
|
.run(args);
|
|
}
|
|
|
|
@StreamListener(Sink.INPUT)
|
|
public void listen(@Payload String in, @Header(AmqpHeaders.CONSUMER_QUEUE) String queue) {
|
|
System.out.println(in + " received from queue " + queue);
|
|
}
|
|
|
|
}</code></pre>
|
|
</div>
|
|
</div>
|
|
<div class="listingblock">
|
|
<div class="title">application.yml</div>
|
|
<div class="content">
|
|
<pre class="highlightjs highlight"><code class="language-yaml hljs" data-lang="yaml"> spring:
|
|
cloud:
|
|
stream:
|
|
bindings:
|
|
input:
|
|
destination: partitioned.destination
|
|
group: myGroup
|
|
consumer:
|
|
partitioned: true
|
|
instance-index: 0</code></pre>
|
|
</div>
|
|
</div>
|
|
<div class="admonitionblock important">
|
|
<table>
|
|
<tr>
|
|
<td class="icon">
|
|
<i class="fa icon-important" title="Important"></i>
|
|
</td>
|
|
<td class="content">
|
|
The <code>RabbitMessageChannelBinder</code> does not support dynamic scaling.
|
|
There must be at least one consumer per partition.
|
|
The consumer’s <code>instanceIndex</code> is used to indicate which partition is consumed.
|
|
Platforms such as Cloud Foundry can have only one instance with an <code>instanceIndex</code>.
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript" src="js/tocbot/tocbot.min.js"></script>
|
|
<script type="text/javascript" src="js/toc.js"></script>
|
|
<link rel="stylesheet" href="js/highlight/styles/atom-one-dark-reasonable.min.css">
|
|
<script src="js/highlight/highlight.min.js"></script>
|
|
<script>hljs.initHighlighting()</script>
|
|
</body>
|
|
</html> |