简体   繁体   中英

Docker compose for Zookeeper and Spring Cloud - my service does not connect to zoo

I'm trying to run zookeeper and spring cloud service inside a container using docker-compose. When I run the service The jar file and start the zoo everything is fine. But, when I'm trying to run in a container The service cannot connect to zoo -

Here is the error when docker-compose up:

controller_1  | 2020-07-21 09:45:12.715  WARN 1 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
controller_1  |
controller_1  | java.net.ConnectException: Connection refused
controller_1  |         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_111-internal]
controller_1  |         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_111-internal]
controller_1  |         at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361) ~[zookeeper-3.4.8.jar!/:3.4.8--1]
controller_1  |         at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1141) ~[zookeeper-3.4.8.jar!/:3.4.8--1]
controller_1  |

Here is my yaml.file

version: '3'
services:
  zookeeper:
    image: 'zookeeper:3.4'
    ports:
      - "2181:2181"
    restart: always
    container_name: zookeeper
    environment:
      ZOOKEEPER_SASL_ENABLED: 'FALSE'
      
  controller:
    build: distributed-search-engine-1/
    ports:
      - "8083:8083"
    links:
      - zookeeper
    depends_on: 
      - zookeeper

Docker file of the service:

FROM java:8-jdk-alpine
COPY ./build/libs/distributed-search-engine-1-0.0.1-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app
EXPOSE 8083
ENTRYPOINT ["java", "-jar", "distributed-search-engine-1-0.0.1-SNAPSHOT.jar"]

My service does not define any Zookeeper port, so I guess 2181 should be fine. anyway, here is my build file:

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

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.search'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/milestone" }
}


ext {
    springCloudVersion = 'Finchley.M8'
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-integration')
    compile group: 'org.springframework.integration', name: 'spring-integration-zookeeper'
    compile('org.springframework.cloud:spring-cloud-starter-zookeeper-config')

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

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



jar {
  manifest { 
    attributes "Main-Class": "com.search.server.Application"
  }  

  from {
    configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
  }
}




Thanks!!!!

This is docker issue, not java. At first glance, it look like you try to access zookeeper on localhost:2181 while on your virtual network, the container respond to DNS name of "zookeeper" (the service name in your docker-compose file), not localhost. Did you try to use zookeeper:2181 instead?

Once that's, fixed, ideally you would actually use an ENV variable to your service specifying the zookeeper host/port so it can be configurable and set it in your docker compose file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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