logo
Alauda Streaming Service for RabbitMQ
logo
Alauda Streaming Service for RabbitMQ
Navigation
Introduction
Release Notes
Lifecycle Policy
Install
Upgrade
Architecture
Concepts

Guides

Create Instance
Delete Instance
User Management
Parameter Configuration
Access Methods
Update Specification
Scheduling Configuration
Monitoring and Alerts
Log
Start & Stop Instance
Restart Instance

HowTo

Client Connection
Patch Version Upgrade

Plugin

Using RabbitMQ Management UI
Permissions

API Reference

Kubernetes APIs

RabbitMQ APIs

RabbitMQ
📝 Edit this page on GitHub
Previous PageHowTo
Next PagePatch Version Upgrade

#Client Connection

There are various connection methods for business applications to access RabbitMQ instances. This section describes how to access RabbitMQ instances using Java and Python.

#TOC

#Applicable Scenarios

Access MethodApplicable Scenario
Access within ClusterApplications within the cluster access RabbitMQ instances through ports exposed by SVC.
Access Outside ClusterApplications outside the cluster access RabbitMQ instances through ports of NodePort.
Access the management portal via the appropriate instance port.

#Connecting to RabbitMQ

Prerequisites

  1. Refer to Access Methods to obtain the specific access address.
  2. Refer to User Management to get the username and password for the instance.
Tip

Make sure to replace <username> and <password> in the code with the actual account information for the instance.

Ensure to replace <ip> and <port> in the code with the actual access address of the instance. In the case of access from outside the cluster, there may be multiple access addresses; you can fill them all in to enhance connection reliability.

Java
Python

The Java access example is as follows:

import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
import java.util.HashMap;
import java.util.UUID;

public class ProducerTest {
    public static void main(String[] args) throws Exception {
        Address[] addresses = {
            new Address(<ip 1>,<port 1>),
            new Address(<ip 2>,<port 2>),
            new Address(<ip 3>,<port 3>)
        };
        String username = "<username>";
        String password = "<password>";
        String vhostName = "/";
        String exchangeName = "testExchange";
        String exchangeType = "direct";
        String queueName = "test";

        ConnectionFactory factory = new ConnectionFactory();

        factory.setUsername(username);

        factory.setPassword(password);

        factory.setAutomaticRecoveryEnabled(true);
        factory.setNetworkRecoveryInterval(5000);

        factory.setVirtualHost(vhostName);

        factory.setConnectionTimeout(30 * 1000);
        factory.setHandshakeTimeout(30 * 1000);
        factory.setShutdownTimeout(0);

        Connection connection = factory.newConnection(addresses);
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(exchangeName, exchangeType, true, false, false, null);
        channel.queueDeclare(queueName, true, false, false, new HashMap<String, Object>());
        channel.queueBind(queueName, exchangeName, "BindingKeyTest");

        for (int i = 0; i < 100; i++  ) {
                String message = "Hello World!"+ i;
                channel.basicPublish(exchangeName, "BindingKeyTest", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
                System.out.println(" [x] Sent '" + message + "'");
        }
        channel.close();
        connection.close();
    }
}

For more examples on Java client, please refer to Java Client API Guide.

For the use of other clients, please refer to RabbitMQ Tutorials.