简体   繁体   English

Docker与Zookeeper,Kafka,Redis和Java Spring Boot组合

[英]Docker Compose with Zookeeper, Kafka, Redis, and Java Spring Boot

I'm having difficulty linking these containers together using Docker Compose, let me preface by saying that I am currently running on a Mac as well. 我在使用Docker Compose将这些容器链接在一起时遇到困难,请允许我说我目前也在Mac上运行。

The application is currently working without the use of Docker Compose, in that if I run these all individually (not with Docker) the application works as intended. 该应用程序当前可以在不使用Docker Compose的情况下运行,因为如果我单独运行所有这些(而不是使用Docker),则该应用程序将按预期运行。

As intended means that the application is reading from Redis as well as pulling data that comes across certain Kafka topics and displaying them on the front-end. 如预期的那样,该应用程序正在从Redis读取数据,并提取涉及某些Kafka主题的数据并将其显示在前端。

On to what I believe are the necessary files. 我认为是必要的文件。

docker-compose.yml

version: '2'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
  kafka:
    image: wurstmeister/kafka:0.10.2.0
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: localhost
      KAFKA_CREATE_TOPICS: "flight-events:1:1,reported-flight-time-events:1:1,pax-flight-events:1:1"
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - zookeeper
  redis:
    image: redis
    ports:
      - "6379"
    restart: always
  kafka-websocket-connector:
    build: ./kafka-websocket-connector
    image: <user>/kafka-websocket-connector
    ports:
      - "8077:8077"
    depends_on:
      - kafka
      - redis
      - zookeeper
    links:
      - "kafka"
      - "redis"

The Dockerfile that is being referenced under kafka-websocket-connector is as follows: kafka-websocket-connector下引用的Dockerfile如下:

Dockerfile

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD target/kafka-websocket-connector-0.0.1-SNAPSHOT.jar app.jar
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

If I attempt to run the command docker-compose up --build I receive the following error: 如果我尝试运行docker-compose up --build命令docker-compose up --build收到以下错误:

ERROR: Service 'kafka-websocket-connector' failed to build: ADD failed:      stat /var/lib/docker/tmp/docker-builder838069739/target/kafka-websocket-connector-0.0.1-SNAPSHOT.jar: no such file or directory

I don't know if this necessarily relates to the connecting these components, but my build.gradle for the Spring application is as follows: 我不知道这是否一定与连接这些组件有关,但是我对Spring应用程序的build.gradle如下:

build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

        classpath('se.transmode.gradle:gradle-docker:1.2')
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

apply plugin: 'docker'

group = '<user>'

jar {
    baseName = 'kafka-websocket-connector'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = 1.8
}

task buildDocker(type: Docker, dependsOn: build) {
  applicationName = jar.baseName
  dockerfile = file('Dockerfile')
  doFirst {
    copy {
      from jar
      into "${stageDir}/target"
    }
  }
}



repositories {
    mavenCentral()
}


ext {
    springCloudVersion = 'Dalston.SR1'
}

dependencies {

    compile('org.springframework.boot:spring-boot-starter-data-redis')
    compile('org.springframework.kafka:spring-kafka')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-websocket')
    compile("org.webjars:webjars-locator")
    compile("org.webjars:sockjs-client:1.0.2")
    compile("org.webjars:stomp-websocket:2.3.3")
    compile("org.webjars:bootstrap:3.3.7")
    compile("org.webjars:jquery:3.1.0")
    compile group: 'org.webjars', name: 'd3js', version: '4.2.1'
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')

}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

bootRepackage {
    executable = true
    enabled = true
}

So to restate the problem, I am having trouble connecting the following components using Docker Compose: Zookeeper, Kafka, Redis, and a Spring Boot application. 因此,要重申该问题,我在使用Docker Compose连接以下组件时遇到了麻烦:Zookeeper,Kafka,Redis和Spring Boot应用程序。

The KAFKA_ADVERTISED_HOST_NAME: localhost environment variable in your kafka service is likely the cause of the problem. 您的kafka服务中的KAFKA_ADVERTISED_HOST_NAME: localhost环境变量可能是导致问题的原因。 It needs to be reachable by other containers. 它需要其他容器可以到达。 Try change it to KAFKA_ADVERTISED_HOST_NAME: kafka . 尝试将其更改为KAFKA_ADVERTISED_HOST_NAME: kafka

All the containers of the services defined in your docker-compose.yml are added to a user-defined network, named after your compose project. 在docker-compose.yml中定义的所有服务容器都将添加到用户定义的网络中,该网络以您的compose项目命名。 The containers in this network are reachable and discoverable by other each, using hostnames that are identical to the container name. 使用与容器名称相同的主机名,此网络中的容器可相互访问和发现。 You can find out more information on Docker Compose networking in their documentation . 您可以在其文档中找到有关Docker Compose网络的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM