我正在开发Wildfly-Swarm应用程序,我想使用Consul作为我的服务发现.所以我添加了topology-consul分数,在project-defaults.yml中设置了Consul路径,并将@Advertise(“service-name”)添加到了我的Endpoint.
如果我开始使用我的应用程序
java –jar my-swarm-app.jar
一切正常.
我的project-defaults.yml:
service:
catalog:
service-name: "service-name"
swarm:
port:
offset: 501
consul:
url: "http://172.30.3.80:8500"
但是当我使用这个Dockerfile将我的胖罐包装在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自我实现服务发现.
像这样:
@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 paramater -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
最佳答案