[英]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.