【问题标题】:Spring boot with embedded tomcat runs on a port but its won't work带有嵌入式 tomcat 的 Spring Boot 在端口上运行,但它不起作用
【发布时间】:2019-05-06 16:54:19
【问题描述】:

我是 Linux 的新手,我正在尝试运行带有嵌入式 tomcat 的 Spring Boot 应用程序,我尝试了互联网上的所有内容,但我无法理解我的问题。所以这是我的代码(Maven)

  <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test.my.app</groupId>
    <artifactId>TestApplication</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <start-class>com.test.my.Applocation.App</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>

        <dependency>
            <groupId>com.squareup.retrofit2</groupId>
            <artifactId>retrofit</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>logging-interceptor</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.retrofit2</groupId>
            <artifactId>converter-gson</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

这是我的 application.properties 文件。

server.port=0

这就是我将项目构建到 Jar 的方式:

mvn -> 清理 -> 编译 -> 打包

我可以在我的 Windows 计算机上运行这个应用程序,而且它工作得很好!但是我的问题出在Linux服务器上,当我使用systemed运行这个应用程序时,这是日志,它说它成功但它不工作

INFO 20351 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.hateoas.config.HateoasConfiguration' of type [org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$b190a49] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO 20351 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 0 (http)
INFO 20351 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
INFO 20351 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
INFO 20351 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
INFO 20351 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
INFO 20351 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2302 ms
INFO 20351 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
INFO 20351 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 41940 (http) with context path ''
INFO 20351 --- [           main] c.t.c.TransApplication.TransApplication  : Started TransApplication in 4.878 seconds (JVM running for 5.402)

这是我的

netstat -ltnpa | grep -i --color LISTEN

41940 端口记录

tcp6       0      0 :::41940                :::*                    LISTEN      20351/java 

但是现在我想用 example.com:41940/myrequest 向这个端口发送一个请求,但是它不工作并且服务器没有响应

==================== 编辑 我将 server.port=0 编辑为 8090

这是

的结果

curl -XGET 本地主机:8090

curl -XGET localhost:8090 % Total % Received % Xferd Average 速度 时间 时间 时间 当前 Dload Upload 总剩余速度 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 93 0 93 0 0 325 0 --:--:-- --:--:-- --:--:-- 326 { "_links" : { “轮廓” : { "href" : "http://localhost:8090/profile" } } }

【问题讨论】:

  • 您是否已经尝试过设置像 8080 这样的静态端口?
  • 我的 8080 端口不可用,但我尝试了 8090,但它也不起作用@HannesR
  • 设置静态端口时日志说什么?
  • 您有什么配置可以将 example.com 映射到服务器的 IP 地址?
  • 与 41940 端口完全一样,一切正常运行,但无法正常工作@HannesR

标签: java linux spring spring-boot tomcat


【解决方案1】:

尝试使用以下参数启动您的应用程序:

-Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Addresses

然后使用 netstat 确保应用程序正在侦听 IPv4。

如果它也无法正常工作,则发布此输出:

curl -sSL -D - http://localhost:8090 -o /dev/null

看看这里:Spring boot application listens over IPv6 without -Djava.net.preferIPv4Stack=true and -Djava.net.preferIPv4Addresses parameters

我认为你的 tomcat 只监听 IPv6 而不是 IPv4。

BR

【讨论】:

  • 嗨,我尝试使用 ipv6 运行,但我的问题解决了,非常感谢您的帮助
【解决方案2】:

正如我所说,我是 Linux 的新手,问题是我没有将端口引入防火墙并且现在可以工作

【讨论】:

    【解决方案3】:

    我正在将我的 Spring Boot 应用程序部署到 Pivotal Cloud Foundation 容器。 我遇到了同样的错误,但发现我缺少一个 POM 依赖项(看来你也是):

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    

    我在帖子中发现了这个: Spring boot: Unable to start embedded Tomcat servlet container

    注意,它给我带来了不同的错误,但我取得了进步。

    我的的结论对后代的影响......

    我添加此评论是因为这解决了我的问题。

    依赖项正在拉入 Servlet API 2.5 Tomcat 8.5(嵌入式 Spring Boot)需要 Servlet API 3.1!

    <groupId>org.jvnet.jax-ws-commons.spring</groupId>
        <artifactId>jaxws-spring</artifactId>
        <version>1.9</version>
        <exclusions>
            <exclusion>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
            </exclusion>
    

    依赖项有 Servlet 2.5 API。 Tomcat 8.1 需要 '3.1'

    发现查看Maven依赖树:

    man dependency:tree 
    

    来自这个帖子:Getting NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName()

    【讨论】:

    • 在spring starter web依赖中有一个嵌入的tomcat
    • 我添加此评论是因为这解决了我的问题。依赖项正在引入 Servlet API 2.5 Tomcat 8.5(嵌入式 Spring Boot)需要 Servlet API 3.1! org.jvnet.jax-ws-commons.springjaxws-spring1.9javax.servlet groupId> servlet-api 依赖有 Servlet 2.5 API。 Tomcat 8.1 需要 '3.1' 来自这篇文章:stackoverflow.com/questions/34950164/…
    猜你喜欢
    • 2016-10-10
    • 2019-03-01
    • 2020-04-06
    • 2019-05-26
    • 2017-09-14
    • 1970-01-01
    • 2015-07-31
    • 2017-10-21
    • 2020-11-09
    相关资源
    最近更新 更多