java - Spring Boot 并行发布 REST 和 SOAP

标签 java web-services spring-boot binding

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

我想在 0.0.0.0:9000 而不是 127.0.0.1:9000 上绑定(bind) soap 服务。我怎样才能做到这一点?

这是我的设置:

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)启动并在接口(interface) 0.0.0.0 上默认监听端口 9090。肥皂服务绑定(bind)到 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);
            }
        }
    }
}

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

Docker inspect 显示端口映射正确:

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

最佳答案

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

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/ 我最初尝试的内容。

为什么它对其余端口有效? 好吧,事实并非如此。其余服务工作是因为端口未在 application.propertiesrest.url 下发布。它们通过从 application.properties 中读取 server.port 绑定(bind)到初始的 spring-boot-app-port 集。该端口被 tomcats 默认监听接口(interface)覆盖 0.0.0.0

正如 P.J.Meisch 所说 - 这只是 soap.url 绑定(bind)到 localhost 的问题。

关于java - Spring Boot 并行发布 REST 和 SOAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43776467/

相关文章:

java - 如何使用java运行setup.exe文件

asp.net - 使用asp.net发送20,000多个电子邮件

spring - 如何从休息服务开始批处理。 afterJob 运行但 EnableBatchProcessing 仅在部署后运行

java - JPAQuery 与 Spring Pageable

mysql - 使用 Spring Data JPA 自定义存储库方法将数据从 csv 加载到 mysql 表

java - 按路径将 zip 文件夹中的文件放入字符串数组中

java - 打开doc文件时"The document is really a OOXML file"

java - Android:用于设置 NestedScrollView 最大高度的自定义类不起作用(没有滚动条)

php - 如何在 PHP 中的多个 get 请求之间共享对象?

android - 适用于 Android 和 Mysql 的最佳 Web 服务