随笔 - 239  文章 - 0  评论 - 29  阅读 - 69万

springboot项目打包成war包部署到外部的tomcat

先在pom.xml文件里引入webflux依赖包:

        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>

在项目里添加一个额外的类即可:

import com.my.Application;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.adapter.AbstractReactiveWebInitializer;

@Slf4j
@Configuration
@ConditionalOnMissingBean(ReactiveWebServerFactory.class)
public class ConfigReactiveWebInitializer extends AbstractReactiveWebInitializer {

    @Override
    protected ApplicationContext createApplicationContext() {
        log.info("ConfigReactiveWebInitializer step 1. createApplicationContext" );
        SpringApplication springApplication = new SpringApplication(getConfigClasses());
        return springApplication.run();
    }

    @Override
    protected Class<?>[] getConfigClasses() {
        log.info("ConfigReactiveWebInitializer step 2. setConfigClasses = Application.class" ); 
        return new Class[]{ Application.class }; // 就是你的启动类
    }

    @Bean
    public NettyReactiveWebServerFactory nettyReactiveWebServerFactory() {
        log.info("ConfigReactiveWebInitializer step 3. register bean NettyReactiveWebServerFactory" );
        return new NettyReactiveWebServerFactory();
    }


}

配置application.properties:

server.port=8095
server.servlet.context-path=/my-service
spring.application.name=my-service
spring.main.web-application-type=reactive
spring.main.allow-bean-definition-overriding=true
management.endpoint.health.show-details=always
spring.profiles.active=
# 包含这些额外的properties文件:application-cron.properties; application-redis.properties; application-rabbitmq.properties;
spring.profiles.include=cron,redis,rabbitmq

 

 

end.

posted on   梦幻朵颜  阅读(238)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界

点击右上角即可分享
微信分享提示