Docker为Zookeeper和Spring Cloud编写-我的服务没有连接到zoo



我正在尝试使用docker compose在容器内运行zookeeper和spring云服务。当我运行服务jar文件并启动zoo时,一切都很好。但是,当我试图在容器中运行时,该服务无法连接到动物园-

以下是docker组合时的错误:

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  |

这是我的山文件

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文件:

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"]

我的服务没有定义任何Zookeeper端口,所以我想2181应该可以。无论如何,这是我的构建文件:

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) }
}
}


谢谢!!!!

这是docker问题,而不是java问题。乍一看,似乎您试图访问localhost:2181上的zookeeper,而在您的虚拟网络上,容器响应DNS名称";动物园管理员;(docker compose文件中的服务名称(,而不是localhost。你有没有试着用动物园管理员2181?

一旦修复,理想情况下,您会在服务中使用一个ENV变量来指定zookeeper主机/端口,这样它就可以进行配置,并在docker compose文件中进行设置。

最新更新