alibaba/canal · error · CanalException

failed to parse host

Error message

failed to parse host

What it means

Thrown by CanalRabbitMQProducer.init when the host string starts with 'amqp' and ConnectionFactory.setUri(servers) raises URISyntaxException, NoSuchAlgorithmException, or KeyManagementException. The URI is malformed, uses an unsupported scheme, or specifies a TLS/algorithm the JVM does not provide.

Source

Thrown at connector/rabbitmq-connector/src/main/java/com/alibaba/otter/canal/connector/rabbitmq/producer/CanalRabbitMQProducer.java:62

    private static final Logger logger = LoggerFactory.getLogger(CanalRabbitMQProducer.class);

    private Connection          connect;
    private Channel             channel;

    @Override
    public void init(Properties properties) {
        RabbitMQProducerConfig rabbitMQProperties = new RabbitMQProducerConfig();
        this.mqProperties = rabbitMQProperties;
        super.init(properties);
        loadRabbitMQProperties(properties);

        ConnectionFactory factory = new ConnectionFactory();
        String servers = rabbitMQProperties.getHost();
        if (servers.startsWith("amqp")) {
            try {
                factory.setUri(servers);
            } catch (URISyntaxException | NoSuchAlgorithmException | KeyManagementException ex) {
                throw new CanalException("failed to parse host", ex);
            }
        } else if (servers.contains(":")) {
            String[] serverHostAndPort = AddressUtils.splitIPAndPort(servers);
            factory.setHost(serverHostAndPort[0]);
            factory.setPort(Integer.parseInt(serverHostAndPort[1]));
        } else {
            factory.setHost(servers);
        }

        if (mqProperties.getAliyunAccessKey().length() > 0 && mqProperties.getAliyunSecretKey().length() > 0
            && mqProperties.getAliyunUid() > 0) {
            factory.setCredentialsProvider(new AliyunCredentialsProvider(mqProperties.getAliyunAccessKey(),
                mqProperties.getAliyunSecretKey(),
                mqProperties.getAliyunUid()));
        } else {
            factory.setUsername(rabbitMQProperties.getUsername());
            factory.setPassword(rabbitMQProperties.getPassword());
        }

View on GitHub (pinned to 87be50e876)

Solutions

  1. Validate the URI format: scheme amqp/amqps, host, optional port, optional vhost, optional userinfo.
  2. Percent-encode any special characters in the vhost or credentials (e.g. vhost '/test v' must encode the space).
  3. Trim whitespace from the rabbitmq.host property before passing it.
  4. If you do not need a full URI, use host[:port] form instead so the code takes the AddressUtils.splitIPAndPort branch.

Example fix

// before
String servers = rabbitMQProperties.getHost();
if (servers.startsWith("amqp")) {
    factory.setUri(servers);
}

// after — validate and trim before setUri
String servers = rabbitMQProperties.getHost().trim();
if (servers.startsWith("amqp")) {
    try { factory.setUri(servers); }
    catch (Exception ex) {
        throw new CanalException("failed to parse host '" + servers + "': " + ex.getMessage(), ex);
    }
}
Defensive patterns

Strategy: validation

Validate before calling

String servers = rabbitMQProperties.getHost().trim();
if (servers.startsWith("amqp")) {
    try { new java.net.URI(servers); }
    catch (java.net.URISyntaxException e) {
        throw new IllegalArgumentException("invalid amqp URI: " + servers, e);
    }
}

Type guard

static boolean isAmqpUri(String s) {
    if (s == null) return false;
    String t = s.trim();
    return t.startsWith("amqp://") || t.startsWith("amqps://");
}

Prevention

When it happens

Trigger: rabbitMQProperties.getHost() starts with 'amqp' and factory.setUri(servers) fails. Causes: missing/amqp-uri-syntax-violating characters (spaces, bad encoding); scheme not amqp/amqps; bad credentials embedded in the URI; amqps with a TLS algorithm unavailable in the JVM.

Common situations: Hand-typed amqp URI with a typo; URI containing special characters not percent-encoded; amqps URI on a JVM without the required TLS provider; missing/amqp-uri-syntax-violating credentials or vhost segment; copy-paste that included surrounding whitespace.

Understand the failure class

Related errors


AI-assisted analysis of alibaba/canal@87be50e876 (2026-08-14). Data as JSON: /api/errors/ea283993b0de6dce. Report an issue: GitHub.