我正在开发 Wildfly-Swarm 应用程序,我想使用 Consul 作为我的服务发现。所以我添加了 topology-consul 分数,在 project-defaults.yml 中设置我的 Consul 路径并添加@Advertise("service-name")
到我的 Endpoint。
如果我开始我的应用程序使用
java –jar my-swarm-app.jar
一切正常。
我的项目-defaults.yml:
service:
catalog:
service-name: "service-name"
swarm:
port:
offset: 501
consul:
url: "http://172.30.3.80:8500"
但是当我用这个 Dockerfile 将我的胖 jar 打包到 Docker 映像中时:
FROM openjdk:8-jre-alpine
ADD my-swarm-app.jar /opt/my-swarm-app.jar
EXPOSE 8581
ENTRYPOINT ["java", "-jar", "-Djava.net.preferIPv4Stack=true", "/opt/my-swarm-app.jar"]
构建它:
docker build -f Dockerfile -t my-swarm-app .
并像这样运行它:
docker run -p 8581:8581 my-swarm-app
我得到以下异常:
2017-09-26 15:17:54,240 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-4) MSC000001: Failed to start service swarm.topology.register.consent-service.http: org.jboss.msc.service.StartException in service swarm.topology.register.consent-service.http: com.orbitz.consul.ConsulException: Invalid service address
at org.wildfly.swarm.topology.deployment.RegistrationAdvertiser.start(RegistrationAdvertiser.java:79)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.orbitz.consul.ConsulException: Invalid service address
at com.orbitz.consul.AgentClient.register(AgentClient.java:180)
at com.orbitz.consul.AgentClient.register(AgentClient.java:184)
at org.wildfly.swarm.topology.consul.runtime.Advertiser.advertise(Advertiser.java:65)
at org.wildfly.swarm.topology.consul.runtime.ConsulTopologyConnector.advertise(ConsulTopologyConnector.java:60)
at org.wildfly.swarm.topology.deployment.RegistrationAdvertiser.start(RegistrationAdvertiser.java:77)
... 5 more
难道我做错了什么?
编辑:我尝试使用 consul-api 自行实现服务发现。
<dependency>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
<version>1.2.4</version>
</dependency>
像这样:
@ApplicationScoped
public class Config {
private ConsulClient client;
@Inject
@ConfigurationValue("swarm.http.port")
private Integer port;
public void init(@Observes @Initialized(ApplicationScoped.class) ServletContext context) {
client = new ConsulClient("http://172.30.3.80:8500");
// register new service with associated health check
NewService newService = new NewService();
newService.setId("myapp_02");
newService.setTags(Collections.singletonList("EU-East"));
newService.setName("myapp_aaa");
newService.setPort(port);
client.agentServiceRegister(newService);
}
}
它在 docker 镜像中工作。这可能是 Wildfly-Swarm 拓扑分数中的一个错误,还是我缺少一些配置?
编辑 2:我发现问题出在 wildfly-swarm 参数-Djava.net.preferIPv4Stack=true
上。当我使用此参数运行 jar 文件时,我得到相同的异常,但如果我删除它 Dockerfile 以创建 docker 映像并运行它,我得到此异常:
2017-09-27 20:34:46,460 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-8) MSC000001: Failed to start service jboss.undertow.listener.default: org.jboss.msc.service.StartException in service jboss.undertow.listener.default: WFLYUT0082: Could not start 'default' listener.
at org.wildfly.extension.undertow.ListenerService.start(ListenerService.java:153)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketException: Protocol family unavailable
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.xnio.nio.NioXnioWorker.createTcpConnectionServer(NioXnioWorker.java:171)
at org.xnio.XnioWorker.createStreamConnectionServer(XnioWorker.java:245)
at org.wildfly.extension.undertow.HttpListenerService.startListening(HttpListenerService.java:126)
at org.wildfly.extension.undertow.ListenerService.start(ListenerService.java:142)
... 5 more
这是 github 项目的链接,您可以在其中重现错误: https ://github.com/pkristja/wildfly-swarm-consul-demo