使用Powershell使用参数初始化RabbitMQ Exchange

时间:2015-02-17 01:39:19

标签: powershell rabbitmq rabbitmq-exchange

我正在尝试使用Powershell初始化Exchange。我需要定义一个DLQ,所以我尝试以下语法:

$exchangeURL = $apiURL + "/exchanges/myHost/myExchange";
$body = "{""type"":""fanout"",""auto_delete"":false,""durable"":true,""arguments"":[{""x-dead-letter-exchange"": ""myExchangeDLQ""}]}"
$response = Invoke-WebRequest -Uri $exchangeURL -Headers $headers -Method Put -ContentType "application/json" -Body $body

我收到此错误:

Invoke-WebRequest :
{"error":"bad_request","reason":["unhandled_type",["x-dead-letter-exchange","myExchangeDLQ"]]}
+ $response = Invoke-WebRequest -Uri $exchangeURL -Headers $headers -Method Put -C ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

我在this link看到可能并不那么容易:

  

参数字段在任何地方都被忽略。你不能创建一个队列,   交换或与参数绑定。队列,交换或绑定   参数不会显示那些参数

3 个答案:

答案 0 :(得分:1)

交换不受限于DLX(死信交换),队列也是如此。如果您执行以下操作,它应该可以工作:

  1. 使用空args列表创建交换:

    $ body =" {"" type"":"" fanout"","& #34; auto_delete"":假,""耐用"":真,""参数"&#34 ;: []}"

  2. 使用DLX

  3. 创建队列
  4. 将交换所绑定到队列

答案 1 :(得分:1)

对于RabbitMQClient版本3.5.5,QueueDeclare的arguments参数已从IDictionary更改为IDictionary<string,object>,因此abx78提供的queueArgs参数定义需要稍微更改,否则会出现隐藏的参数计数不匹配powershell抛出的错误

版本3.1.5的旧方式

$queueArgs = @{"x-dead-letter-exchange"="charges_deadletter_exchange";};

新方式

$queueArgs= New-Object "System.Collections.Generic.Dictionary``2[System.String,System.Object]"
$queueArgs.Add("x-dead-letter-exchange", "charges_deadletter_exchange")

答案 2 :(得分:0)

这是我在Pluralsight课程中找到的纯PowerShell替代方案 - Michael Stephenson,RabbitMQ for Developers第2部分。

$RabbitDllPath = "packages\RabbitMQ.Client.3.1.5\lib\net30\RabbitMQ.Client.dll"

$RabbitDllPath = Resolve-Path $RabbitDllPath 
Write-Host "Rabbit DLL Path: " 
Write-Host $RabbitDllPath -foregroundcolor green

set-ExecutionPolicy Unrestricted

$absoluteRabbitDllPath = Resolve-Path $RabbitDllPath

Write-Host "Absolute Rabbit DLL Path: " 
Write-Host $absoluteRabbitDllPath -foregroundcolor green

[Reflection.Assembly]::LoadFile($absoluteRabbitDllPath)

Write-Host "Setting up RabbitMQ Connection Factory"
$factory = new-object RabbitMQ.Client.ConnectionFactory
$hostNameProp = [RabbitMQ.Client.ConnectionFactory].GetField(“HostName”)
$hostNameProp.SetValue($factory, “localhost”)

$usernameProp = [RabbitMQ.Client.ConnectionFactory].GetField(“UserName”)
$usernameProp.SetValue($factory, “guest”)

$passwordProp = [RabbitMQ.Client.ConnectionFactory].GetField(“Password”)
$passwordProp.SetValue($factory, “guest”)

$createConnectionMethod = [RabbitMQ.Client.ConnectionFactory].GetMethod(“CreateConnection”, [Type]::EmptyTypes)
$connection = $createConnectionMethod.Invoke($factory, “instance,public”, $null, $null, $null)

Write-Host "Setting up RabbitMQ Model"
$model = $connection.CreateModel()

Write-Host "Create Dead Letter Exchange"
$exchangeType = [RabbitMQ.Client.ExchangeType]::Fanout
$model.ExchangeDeclare("DeadLetterExchange", $exchangeType, $true)

Write-Host "Creating Dead Letter Queue"
$model.QueueDeclare(“DeadLetter”, $true, $false, $false, $null)
$model.QueueBind("DeadLetter", "DeadLetterExchange", "")

Write-Host "Creating Queue"
$args = @{"x-dead-letter-exchange"="DeadLetterExchange";};
$model.QueueDeclare(“Normal”, $true, $false, $false, $args)

Write-Host "Setup complete"

这种方法利用了C#客户端。