0
votes
[HttpGet]
[Route("api/sender")]
public string Sender(string message, string hostName)
{
  try
    {
       Send("myQ1", message, hostName);
       return "successfully message sent to Queue";
    }
    catch (Exception ex)
    {
       return ex.Message;
    }                     
        
}

public static void Send(string queue, string data,string hostName)
{
   var factory = new ConnectionFactory() { HostName = hostName };
   using (IConnection connection = factory.CreateConnection())
   {
      using (IModel channel = connection.CreateModel())
      {
         channel.QueueDeclare(queue, false, false, false, null);
         channel.BasicPublish(string.Empty, queue, null, Encoding.UTF8.GetBytes(data));
       }
    }
} 

I have configured RabbitMQ cluster on Azure AKS(Hybrid cluster Win+Linux) using helm and then deployed the above code by creating docker image using LoadBalancer Service to access the application. So, my API URL is like :

http://x.x.x.x/api/sender?message=Hi&hostname=rabbit@my-release-rabbitmq-0.my-release-rabbitmq-headless.default.svc.cluster.local

Here I am passing hostname dynamically to this API as : rabbit@my-release-rabbitmq-0.my-release-rabbitmq-headless.default.svc.cluster.local

I have port forwarded from 15672 to 3000 to access RabbitMQ Dashboard from my native computer like below image.

enter image description here

But not able to send the message to Queue.

So, my question is what should I pass as host name in the API I have created(above code) to send message to RabbitMQ queue

Below is my AKS cluster deployment details: enter image description here

1
Are you trying to connect from inside or outside the cluster?coderanger
An API I have deployed on Azure ASK and using the Loadbalancer IP I am calling that API . Now that API is responsible to push message to queue. i.e. pushing the message from inside the clusterRoushan

1 Answers

0
votes

I have modified the code like:

var factory = new ConnectionFactory() { HostName = hostName, UserName="RabbitMQ Username",Password="RabitMQ Password" };

Then I passed the hostname="my-release-rabbitmq-headless"; This is the service name.

Now its working as expected.