[英]Spring boot - Rest Call client without embedded tomcat
我一直在試圖找出 Spring Boot 的一個問題,因為我是 Spring 的新手,所以我想在這里獲得一些幫助。
我有一個基於 Spring Boot 的 java 應用程序,它作為守護進程運行,並向遠程服務器發出一些 GET 請求。 (僅作為客戶)。
但是我的 spring boot 應用程序在內部啟動了一個嵌入式 tomcat 容器。 我的理解是,如果 java 應用程序充當服務器,則需要 tomcat。 但是我的應用程序只是遠程機器的 GET API 的使用者,為什么它需要一個嵌入式 tomcat ?
在我的 pom 文件中,我指定了 spring-boot-starter-web,假設它甚至需要進行 GET 調用。
但是在對禁用嵌入式 tomcat 做了一些研究之后,我找到了一個解決方案。
要進行以下更改,
@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,
WebMvcAutoConfiguration.class})
& 在 application.yml 中
spring:
main:
web-environment: false
隨着 application.yml 的變化,我的 jar 甚至沒有開始,直接中止,甚至沒有在 logback 日志中記錄任何內容。
現在,如果我刪除 application.yml 更改,我的 jar 將啟動(僅在 @SpringBootApplication anno 中進行第一次更改。)但會出現一些異常。
[main] o.s.boot.SpringApplication : Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
我的疑問是,
1)tomcat,無論是獨立的還是嵌入式的,真的需要一個只對遠程機器進行GET API調用的應用程序嗎?
2) 我如何克服這個異常並安全地刪除嵌入式 tomcat 並仍然執行 GET API 調用?
您似乎完全走錯了路,從 Web 應用程序模板開始,然后嘗試關閉 Web 應用程序方面。
最好從常規命令行客戶端模板開始,然后從那里開始,如相關 Spring 指南中所述。
基本上應用程序減少到
@SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String args[]) {
SpringApplication.run(Application.class);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
Quote quote = restTemplate.getForObject(
"http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
};
}
}
和 pom 到
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
我有這個問題。 我想要的只是讓一個客戶端發出 REST 請求。 不幸的是,我有一個嵌入 Jetty 的依賴項,而 Jetty 總是被啟動。
為了禁用 Jetty,我需要做的就是在 applications.properties 中添加以下條目:
spring.main.web-application-type=none
那修復了它。
這是對我來說最簡單的解決方案,讓 spring boot 應用程序只是一個安靜的 api 消費者。
替換依賴
implementation("org.springframework.boot:spring-boot-starter-web")
和
implementation("org.springframework.boot:spring-boot-starter-json")
RestTemplate
和jackson
在項目中可用,沒有內嵌tomcat。
回答您的問題:
1) 由默認嵌入 - 客戶端 HTTP 請求不需要;
2) 您可以在沒有任何 Web 的情況下將 CommandLineRunner 用於 spring boot 應用程序:
@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringBootConsoleApplication.class, args);
}
@Override
public void run(String... args) {
// TODO: start you thread here to execute client HTTP REST requests (or any other job you need)
}
}
這將完全禁用網絡 - 沒有手動錯誤配置的問題。
這是一些文檔: http : //www.baeldung.com/spring-boot-console-app
您還需要使用 spring-boot-starter 替換 spring-boot-starter-web 依賴項:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
根據您的問題,我假設您希望您的應用程序繼續在后台運行,並在其生命周期中進行一些 get 調用。 如果是這樣,那么
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.