[英]Spring boot with embedded tomcat runs on a port but its won't work
我在Linux中是一個新手,我想在運行帶有嵌入式tomcat的Spring Boot應用程序時,我實際上嘗試了Internet上的所有內容,但我不明白我的問題。 這是我的代碼(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服務器上,當我在系統上運行此應用程序時,這是日志,它說成功了,但是不起作用
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 --colour聽
從41940端口記錄
tcp6 0 0 :::41940 :::* LISTEN 20351/java
但是現在我想使用example.com:41940/myrequest向該端口發送請求,但它無法正常工作並且服務器無法響應
==================編輯我編輯server.port = 0到8090
這是
curl -XGET本地主機:8090
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 ”}}}
嘗試使用以下參數啟動您的應用程序:
-Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Addresses
然后使用netstat確保應用程序正在偵聽IPv4。
如果它也不起作用,則發布以下輸出:
curl -sSL -D - http://localhost:8090 -o /dev/null
在這里看看: Spring Boot應用程序在沒有-Djava.net.preferIPv4Stack = true和-Djava.net.preferIPv4Addresses參數的情況下通過IPv6進行偵聽
我認為您的tomcat僅偵聽IPv6,而不偵聽IPv4。
BR
我正在將Spring Boot應用程序部署到Pivotal Cloud Foundation容器。 我遇到了同樣的錯誤,但是發現我缺少POM依賴項(而且看來你也是):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
我在發布: 春季啟動:無法啟動嵌入式Tomcat Servlet容器中找到了這一點
注意,它為我導致了不同的錯誤,但是我取得了進步。
我的后代結論...
我添加此評論是因為這可以解決我的問題。
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
從這篇文章: Get NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName()
正如我說的那樣,我在Linux中是一個新手,問題是我沒有將端口引入防火牆,現在可以工作了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.