【问题标题】:Spring Boot publish REST & SOAP parallelSpring Boot 并行发布 REST 和 SOAP
【发布时间】:2017-05-04 07:24:55
【问题描述】:

我无法连接到在端口 9000 上侦听运行 Spring-Boot 应用程序的 SOAP 服务。该应用程序在 docker 容器中运行。问题是soap-endpoint正在监听localhost而不是0.0.0.0。

我想将肥皂服务绑定到 0.0.0.0:9000 而不是 127.0.0.1:9000。我怎样才能做到这一点?

这是我的设置:

Application.properties
soap.url = http://localhost:9000/
rest.url = http://localhost:9090/
server.port = 9090

Spring-boot 正确启动

Springboot-Startup-log
2017-05-04 06:36:53.095  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9090 (http)
2017-05-04 06:36:53.110  INFO 1 --- [           main] ch.gemdat.Application                    : Started Application in 28.285 seconds (JVM running for 29.465)
2017-05-04 06:36:55.387  INFO 1 --- [           main] myapp                                    : - services/soap/form/call/v1
2017-05-04 06:36:55.388  INFO 1 --- [           main] myapp                                    : >> myapp successfully started in 0:31 <<

tomcat (rest-services) 启动并在接口 0.0.0.0 上侦听默认端口 9090。肥皂服务绑定到 localhost:9000。

netstat -tulpn
tcp        0      0 0.0.0.0:9090            0.0.0.0:*               LISTEN      1/java
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      1/java

我完全可以连接到端口 9090 上运行的所有服务。因为它们在 0.0.0.0 上正确侦听。

这就是我发布肥皂服务的方式:

private static void publishSoapServices() {
    MyAppLogger.info("Register soap endpoints...");
    BeanDefinitionRegistry beanDefRegistry = new SimpleBeanDefinitionRegistry();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanDefRegistry);
    scanner.addIncludeFilter(new AssignableTypeFilter(MyAppService.class));
    scanner.scan(Constants.SOAP_PACKAGE);

    for (String bean : beanDefRegistry.getBeanDefinitionNames()) {
        if (!bean.contains("springframework")) {
            try {
                Class<?> soapClass = Class.forName(beanDefRegistry.getBeanDefinition(bean).getBeanClassName());
                if (MyAppSoapService.class.isAssignableFrom(soapClass)) {
                    MyAppService soapService = (MyAppService) soapClass.newInstance();
                    Endpoint.publish(PropertyReader.getValue("soap.url") + soapService.getEndpoint(), soapService);
                    ctx.getAutowireCapableBeanFactory().autowireBean(soapService);
                    MyAppLogger.info("- " + soapService.getEndpoint());
                }
            } catch (Exception e) {
                MyAppLogger.error("Soap service ''{0}'' could not be published.", e, bean);
            }
        }
    }
}

所有服务都可以在 localhost 上使用。表示http://localhost:9000/services/soap/form/call/v1 有效。但是当我在 docker 容器中运行它时(运行参数:-p 9000:9000 -p 9090:9090)只有其余服务可用(因为它们只听本地主机......而休息服务听所有 ips (0.0.0.0))

Docker 检查显示端口映射正确:

"Ports": {
            "9000/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "9000"
                }
            ]

【问题讨论】:

  • Endpoint.publish(PropertyReader.getValue(http://localhost:9000/) + soapService.getEndpoint(), soapService); 这不是有效的 Java 代码,如果你用 localhost:9000 调用它,你只会绑定到 localhost
  • @P.J.Meisch 感谢您的评论。你是对的。我想让代码更容易理解,并将 application.properties 中的 key 替换为值。但是做错了。我更正它以显示实际代码。

标签: java web-services spring-boot binding


【解决方案1】:

经过反复试验,我找到了解决方案。

Application.properties
soap.url = http://0.0.0.0:9000/
rest.url = http://localhost:9090/
server.port = 9090

解决方法是将application.properties方法中使用的soap.url设置为http://0.0.0.0:9000/。它不适用于我最初尝试的0.0.0.0:9000/

那么为什么它对其余端口有效? 好吧,它没有。其余服务有效,因为端口未在rest.url 下从application.properties 发布。它们通过从 application.properties 读取 server.port 绑定到初始 spring-boot-app-port 集。该端口被tomcats默认监听0.0.0.0的接口覆盖

正如 P.J.Meisch 所说 - 只是 soap.url 绑定到 localhost 是问题所在。

【讨论】:

  • 也只是想回答这个问题,您需要将 Endpoint 绑定到 http://0.0.0.0:9000/ 而不是 localhost。很高兴你自己找到了它;-)
  • 感谢您的帮助。我现在尝试解决这个问题 2 天。您为此获得了额外的支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多