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裏面的配置文件注入的問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章