Docker:尝试在容器内运行java vert.x app。

时间:2022-03-22 15:16:02

I'm trying to run a vert.x java app inside a docker container. The app connects to a zookeeper instance running on another host. Connectivity to the zookeeper instance has been tested from the host and container. The app runs fine when I run it directly on the host. However, when I try to run the jar file inside the container it throws an error stating the following:

我正在尝试在docker容器中运行vert.x java app。该应用程序连接到在另一台主机上运行的zookeeper实例。已经从主机和容器测试了与zookeeper实例的连接。当我直接在主机上运行它时,应用程序运行正常。但是,当我尝试在容器内运行jar文件时,它会抛出一个错误,说明以下内容:

SLF4J: Actual binding is of type          [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

2018-01-15 11:05:15.126  INFO 7 --- [           main] c.b.vertxdemo.VertxdemoApplication       : Starting VertxdemoApplication v0.0.1-SNAPSHOT on de43fb40ccba with PID 7 (/tradingengine/vertxdemo-0.0.1-SNAPSHOT.jar started by root in /tradingengine)
2018-01-15 11:05:15.131  INFO 7 --- [           main] c.b.vertxdemo.VertxdemoApplication       : No active profile set, falling back to default profiles: default
2018-01-15 11:05:15.223  INFO 7 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4b9af9a9: startup date [Mon Jan 15 11:05:15 GMT 2018]; root of context hierarchy
Vertx Options PORT - 0
Vertx Options PUBLICPORT - -1
2018-01-15 11:05:15.992  INFO 7 --- [worker-thread-0] i.v.s.c.z.ZookeeperClusterManager        : Zookeeper hosts set to 10.1.0.199:2181
2018-01-15 11:05:16.131  INFO 7 --- [worker-thread-0] o.a.c.f.imps.CuratorFrameworkImpl        : Starting
2018-01-15 11:05:16.151  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT
2018-01-15 11:05:16.151  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:host.name=de43fb40ccba
2018-01-15 11:05:16.151  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:java.version=1.8.0_151
2018-01-15 11:05:16.151  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:java.vendor=Oracle Corporation
2018-01-15 11:05:16.151  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:java.home=/usr/lib/jvm/java-1.8-openjdk/jre
2018-01-15 11:05:16.151  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:java.class.path=vertxdemo-0.0.1-SNAPSHOT.jar
2018-01-15 11:05:16.151  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:java.library.path=/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64:/usr/lib/jvm/java-1.8-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-01-15 11:05:16.151  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:java.io.tmpdir=/tmp
2018-01-15 11:05:16.151  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:java.compiler=<NA>
2018-01-15 11:05:16.151  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:os.name=Linux
2018-01-15 11:05:16.152  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:os.arch=amd64
2018-01-15 11:05:16.152  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:os.version=3.10.0-693.5.2.el7.x86_64
2018-01-15 11:05:16.152  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:user.name=root
2018-01-15 11:05:16.152  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:user.home=/root
2018-01-15 11:05:16.152  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Client environment:user.dir=/tradingengine
2018-01-15 11:05:16.153  INFO 7 --- [worker-thread-0] org.apache.zookeeper.ZooKeeper           : Initiating client connection, connectString=10.1.0.199:2181 sessionTimeout=20000 watcher=org.apache.curator.ConnectionState@34191123
2018-01-15 11:05:16.184  INFO 7 --- [0.1.0.199:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server 10.1.0.199/10.1.0.199:2181. Will not attempt to authenticate using SASL (unknown error)
2018-01-15 11:05:16.201  INFO 7 --- [0.1.0.199:2181)] org.apache.zookeeper.ClientCnxn          : Socket connection established to 10.1.0.199/10.1.0.199:2181, initiating session
2018-01-15 11:05:16.220  INFO 7 --- [0.1.0.199:2181)] org.apache.zookeeper.ClientCnxn          : Session establishment complete on server 10.1.0.199/10.1.0.199:2181, sessionid = 0x160e3de5a400082, negotiated timeout = 20000
2018-01-15 11:05:16.230  INFO 7 --- [d-0-EventThread] o.a.c.f.state.ConnectionStateManager     : State change: CONNECTED
2018-01-15 11:05:16.353  INFO 7 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-01-15 11:05:16.371  INFO 7 --- [           main] c.b.vertxdemo.VertxdemoApplication       : Started VertxdemoApplication in 1.622 seconds (JVM running for 2.123)
2018-01-15 11:05:17.020 ERROR 7 --- [ntloop-thread-0] io.vertx.core.impl.VertxImpl             : Failed to start event bus

java.net.BindException: Address not available
    at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_151]
    at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_151]
    at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_151]
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_151]
    at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:128) ~[netty-transport-4.1.15.Final.jar!/:4.1.15.Final]
    at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558) ~[netty-transport-4.1.15.Final.jar!/:4.1.15.Final]
    at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1283) ~[netty-transport-4.1.15.Final.jar!/:4.1.15.Final]
    at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501) ~[netty-transport-4.1.15.Final.jar!/:4.1.15.Final]
    at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486) ~[netty-transport-4.1.15.Final.jar!/:4.1.15.Final]
    at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:989) ~[netty-transport-4.1.15.Final.jar!/:4.1.15.Final]
    at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254) ~[netty-transport-4.1.15.Final.jar!/:4.1.15.Final]
    at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:365) ~[netty-transport-4.1.15.Final.jar!/:4.1.15.Final]
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-common-4.1.15.Final.jar!/:4.1.15.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403) ~[netty-common-4.1.15.Final.jar!/:4.1.15.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463) ~[netty-transport-4.1.15.Final.jar!/:4.1.15.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) ~[netty-common-4.1.15.Final.jar!/:4.1.15.Final]
    at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_151]

Vertx Failed

Here is my code that I am trying to run:

package com.myapp.vertxdemo;

import java.util.UUID;

import javax.annotation.PostConstruct;

import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.spi.cluster.zookeeper.ZookeeperClusterManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.json.JsonObject;

@SpringBootApplication
@ComponentScan(basePackages = "com.myapp")
@EnableAutoConfiguration
public class VertxdemoApplication implements CommandLineRunner {

    @Value("${zookeeper.host}")
    String zookeeperHost;

    @Value("${zookeeper.cluster.host}")
    String zookeeperClusterHost;

    @Value("${zookeeper.cluster.port}")
    int zookeeperClusterPort;   

    @Autowired
    DemoVerticle demovertical;

    public static void main(String[] args) {

        SpringApplication.run(VertxdemoApplication.class, args);

    }

    @Override
    public void run(String... arg0) throws Exception {
        // TODO Auto-generated method stub

    }

    @PostConstruct
    private void Deploy() {

        JsonObject zkConfig = new JsonObject();
        zkConfig.put("zookeeperHosts", zookeeperHost);
        zkConfig.put("rootPath", "io.vertxdemo1");
        zkConfig.put("retry", new JsonObject().put("initialSleepTime", 3000).put("maxTimes", 3));
        ClusterManager mgr = new ZookeeperClusterManager(zkConfig);
        VertxOptions options = new VertxOptions()
                .setClustered(true)
                .setClusterHost(zookeeperClusterHost)
                //.setClusterPort(zookeeperClusterPort)
                .setClusterManager(mgr);

        System.out.println("Vertx Options PORT - " + options.getClusterPort());
        System.out.println("Vertx Options PUBLICPORT - " + options.getClusterPublicPort());

        String guid = UUID.randomUUID().toString();

        Vertx.clusteredVertx(options, res -> {
            if (res.succeeded()) {
                Vertx vertx = res.result();
                vertx.deployVerticle(demovertical);
                vertx.setPeriodic(5000, id -> {
                    vertx.eventBus().publish("PRICE_FEED", guid);
                });
                System.out.println("Vertx Deployed");
            } else {
                System.out.println("Vertx Failed");
            }
        });

    }

}

2 个解决方案

#1


0  

Your exception indicates that binding fails, I'm pretty sure the values you set for your zookeeper config are either not set or invalid.

您的异常表示绑定失败,我很确定您为zookeeper配置设置的值未设置或无效。

Debug the values of ${zookeeper.host}, ${zookeeper.cluster.host} and ${zookeeper.cluster.port}and if they're not set configure them properly

调试$ {zookeeper.host},$ {zookeeper.cluster.host}和$ {zookeeper.cluster.port}的值,如果没有设置,请正确配置它们

#2


0  

when you run the docker instance

当您运行docker实例时

docker run --network host

docker run --network host

The problem is the interface (and the address) you are trying to bind to is not available inside the docker (inside the container it's usually 172.17.x.x)

问题是您尝试绑定的接口(和地址)在docker中不可用(在容器内部通常是172.17.x.x)

when you specify the the host type networking (read here for more info https://docs.docker.com/network/#scope-of-this-topic) the container can use the ip of the host

当你指定主机类型网络时(在这里阅读更多信息https://docs.docker.com/network/#scope-of-this-topic)容器可以使用主机的IP

#1


0  

Your exception indicates that binding fails, I'm pretty sure the values you set for your zookeeper config are either not set or invalid.

您的异常表示绑定失败,我很确定您为zookeeper配置设置的值未设置或无效。

Debug the values of ${zookeeper.host}, ${zookeeper.cluster.host} and ${zookeeper.cluster.port}and if they're not set configure them properly

调试$ {zookeeper.host},$ {zookeeper.cluster.host}和$ {zookeeper.cluster.port}的值,如果没有设置,请正确配置它们

#2


0  

when you run the docker instance

当您运行docker实例时

docker run --network host

docker run --network host

The problem is the interface (and the address) you are trying to bind to is not available inside the docker (inside the container it's usually 172.17.x.x)

问题是您尝试绑定的接口(和地址)在docker中不可用(在容器内部通常是172.17.x.x)

when you specify the the host type networking (read here for more info https://docs.docker.com/network/#scope-of-this-topic) the container can use the ip of the host

当你指定主机类型网络时(在这里阅读更多信息https://docs.docker.com/network/#scope-of-this-topic)容器可以使用主机的IP