pulsar的producer获取broker地址

ProducerImpl

public ProducerImpl(PulsarClientImpl client, String topic, String producerName, ProducerConfiguration conf,
            CompletableFuture producerCreatedFuture, int partitionIndex) {
        super(client, topic, conf, producerCreatedFuture);
        this.producerId = client.newProducerId();
        this.producerName = producerName;
        this.partitionIndex = partitionIndex;
        this.pendingMessages = Queues.newArrayBlockingQueue(conf.getMaxPendingMessages());
        this.pendingCallbacks = Queues.newArrayBlockingQueue(conf.getMaxPendingMessages());
        this.semaphore = new Semaphore(conf.getMaxPendingMessages(), true);
        this.compressor = CompressionCodecProvider
                .getCompressionCodec(convertCompressionType(conf.getCompressionType()));

        if (conf.getSendTimeoutMs() > 0) {
            sendTimeout = client.timer().newTimeout(this, conf.getSendTimeoutMs(), TimeUnit.MILLISECONDS);
        }

        this.createProducerTimeout = System.currentTimeMillis() + client.getConfiguration().getOperationTimeoutMs();
        if (conf.getBatchingEnabled()) {
            this.maxNumMessagesInBatch = conf.getBatchingMaxMessages();
            this.batchMessageContainer = new BatchMessageContainer(maxNumMessagesInBatch,
                    convertCompressionType(conf.getCompressionType()), topic, producerName);
        } else {
            this.maxNumMessagesInBatch = 1;
            this.batchMessageContainer = null;
        }
        if (client.getConfiguration().getStatsIntervalSeconds() > 0) {
            stats = new ProducerStats(client, conf, this);
        } else {
            stats = ProducerStats.PRODUCER_STATS_DISABLED;
        }
        grabCnx();
    }

grabCnx()

protected void grabCnx() {
        if (clientCnx.get() != null) {
            log.warn("[{}] [{}] Client cnx already set, ignoring reconnection request", topic, getHandlerName());
            return;
        }

        if (!isValidStateForReconnection()) {
            // Ignore connection closed when we are shutting down
            log.info("[{}] [{}] Ignoring reconnection request (state: {})", topic, getHandlerName(), state);
            return;
        }

        try {
            client.getConnection(topic) //
                    .thenAccept(this::connectionOpened) //
                    .exceptionally(this::handleConnectionError);
        } catch (Throwable t) {
            log.warn("[{}] [{}] Exception thrown while getting connection: ", topic, getHandlerName(), t);
            reconnectLater(t);
        }
    }

client.getConnection(topic)

protected CompletableFuture getConnection(final String topic) {
        DestinationName destinationName = DestinationName.get(topic);

        return lookup.getBroker(destinationName).thenCompose((brokerAddress) -> cnxPool.getConnection(brokerAddress));
    }

lookup.getBroker(destinationName)

public CompletableFuture getBroker(DestinationName destination) {
        return httpClient.get(BasePath + destination.getLookupName(), LookupData.class).thenCompose(lookupData -> {
            // Convert LookupData into as SocketAddress, handling exceptions
            try {
                URI uri;
                if (useTls) {
                    uri = new URI(lookupData.getBrokerUrlTls());
                } else {
                    String serviceUrl = lookupData.getBrokerUrl();
                    if (serviceUrl == null) {
                        serviceUrl = lookupData.getNativeUrl();
                    }
                    uri = new URI(serviceUrl);
                }
                return CompletableFuture.completedFuture(new InetSocketAddress(uri.getHost(), uri.getPort()));
            } catch (Exception e) {
                // Failed to parse url
                return FutureUtil.failedFuture(e);
            }
        });
    }

请求

http://192.168.99.100:8080/lookup/v2/destination/persistent/sample/standalone/ns1/demo-topic

返回

{
brokerUrl: "pulsar://1c46a4e9d924:6650",
brokerUrlTls: "",
httpUrl: "http://1c46a4e9d924:8080",
nativeUrl: "pulsar://1c46a4e9d924:6650",
brokerUrlSsl: ""
}

你可能感兴趣的:(pulsar)