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 2022-12-17 19:17  梦幻朵颜  阅读(215)  评论(0编辑  收藏  举报