springboot项目部署到独立tomcat的爬坑集锦

今天正月十五,可怜的我还在这里码字,首先祝大家“猿宵节”快乐!距离我发布的spring cloud初级教程已经有段时间了,这段时间经历了一个春节,加上年后我又有了点事情要做,所以我在初级教程中预告的spring cloud手脚架项目估计要食言了。不过今天冒个泡,就是让大家知道,事情我还在做,什么时间做完,这个对于我来说也是很重要的事情。这次不预告时间,因为我也有许多其他事情要忙。所以在这里跟大家说声对不起了。

集锦一:普通的springboot项目直接部署jar包

因为我的项目中包含了静态资源,以及上传路径,如果直接部署,我没底springboot会给我出什么幺蛾子。当然我也试过了直接部署,启动的时候各种报错,加上传言说生产环境部署springboot会额外加很多东西,并且你不知道它什么时候就会变的很慢。我在云服务器上启动的时候速度奇慢,并且静态资源也出问题,所以我就放弃了,换tomcat独立部署。不过如果你打完jar包后,使用以下命令可以不改配置文件中的端口启动服务。

java -jar ./yourjar.jar --server.port=8080

集锦二:springboot项目不能直接打war包部署

两步可以搞定这个问题,第一个步骤有两种方式,我只选了其中对我来说最方便的方式

  1. 修改pom.xml
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。
                相当于compile,但是打包阶段做了exclude操作-->
            <scope>provided</scope>
        </dependency>
  1. 修改启动文件,让application继承SpringBootServletInitializer
public class WebsiteApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
       SpringApplication.run(WebsiteApplication.class, args);
    } 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WebsiteApplication.class);
    }
}

集锦三:因为tomcat版本问题导致的lombok插件报错:Invalid byte tag in constant pool: 19

我在部署的过程中,除了这个还有log4j-api也报了一个类似的错误,不过好像是Invalid byte tag in constant pool: 18,有人说是要降lo4j-api的版本来解决,而我不想这么做,所以我将tomcat 7升级到了tomcat 8,问题解决。

集锦四:Tomcat 启动的时候总是出现大量如下的警告

警告: Unable to add the resource at [/WEB-INF/lib/springwebmvc-3.0.2.RELEASE.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache

找到conf/context.xml配置文件,在context里面添加如下配置:

<Resources cachingAllowed="true" cacheMaxSize="100000" /> 

集锦五:springboot打包war包时pom.xml提示war标签出现错误

因为我们的springboot项目是没有web.xml的,而打包时默认的maven-war-plugin版本太低,导致从jar换到war时,因为没有web.xml文件打包插件就不知道怎么玩了。升级方式如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.0</version>
</plugin>

集锦六:部署到独立tomcat,springboot项目filter无法启用

具体的问题是@ServletComponentScan失效,前面我修改了启动文件,放到tomcat的时候,启动时走的流程与springboot不一样,所以注解失效了。咋解:

  1. filter该以前怎么写现在照样怎么写
  2. 加入如下文件,并在启动文件上将此配置加入@ComponentScan
@Configuration
public class RegistrationFilterConfig {
    @Value("${except.url}")
    private String exceptUrls ;
    @Bean
    public FilterRegistrationBean<UserLoginFilter> regUserLoginFilterBean() {
        FilterRegistrationBean<UserLoginFilter> bean = new FilterRegistrationBean<UserLoginFilter>();
        bean.setFilter(new UserLoginFilter());
        bean.addUrlPatterns("/admin/*", "/user/*" , "/api/*");
        bean.setName("userLoginFilter");
        bean.addInitParameter("exceptUrls", exceptUrls);
        bean.setOrder(1);
        return bean;
    }
}

集锦七:filter直接使用@Value注解的配置文件为空

上面一个问题,大家看到我的exceptUrls这个参数了吧,这个参数原先是在filter里面的,换了新的注册方式后,就注入不进来了,如何优雅的解决?关键就是在这里

bean.addInitParameter("exceptUrls", exceptUrls);

然后filter里面加入如下代码

    private String exceptUrls ;
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        exceptUrls = filterConfig.getInitParameter("exceptUrls");
    }

即可解决filter里面的配置文件注入的问题。

猜你喜欢

转载自www.cnblogs.com/yb2020/p/10403482.html