我正在开发Wildfly-Swarm应用,我想使用Consul作为我的服务发现。因此,我添加了拓扑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自我实现服务发现。
        <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:
我发现问题出在野蝇群参数-Djava.net.preferIPv4Stack=true上。当我使用此参数运行jar文件时,会遇到相同的异常,但是如果我将其删除以创建docker镜像的Dockerfile并运行,则会出现此异常:
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

最佳答案

它看起来类似于this问题。

您可以尝试将swarm.bind.address: 127.0.0.1添加到yml配置中吗?

关于java - Wildfly-Swarm Consul服务发现-无效的服务地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46430584/

10-09 00:27