【问题标题】:Initialize RabbitMQ Exchange with arguments using Powershell使用 Powershell 使用参数初始化 RabbitMQ Exchange
【发布时间】:2015-02-17 01:39:19
【问题描述】:

我正在尝试使用 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 看到,也许这并不容易:

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

【问题讨论】:

    标签: powershell rabbitmq rabbitmq-exchange


    【解决方案1】:

    交换不会绑定到 DLX(死信交换),队列会绑定。如果您执行以下操作,它应该可以工作:

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

      $body = "{""type"":""fanout"",""auto_delete"":false,""durable"":true,""arguments"":[]}"

    2. 使用 DLX 创建队列

    3. 将交换绑定到队列

    【讨论】:

    • 感谢您的回答。不幸的是,它给出了同样的错误
    • 这是我从我的工作设置脚本中创建的一个要点。它做的比你需要的要多,但一切正常,所以你可以拿走你需要的东西。 gist.github.com/jayhilden/e60b26a94cdc7599a0ab
    • 哦,我明白了。这基本上设置了代理的策略,以便死信交换对每个队列都有效。有趣
    • 正确。您可以轻松更新它以在不同的队列上设置不同的 DLX,但我不需要。我在全局范围内设置它,这样我就不必在每个队列上都搞砸了。
    • 不客气。另一个注意事项:我们曾经以这种方式初始化我们的兔子队列和交换,但后来转移到将代码放在 C# 中并在应用程序启动时运行它。如果队列已经存在,那么代码什么也不做,否则它会自动创建所有内容。这对我们来说是一个比在 powershell 中做的更好的解决方案。祝你好运。
    【解决方案2】:

    对于 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")
    

    【讨论】:

      【解决方案3】:

      这是我在 Pluralsight 课程中发现的纯 PowerShell 替代方案 - Michael Stephenson,RabbitMQ for Developers Part 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# 客户端。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-24
        • 2019-07-20
        • 2022-01-19
        • 2015-11-23
        • 1970-01-01
        • 1970-01-01
        • 2017-07-12
        • 2023-03-11
        相关资源
        最近更新 更多