无法从在docker外部运行的spring Boot 应用程序连接到在docker内部运行的Kafka集群[重复]

1wnzp6jl  于 2023-11-16  发布在  Apache
关注(0)|答案(1)|浏览(205)

此问题在此处已有答案

Connect to Kafka running in Docker(6个答案)
24天前关闭
这是我的docker-compose文件-我无法从在我的windows本地运行的springboot应用程序连接到在docker中运行的Kafka。我已经共享了必要的文件。我已经通过在docker容器中执行exec创建了topic,所有这些都可以工作,但我需要从我的本地应用程序访问它。

version: '3'
services:
  zookeeper-1:
    image: confluentinc/cp-zookeeper:latest
    container_name: zookeeper-1
    environment:
      ZOOKEEPER_SERVER_ID: 1
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - "22181:2181"

  zookeeper-2:
    image: confluentinc/cp-zookeeper:latest
    container_name: zookeeper-2
    environment:
      ZOOKEEPER_SERVER_ID: 2
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - "32181:2181"

  broker-1:
    image: confluentinc/cp-kafka:latest
    container_name: broker-1
    ports:
      - "19092:9092"
    depends_on:
      - zookeeper-1
      - zookeeper-2
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper-1:2181,zookeeper-2:2181'
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker-1:9092,PLAINTEXT_HOST://localhost:19092
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT

  broker-2:
    image: confluentinc/cp-kafka:latest
    container_name: broker-2
    ports:
      - "19093:9092"
    depends_on:
      - zookeeper-1
      - zookeeper-2
    environment:
      KAFKA_BROKER_ID: 2
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper-1:2181,zookeeper-2:2181'
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker-2:9093,PLAINTEXT_HOST://localhost:19093
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT

  broker-3:
    image: confluentinc/cp-kafka:latest
    container_name: broker-3
    ports:
      - "19094:9092"
    depends_on:
      - zookeeper-1
      - zookeeper-2
    environment:
      KAFKA_BROKER_ID: 3
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper-1:2181,zookeeper-2:2181'
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker-3:9094,PLAINTEXT_HOST://localhost:19094
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT

字符串
我已经添加了所有的问题。我试着搜索了很多,但我找不到问题。
Sping Boot 中的应用程序属性文件-

spring.kafka.bootstrap-servers=localhost:19092,localhost:19093,localhost:19094
spring.kafka.consumer.group-id=profile
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer


kafkaconfig类-

@Configuration
@Getter
public class KakfaConfig {
    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Value("${spring.kafka.consumer.group-id}")
    private String consumerGroupId;

    @Value("${spring.kafka.consumer.auto-offset-reset}")
    private String autoOffsetReset;

    @Value("${spring.kafka.consumer.key-deserializer}")
    private String keyDeserializer;

    @Value("${spring.kafka.consumer.value-deserializer}")
    private String valueDeserializer;

    @Value("${spring.kafka.producer.key-serializer}")
    private String keySerializer;

        @Value("${spring.kafka.producer.value-serializer}")
        private String valueSerializer;

    @Bean
    public Map<String, Object> getKafkaTemplate() {
        Map<String, Object> props = new HashMap<>();
        props.put("bootstrap.servers", bootstrapServers);
        props.put("group.id", consumerGroupId);
        props.put("auto.offset.reset", autoOffsetReset);
        props.put("key.deserializer", keyDeserializer);
        props.put("value.deserializer", valueDeserializer);
        props.put("key.serializer", keySerializer);
        props.put("value.serializer", valueSerializer);
        return props;
    }

    @Bean(name = "userProfileKafkaTemplate")
    public KafkaTemplate<String, String> userProfileKafkaTemplate() {
        Map<String, Object> props = getKafkaTemplate();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(props));
    }
}


错误消息-

2023-10-20T19:38:49.111+05:30  INFO 19236 --- [nio-8080-exec-1] c.c.p.producer.UserProfileKakfaProducer  : Sending message to user profile topic: user-profile-topic, Event type USER_PROFILE_CREATED
2023-10-20T19:38:49.198+05:30  INFO 19236 --- [nio-8080-exec-1] o.a.k.clients.producer.ProducerConfig    : ProducerConfig values: 
    acks = -1
    auto.include.jmx.reporter = true
    batch.size = 16384
    bootstrap.servers = [localhost:19092, localhost:19093, localhost:19094]
    buffer.memory = 33554432
    client.dns.lookup = use_all_dns_ips
    client.id = producer-1
    compression.type = none
    connections.max.idle.ms = 540000
    delivery.timeout.ms = 120000
    enable.idempotence = true
    interceptor.classes = []
    key.serializer = class org.apache.kafka.common.serialization.StringSerializer
    linger.ms = 0
    max.block.ms = 60000
    max.in.flight.requests.per.connection = 5
    max.request.size = 1048576
    metadata.max.age.ms = 300000
    metadata.max.idle.ms = 300000
    metric.reporters = []
    metrics.num.samples = 2
    metrics.recording.level = INFO
    metrics.sample.window.ms = 30000
    partitioner.adaptive.partitioning.enable = true
    partitioner.availability.timeout.ms = 0
    partitioner.class = null
    partitioner.ignore.keys = false
    receive.buffer.bytes = 32768
    reconnect.backoff.max.ms = 1000
    reconnect.backoff.ms = 50
    request.timeout.ms = 30000
    retries = 2147483647
    retry.backoff.ms = 100
    sasl.client.callback.handler.class = null
    sasl.jaas.config = null
    sasl.kerberos.kinit.cmd = /usr/bin/kinit
    sasl.kerberos.min.time.before.relogin = 60000
    sasl.kerberos.service.name = null
    sasl.kerberos.ticket.renew.jitter = 0.05
    sasl.kerberos.ticket.renew.window.factor = 0.8
    sasl.login.callback.handler.class = null
    sasl.login.class = null
    sasl.login.connect.timeout.ms = null
    sasl.login.read.timeout.ms = null
    sasl.login.refresh.buffer.seconds = 300
    sasl.login.refresh.min.period.seconds = 60
    sasl.login.refresh.window.factor = 0.8
    sasl.login.refresh.window.jitter = 0.05
    sasl.login.retry.backoff.max.ms = 10000
    sasl.login.retry.backoff.ms = 100
    sasl.mechanism = GSSAPI
    sasl.oauthbearer.clock.skew.seconds = 30
    sasl.oauthbearer.expected.audience = null
    sasl.oauthbearer.expected.issuer = null
    sasl.oauthbearer.jwks.endpoint.refresh.ms = 3600000
    sasl.oauthbearer.jwks.endpoint.retry.backoff.max.ms = 10000
    sasl.oauthbearer.jwks.endpoint.retry.backoff.ms = 100
    sasl.oauthbearer.jwks.endpoint.url = null
    sasl.oauthbearer.scope.claim.name = scope
    sasl.oauthbearer.sub.claim.name = sub
    sasl.oauthbearer.token.endpoint.url = null
    security.protocol = PLAINTEXT
    security.providers = null
    send.buffer.bytes = 131072
    socket.connection.setup.timeout.max.ms = 30000
    socket.connection.setup.timeout.ms = 10000
    ssl.cipher.suites = null
    ssl.enabled.protocols = [TLSv1.2, TLSv1.3]
    ssl.endpoint.identification.algorithm = https
    ssl.engine.factory.class = null
    ssl.key.password = null
    ssl.keymanager.algorithm = SunX509
    ssl.keystore.certificate.chain = null
    ssl.keystore.key = null
    ssl.keystore.location = null
    ssl.keystore.password = null
    ssl.keystore.type = JKS
    ssl.protocol = TLSv1.3
    ssl.provider = null
    ssl.secure.random.implementation = null
    ssl.trustmanager.algorithm = PKIX
    ssl.truststore.certificates = null
    ssl.truststore.location = null
    ssl.truststore.password = null
    ssl.truststore.type = JKS
    transaction.timeout.ms = 60000
    transactional.id = null
    value.serializer = class org.apache.kafka.common.serialization.StringSerializer

2023-10-20T19:38:49.399+05:30  INFO 19236 --- [nio-8080-exec-1] o.a.k.clients.producer.KafkaProducer     : [Producer clientId=producer-1] Instantiated an idempotent producer.
2023-10-20T19:38:49.524+05:30  WARN 19236 --- [nio-8080-exec-1] o.a.k.clients.producer.ProducerConfig    : These configurations '[key.deserializer, value.deserializer, group.id, auto.offset.reset]' were supplied but are not used yet.
2023-10-20T19:38:49.526+05:30  INFO 19236 --- [nio-8080-exec-1] o.a.kafka.common.utils.AppInfoParser     : Kafka version: 3.4.1
2023-10-20T19:38:49.527+05:30  INFO 19236 --- [nio-8080-exec-1] o.a.kafka.common.utils.AppInfoParser     : Kafka commitId: 8a516edc2755df89
2023-10-20T19:38:49.527+05:30  INFO 19236 --- [nio-8080-exec-1] o.a.kafka.common.utils.AppInfoParser     : Kafka startTimeMs: 1697810929524
2023-10-20T19:38:50.448+05:30  INFO 19236 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient   : [Producer clientId=producer-1] Node -3 disconnected.
2023-10-20T19:38:50.455+05:30  WARN 19236 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient   : [Producer clientId=producer-1] Connection to node -3 (localhost/127.0.0.1:19094) could not be established. Broker may not be available.
2023-10-20T19:38:50.456+05:30  WARN 19236 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient   : [Producer clientId=producer-1] Bootstrap broker localhost:19094 (id: -3 rack: null) disconnected
2023-10-20T19:38:50.851+05:30  INFO 19236 --- [ad | producer-1] org.apache.kafka.clients.Metadata        : [Producer clientId=producer-1] Resetting the last seen epoch of partition user-profile-topic-0 to 5 since the associated topicId changed from null to 96WtPNbMTqWDKfQuy2GZxg
2023-10-20T19:38:50.853+05:30  INFO 19236 --- [ad | producer-1] org.apache.kafka.clients.Metadata        : [Producer clientId=producer-1] Resetting the last seen epoch of partition user-profile-topic-5 to 4 since the associated topicId changed from null to 96WtPNbMTqWDKfQuy2GZxg
2023-10-20T19:38:50.854+05:30  INFO 19236 --- [ad | producer-1] org.apache.kafka.clients.Metadata        : [Producer clientId=producer-1] Resetting the last seen epoch of partition user-profile-topic-1 to 4 since the associated topicId changed from null to 96WtPNbMTqWDKfQuy2GZxg
2023-10-20T19:38:50.854+05:30  INFO 19236 --- [ad | producer-1] org.apache.kafka.clients.Metadata        : [Producer clientId=producer-1] Resetting the last seen epoch of partition user-profile-topic-4 to 4 since the associated topicId changed from null to 96WtPNbMTqWDKfQuy2GZxg
2023-10-20T19:38:50.854+05:30  INFO 19236 --- [ad | producer-1] org.apache.kafka.clients.Metadata        : [Producer clientId=producer-1] Resetting the last seen epoch of partition user-profile-topic-2 to 4 since the associated topicId changed from null to 96WtPNbMTqWDKfQuy2GZxg
2023-10-20T19:38:50.854+05:30  INFO 19236 --- [ad | producer-1] org.apache.kafka.clients.Metadata        : [Producer clientId=producer-1] Resetting the last seen epoch of partition user-profile-topic-3 to 5 since the associated topicId changed from null to 96WtPNbMTqWDKfQuy2GZxg
2023-10-20T19:38:50.861+05:30  INFO 19236 --- [ad | producer-1] org.apache.kafka.clients.Metadata        : [Producer clientId=producer-1] Cluster ID: LR9OWVzUToybvB004q6T2g
2023-10-20T19:38:51.018+05:30  INFO 19236 --- [ad | producer-1] o.a.k.c.p.internals.TransactionManager   : [Producer clientId=producer-1] ProducerId set to 1000 with epoch 0
2023-10-20T19:38:53.713+05:30  WARN 19236 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient   : [Producer clientId=producer-1] Error connecting to node broker-1:9092 (id: 1 rack: null)

java.net.UnknownHostException: No such host is known (broker-1)
    at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:na]
    at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:933) ~[na:na]
    at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1543) ~[na:na]
    at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:852) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1532) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1384) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1305) ~[na:na]
    at org.apache.kafka.clients.DefaultHostResolver.resolve(DefaultHostResolver.java:27) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.ClientUtils.resolve(ClientUtils.java:110) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.currentAddress(ClusterConnectionStates.java:510) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.access$200(ClusterConnectionStates.java:467) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.ClusterConnectionStates.currentAddress(ClusterConnectionStates.java:173) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.NetworkClient.initiateConnect(NetworkClient.java:990) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.NetworkClient.ready(NetworkClient.java:301) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.producer.internals.Sender.sendProducerData(Sender.java:354) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.producer.internals.Sender.runOnce(Sender.java:327) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:243) ~[kafka-clients-3.4.1.jar:na]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

2023-10-20T19:38:56.398+05:30  WARN 19236 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient   : [Producer clientId=producer-1] Error connecting to node broker-2:9093 (id: 2 rack: null)

java.net.UnknownHostException: No such host is known (broker-2)
    at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:na]
    at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:933) ~[na:na]
    at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1543) ~[na:na]
    at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:852) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1532) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1384) ~[na:na]
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1305) ~[na:na]
    at org.apache.kafka.clients.DefaultHostResolver.resolve(DefaultHostResolver.java:27) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.ClientUtils.resolve(ClientUtils.java:110) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.currentAddress(ClusterConnectionStates.java:510) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.access$200(ClusterConnectionStates.java:467) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.ClusterConnectionStates.currentAddress(ClusterConnectionStates.java:173) ~[kafka-clients-3.4.1.jar:na]
    at org.apache.kafka.clients.NetworkClient.initiateConnect(Netw


请帮帮我

cxfofazt

cxfofazt1#

你应该尝试在docker-compose中定义网络。

相关问题