|
1 | 1 | # RabbitMQ configuration
|
2 | 2 |
|
| 3 | +## Basic configuration |
| 4 | + |
3 | 5 | To connect to a RabbitMQ server, it is necessary to instantiate `IQueueService` and configure it to use a desired endpoint, credentials and other valuable connection settings.
|
4 | 6 | `IQueueService` allows clients to configure queues to exchange bindings, and to consume and produce messages in different ways (sync or async, with or without delay). To add `IQueueService` in your application simply use the `AddRabbitMqClient` extension method as in the example below.
|
5 | 7 |
|
@@ -48,7 +50,7 @@ A RabbitMQ client can be configured via a configuration section located in the `
|
48 | 50 | ```
|
49 | 51 |
|
50 | 52 | A RabbitMQ connection can be configured with properties:
|
51 |
| -- `HostName` - RabbitMQ server, |
| 53 | +- `HostName` - RabbitMQ server, |
52 | 54 | - `HostNames` - collection of RabbitMQ hostnames,
|
53 | 55 | - `TcpEndpoints` - collection of AMPQ TCP endpoints,
|
54 | 56 | - `Port` - port RabbitMQ running on,
|
@@ -89,6 +91,8 @@ For high availability RabbitMQ clusters with multiple nodes you can set hosts co
|
89 | 91 | }
|
90 | 92 | ```
|
91 | 93 |
|
| 94 | +## Multiple nodes configuration |
| 95 | + |
92 | 96 | If nodes are running on different hosts with different ports you have an option of configuring that via `TcpEndpoints`.
|
93 | 97 |
|
94 | 98 | ```json
|
@@ -149,4 +153,81 @@ public class Startup
|
149 | 153 |
|
150 | 154 | There is also the `AddRabbitMqClientTransient` method which takes `RabbitMqClientOptions`.
|
151 | 155 |
|
| 156 | +## Ssl configuration |
| 157 | + |
| 158 | +In case you want to establish an ssl connection you can use advanced `TcpEndpoints` configuration. |
| 159 | +`RabbitMqTcpEndpoint` has following properties: |
| 160 | +- `HostName` - RabbitMQ server. |
| 161 | +- `Port` - tcp connection port. The default ssl port is 5671. |
| 162 | +- `SslOption` - ssl options model `RabbitMqSslOption`. |
| 163 | + |
| 164 | +Ssl options model `RabbitMqSslOption` consists of: |
| 165 | +- `ServerName` - canonical server name (CA). |
| 166 | +- `CertificatePath` - path to your certificate (a key store). |
| 167 | +- `CertificatePassphrase` - passphrase for client certificate. |
| 168 | +- `Enabled` - flag that defines if certificate should be used. The default value is true. |
| 169 | +- `AcceptablePolicyErrors` - acceptable policy errors. Flags enum `SslPolicyErrors`. The default value is null. |
| 170 | + |
| 171 | +The key part is to make a proper configuration - right `ServerName`, `CertificatePath` (if needed) and `AcceptablePolicyErrors`. |
| 172 | + |
| 173 | +```c# |
| 174 | +var rabbitMqConfiguration = new RabbitMqClientOptions |
| 175 | +{ |
| 176 | + UserName = "guest", |
| 177 | + Password = "guest", |
| 178 | + TcpEndpoints = new List<RabbitMqTcpEndpoint> |
| 179 | + { |
| 180 | + new RabbitMqTcpEndpoint |
| 181 | + { |
| 182 | + HostName = "127.0.0.1", |
| 183 | + Port = 5671, |
| 184 | + SslOption = new RabbitMqSslOption |
| 185 | + { |
| 186 | + Enabled = true, |
| 187 | + ServerName = "yourCA", |
| 188 | + CertificatePath = "/path/tp/client-key-store.p12", |
| 189 | + CertificatePassphrase = "yourPathPhrase", |
| 190 | + AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateChainErrors | SslPolicyErrors.RemoteCertificateNameMismatch |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | +}; |
| 195 | +``` |
| 196 | + |
| 197 | +The same configuration can be in appsettings.json file. |
| 198 | + |
| 199 | +```json |
| 200 | +{ |
| 201 | + "RabbitMq": { |
| 202 | + "TcpEndpoints": [ |
| 203 | + { |
| 204 | + "HostName": "127.0.0.1", |
| 205 | + "Port": 5671, |
| 206 | + "SslOption": { |
| 207 | + "Enabled": true, |
| 208 | + "ServerName": "yourCA", |
| 209 | + "CertificatePath": "/path/tp/client-key-store.p12", |
| 210 | + "CertificatePassphrase": "yourPathPhrase", |
| 211 | + "AcceptablePolicyErrors": "RemoteCertificateChainErrors, RemoteCertificateNameMismatch" |
| 212 | + } |
| 213 | + } |
| 214 | + ], |
| 215 | + "UserName": "guest", |
| 216 | + "Password": "guest" |
| 217 | + } |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +Map that configuration using standard `GetSection` method. |
| 222 | + |
| 223 | +```c# |
| 224 | +var rabbitMqConfiguration = Configuration.GetSection("RabbitMq"); |
| 225 | +``` |
| 226 | + |
| 227 | +And pass it to the `AddRabbitMqClient` extension method as always. |
| 228 | + |
| 229 | +```c# |
| 230 | +services.AddRabbitMqClient(rabbitMqConfiguration); |
| 231 | +``` |
| 232 | + |
152 | 233 | For the exchange configuration see the [Next page](exchange-configuration.md)
|
0 commit comments