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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM