gpt4 book ai didi

java - Docker:尝试在容器内运行 java vert.x 应用程序。

转载 作者:行者123 更新时间:2023-12-02 11:45:13 25 4
gpt4 key购买 nike

我正在尝试在 docker 容器内运行 vert.x java 应用程序。该应用程序连接到在另一台主机上运行的 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

这是我尝试运行的代码:

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

}

}

最佳答案

当您运行 docker 实例时

docker run --network host

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

当您指定主机类型网络时(请阅读此处了解更多信息 https://docs.docker.com/network/#scope-of-this-topic ),容器可以使用主机的 IP

关于java - Docker:尝试在容器内运行 java vert.x 应用程序。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48260496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com