0

i have micro service project generated by Jhipster when run in local host i dont have any problem but when deploy my code in server by docker images i have this error with kafka says:

 [Consumer clientId=consumer-2, groupId=groupId] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.

i am search about this error but without any solution

application.yml

 kafka:
 // bootstrap-servers: 206.189.178.228:9092
  consumer:
    bootstrap-servers: 206.189.178.228:9092 //i am try kafka:9092 
    key.deserializer: org.apache.kafka.common.serialization.StringDeserializer
    value.deserializer: org.apache.kafka.common.serialization.StringDeserializer
    group.id: store-service
    auto.offset.reset: earliest
  producer:
    bootstrap-servers: 206.189.178.228:9092  //i am try kafka:9092 
    key.serializer: org.apache.kafka.common.serialization.StringSerializer
    value.serializer: org.apache.kafka.common.serialization.StringSerializer

kafka config:

@Configuration
@ConfigurationProperties(prefix = "kafka")
public class KafkaProperties {

    private String bootStrapServers = "206.189.178.228:9092";

    private Map<String, String> consumer = new HashMap<>();

    private Map<String, String> producer = new HashMap<>();

    public String getBootStrapServers() {
        return bootStrapServers;
    }

    public void setBootStrapServers(String bootStrapServers) {
        this.bootStrapServers = bootStrapServers;
    }

    public Map<String, Object> getConsumerProps() {
        Map<String, Object> properties = new HashMap<>(this.consumer);
        if (!properties.containsKey("bootstrap.servers")) {
            properties.put("bootstrap.servers", this.bootStrapServers);
        }
        return properties;
    }

    public void setConsumer(Map<String, String> consumer) {
        this.consumer = consumer;
    }

    public Map<String, Object> getProducerProps() {
        Map<String, Object> properties = new HashMap<>(this.producer);
        if (!properties.containsKey("bootstrap.servers")) {
            properties.put("bootstrap.servers", this.bootStrapServers);
        }
        return properties;
    }

    public void setProducer(Map<String, String> producer) {
        this.producer = producer;
    }
}

and security config:

    @Override
public void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http
        .csrf()
        .disable()
        .exceptionHandling()
            .authenticationEntryPoint(problemSupport)
            .accessDeniedHandler(problemSupport)
    .and()
        .headers()
        .contentSecurityPolicy("default-src 'self'; frame-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://storage.googleapis.com; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:")
    .and()
        .referrerPolicy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN)
    .and()
        .featurePolicy("geolocation 'none'; midi 'none'; sync-xhr 'none'; microphone 'none'; camera 'none'; magnetometer 'none'; gyroscope 'none'; speaker 'none'; fullscreen 'self'; payment 'none'")
    .and()
        .frameOptions()
        .deny()
    .and()
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and()
        .authorizeRequests()
        .antMatchers("/api/auth-info").permitAll()
        .antMatchers("/api/store-service-kafka/publish").permitAll()
        .antMatchers("/api/**").authenticated()

my docker-compose kafka.yml :

    services:
  zookeeper:
    image: confluentinc/cp-zookeeper:5.5.0
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    networks:
      - food_default
  kafka:
    image: confluentinc/cp-kafka:5.5.0
    ports:
      - 9092:9092
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ADVERTISED_HOST_NAME: kafka
      LISTENERS: PLAINTEXT://206.189.178.228:9092
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    networks:
      - food_default

networks:
  food_default:
    external: true

this is documentation from jhipster : kafka with jhipster

Abdalrhman Alkraien
  • 645
  • 1
  • 7
  • 22
  • Most likely your docker image is not properly configuring the broker advertised hosts used during the bootstrap process. Google to find out how to configure Kafka with Docker. – Gary Russell Jun 24 '21 at 15:10
  • 1) The error starts in your Docker Kafka service, which you've not shown, and if it is based on your last question, then you've set `KAFKA_ADVERTISED_HOST_NAME=127.0.0.1`, which _matches your error_ 2) What do you mean "i am try kafka:9092"? That is used nowhere in the code. Also, unless your Spring app is **also** in a container, using a Docker service name isn't going to work. And, you use `#` to comment properties file, not `//` – OneCricketeer Jun 24 '21 at 17:22
  • @OneCricketeer i am edit question and add kafka.yml (docker compose file ) – Abdalrhman Alkraien Jun 24 '21 at 18:45
  • Please refer to linked duplicate post. 1) remove `KAFKA_ADVERTISED_HOST_NAME` 2) `KAFKA_LISTENERS` should not be a specific IP; set it to `PLAINTEXT://0.0.0.0:9092` 3) Your advertised listeners needs to be an _externally resolvable_ address; in other words `kafka:9092` is only resolvable _within_ `food_default` network, and so this is where your code needs to run to be able to communicate with `kafka:9092`. Otherwise, you need to "advertise" a different address. And I suggest testing your setup with Kafka CLI commands before writing any code of your own – OneCricketeer Jun 24 '21 at 22:18

0 Answers0