如何 docker-compose spring-boot with kafka?
how to docker-compose spring-boot with kafka?
执行工作正常,但我想我遗漏了一些东西,因为当我使用 REST API 时,它会显示:
(/127.0.0.1:9092) could not be established. Broker may not be available
您可能会在下面看到这个。顺便说一下,我是 Docker 和 Kafka 的新手。
我无法向 /GET/POST/PUT/DELETE 发送或获取数据,因为我使用 Docker。
我创建此设置时的参考资料:https://github.com/codegard/kafka-docker/blob/master/docker-compose.yml
//docker-compose.yaml
version: '3'
services:
#----------------------------------------------------------------
productmicroservice:
image: productmicroservice:latest
container_name: productmicroservice
depends_on:
- product-mysqldb
- kafka
restart: always
build:
context: ./
dockerfile: Dockerfile
ports:
- "9001:8091"
environment:
- MYSQL_HOST=product-mysqldb
- MYSQL_USER=oot
- MYSQL_PASSWORD=root
- MYSQL_PORT=3306
- "SPRING_PROFILES_ACTIVE=${ACTIVE_PROFILE}"
#----------------------------------------------------------------
product-mysqldb:
image: mysql:8.0.28
restart: unless-stopped
container_name: product-mysqldb
ports:
- "3307:3306"
cap_add:
- SYS_NICE
environment:
MYSQL_DATABASE: dbpoc
MYSQL_ROOT_PASSWORD: root
#----------------------------------------------------------------
zookeeper:
image: elevy/zookeeper:latest
container_name: zookeeper
ports:
- "2181:2181"
#----------------------------------------------------------------
kafka:
image: wurstmeister/kafka:2.11-2.0.0
container_name: kafka
restart: on-failure
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
depends_on:
- zookeeper
//appication.yaml
spring.kafka.bootstrap-servers=127.0.0.1:9092
product.kafkaServer= ${spring.kafka.bootstrap-servers}
spring.kafka.properties.security.protocol=PLAINTEXT
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
topic.name=producttopic
spring.jpa.properties.hibernate.check_nullability=true
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/dbpoc
username: root
password: root
jpa:
hibernate:
naming:
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
hibernate.ddl-auto: update
generate-ddl: false
show-sql: false
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
mvc:
throw-exception-if-no-handler-found: true
web:
resources:
add-mappings: false
sql:
init:
mode: always
continue-on-error: true
server:
port: 8091
// .env
ACTIVE_PROFILE=dev
//Dockerfile
FROM openjdk:8-alpine
ADD target/*.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
//topic that i created
./kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic producttopic
//trying to send data manualy in kafka
//working
//producer
bash-4.4# ./kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic producttopic
>hellow
>
//comsumer
bash-4.4# ./kafka-console-consumer.sh --topic producttopic --from-beginning --bootstrap-server 127.0.0.1:9092
hellow
//sending data to from my rest api
//this is wrong but i dont know what is the reason
productmicroservice | 2022-05-04 13:08:28.283 WARN 1 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Connection to node -1 (/127.0.0.1:9092) could not be established. Broker may not be available.
productmicroservice | 2022-05-04 13:08:28.283 WARN 1 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Bootstrap broker 127.0.0.1:9092 (id: -1 rack: null) disconnected
productmicroservice | 2022-05-04 13:08:29.343 WARN 1 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Connection to node -1 (/127.0.0.1:9092) could not be established. Broker may not be available.
//fix reference below comment
//change made
//application.yaml
spring.kafka.topic.name=producttopic
topic.name=${spring.kafka.topic.name}
spring.kafka.bootstrap-servers=kafka:9092
product.kafkaServer= ${spring.kafka.bootstrap-servers}
//docker-compose.yaml
//added to kafka environment
KAFKA_CREATE_TOPICS: "producttopic:1:1"
KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
KAFKA_LISTENERS: INSIDE://kafka:9092,OUTSIDE://0.0.0.0:9093
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka:9092,OUTSIDE://localhost:9093
//manual get data in topic by consumer
./kafka-console-consumer.sh --topic producttopic --from-beginning --bootstrap-server localhost:9093
//code changes in spring
//for topic
@Value("${topic.name}")
//for bootstrapserver
@Value("${product.kafkaServer}")
//must note
wurstmeister/kafka:2.11-2.0.0
I use "2.11-2.0.0" since its compatible with jdk 8 where latest give me error and my project required jdk 8
您正在使用 127.0.0.1:9092
作为来自 Java 容器的 Kafka 容器端点。从容器中,localhost
以容器本身为目标,这是行不通的。
Docker Compose 将设置一个默认值 network,其中您的服务可以通过它们的名称访问。
因此,我认为您应该将 application.yaml 更改为:
spring.kafka.bootstrap-servers=kafka:9092
# ...
执行工作正常,但我想我遗漏了一些东西,因为当我使用 REST API 时,它会显示:
(/127.0.0.1:9092) could not be established. Broker may not be available
您可能会在下面看到这个。顺便说一下,我是 Docker 和 Kafka 的新手。
我无法向 /GET/POST/PUT/DELETE 发送或获取数据,因为我使用 Docker。
我创建此设置时的参考资料:https://github.com/codegard/kafka-docker/blob/master/docker-compose.yml
//docker-compose.yaml
version: '3'
services:
#----------------------------------------------------------------
productmicroservice:
image: productmicroservice:latest
container_name: productmicroservice
depends_on:
- product-mysqldb
- kafka
restart: always
build:
context: ./
dockerfile: Dockerfile
ports:
- "9001:8091"
environment:
- MYSQL_HOST=product-mysqldb
- MYSQL_USER=oot
- MYSQL_PASSWORD=root
- MYSQL_PORT=3306
- "SPRING_PROFILES_ACTIVE=${ACTIVE_PROFILE}"
#----------------------------------------------------------------
product-mysqldb:
image: mysql:8.0.28
restart: unless-stopped
container_name: product-mysqldb
ports:
- "3307:3306"
cap_add:
- SYS_NICE
environment:
MYSQL_DATABASE: dbpoc
MYSQL_ROOT_PASSWORD: root
#----------------------------------------------------------------
zookeeper:
image: elevy/zookeeper:latest
container_name: zookeeper
ports:
- "2181:2181"
#----------------------------------------------------------------
kafka:
image: wurstmeister/kafka:2.11-2.0.0
container_name: kafka
restart: on-failure
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
depends_on:
- zookeeper
//appication.yaml
spring.kafka.bootstrap-servers=127.0.0.1:9092
product.kafkaServer= ${spring.kafka.bootstrap-servers}
spring.kafka.properties.security.protocol=PLAINTEXT
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
topic.name=producttopic
spring.jpa.properties.hibernate.check_nullability=true
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/dbpoc
username: root
password: root
jpa:
hibernate:
naming:
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
hibernate.ddl-auto: update
generate-ddl: false
show-sql: false
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
mvc:
throw-exception-if-no-handler-found: true
web:
resources:
add-mappings: false
sql:
init:
mode: always
continue-on-error: true
server:
port: 8091
// .env
ACTIVE_PROFILE=dev
//Dockerfile
FROM openjdk:8-alpine
ADD target/*.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
//topic that i created
./kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic producttopic
//trying to send data manualy in kafka
//working
//producer
bash-4.4# ./kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic producttopic
>hellow
>
//comsumer
bash-4.4# ./kafka-console-consumer.sh --topic producttopic --from-beginning --bootstrap-server 127.0.0.1:9092
hellow
//sending data to from my rest api
//this is wrong but i dont know what is the reason
productmicroservice | 2022-05-04 13:08:28.283 WARN 1 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Connection to node -1 (/127.0.0.1:9092) could not be established. Broker may not be available.
productmicroservice | 2022-05-04 13:08:28.283 WARN 1 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Bootstrap broker 127.0.0.1:9092 (id: -1 rack: null) disconnected
productmicroservice | 2022-05-04 13:08:29.343 WARN 1 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Connection to node -1 (/127.0.0.1:9092) could not be established. Broker may not be available.
//fix reference below comment
//change made
//application.yaml
spring.kafka.topic.name=producttopic
topic.name=${spring.kafka.topic.name}
spring.kafka.bootstrap-servers=kafka:9092
product.kafkaServer= ${spring.kafka.bootstrap-servers}
//docker-compose.yaml
//added to kafka environment
KAFKA_CREATE_TOPICS: "producttopic:1:1"
KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
KAFKA_LISTENERS: INSIDE://kafka:9092,OUTSIDE://0.0.0.0:9093
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka:9092,OUTSIDE://localhost:9093
//manual get data in topic by consumer
./kafka-console-consumer.sh --topic producttopic --from-beginning --bootstrap-server localhost:9093
//code changes in spring
//for topic
@Value("${topic.name}")
//for bootstrapserver
@Value("${product.kafkaServer}")
//must note
wurstmeister/kafka:2.11-2.0.0
I use "2.11-2.0.0" since its compatible with jdk 8 where latest give me error and my project required jdk 8
您正在使用 127.0.0.1:9092
作为来自 Java 容器的 Kafka 容器端点。从容器中,localhost
以容器本身为目标,这是行不通的。
Docker Compose 将设置一个默认值 network,其中您的服务可以通过它们的名称访问。 因此,我认为您应该将 application.yaml 更改为:
spring.kafka.bootstrap-servers=kafka:9092
# ...