简体   繁体   English

具有嵌入式tomcat的Spring Boot在端口上运行,但无法正常工作

[英]Spring boot with embedded tomcat runs on a port but its won't work

I'm so new in Linux and I'm trying to run my Spring boot application with embedded tomcat on it I tried literally everything on the internet but I can't understand my problem. 我在Linux中是一个新手,我想在运行带有嵌入式tomcat的Spring Boot应用程序时,我实际上尝试了Internet上的所有内容,但我不明白我的问题。 So this is my code (Maven) 这是我的代码(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>

and this is my application.properties file. 这是我的application.properties文件。

server.port=0

and this is how I build my project to Jar : 这就是我将项目构建到Jar的方式:

mvn -> clean -> compile -> package mvn->清理->编译->包

and I can run this application on my Windows computer and its working just fine! 我可以在Windows计算机上运行该应用程序,并且可以正常运行! but my problem is in the Linux server when I run this application with systemed this is the log and it says it was successful but it's not working 但是我的问题出在Linux服务器上,当我在系统上运行此应用程序时,这是日志,它说成功了,但是不起作用

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)

and this is my 这是我的

netstat -ltnpa | netstat -ltnpa | grep -i --colour LISTEN grep -i --colour听

log from 41940 port 41940端口记录

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

but now i want to send a request to this port with example.com:41940/myrequest but its not working and server is not respondig 但是现在我想使用example.com:41940/myrequest向该端口发送请求,但它无法正常工作并且服务器无法响应

=================== EDIT i edit the server.port=0 to 8090 ==================编辑我编辑server.port = 0到8090

and this is the result of 这是

curl -XGET localhost:8090 curl -XGET本地主机:8090

curl -XGET localhost:8090 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 93 0 93 0 0 325 0 --:--:-- --:--:-- --:--:-- 326 { "_links" : { "profile" : { "href" : " http://localhost:8090/profile " } } } curl -XGET localhost:8090%总计%接收的%Xferd平均速度时间时间时间时间当前Dload上传总计已花费左速度0 0 0 0 0 0 0 0-:-:--:-:-- :-:-0100 93 0 93 0 0 325 0-:-:--:-:--:-:-326 {“ _links”:{“ profile”:{ “ href”:“ http:// localhost:8090 / profile ”}}}

Try to start your application with this parameters: 尝试使用以下参数启动您的应用程序:

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

And then with netstat make sure that the application is listening on IPv4. 然后使用netstat确保应用程序正在侦听IPv4。

If it is also not working post the output of this: 如果它也不起作用,则发布以下输出:

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

Have a look here: Spring boot application listens over IPv6 without -Djava.net.preferIPv4Stack=true and -Djava.net.preferIPv4Addresses parameters 在这里看看: Spring Boot应用程序在没有-Djava.net.preferIPv4Stack = true和-Djava.net.preferIPv4Addresses参数的情况下通过IPv6进行侦听

I think your tomcat only listens on IPv6 not IPv4. 我认为您的tomcat仅侦听IPv6,而不侦听IPv4。

BR BR

I am deploying my Spring Boot application to a Pivotal Cloud Foundation container. 我正在将Spring Boot应用程序部署到Pivotal Cloud Foundation容器。 I got the same error but found I was missing a POM dependency (and it appears you are too): 我遇到了同样的错误,但是发现我缺少POM依赖项(而且看来你也是):

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

I found this in Post: Spring boot: Unable to start embedded Tomcat servlet container 我在发布: 春季启动:无法启动嵌入式Tomcat Servlet容器中找到了这一点

NOTE, it has led to different error for me, but I made progress. 注意,它为我导致了不同的错误,但是我取得了进步。

Conclusion of my matter for posterity ... 的后代结论...

I add this comment because this is what fixed my problem. 我添加此评论是因为这可以解决我的问题。

The Dependency was pulling in Servlet API 2.5 Tomcat 8.5 (embedded Spring Boot) needs Servlet API 3.1! 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>

Dependency had Servlet 2.5 API. 依赖具有Servlet 2.5 API。 Tomcat 8.1 needs '3.1' Tomcat 8.1需要“ 3.1”

found to looking at Maven dependency tree: 发现要查看Maven依赖树:

man dependency:tree 

From this post: Getting NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName() 从这篇文章: Get NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName()

正如我说的那样,我在Linux中是一个新手,问题是我没有将端口引入防火墙,现在可以工作了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM