運行springboot項目,如果你關閉當前shell窗口,就會導致服務器的springboot關閉。因爲我們現在用的是springboot自帶的tomcat,不能在後臺運行。
springboot生成的jar在阿里雲的linux服務器後臺運行,不會在shell客戶端關閉時關閉
通過創建stop.sh , start.sh ,run.sh這三個腳本文件來實現後臺長久運行springboot
這裏我把運行的qcl80.jar ,start .sh,stop.sh ,run.sh都放在home下的jar目錄下
1,創建stop.sh
vim stop.sh 創建文件然後把下面內容複製進去,一定要把qcl80.jar替換成你的jar
#!/bin/bash
PID=$(ps -ef | grep qcl80.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
echo Application is already stopped
else
echo kill $PID
kill $PID
fi
2,創建start.sh,這裏我們用80端口,這樣可以直接通過ip訪問,不用再輸端口了
vim start.sh 輸入這個命令後然後把下面的內容複製進去
#!/bin/bash
nohup java -jar qcl80.jar --server.port=80 &
3,創建run.sh
整合了關閉和啓動的腳本:run.sh,由於會先執行關閉應用,然後再啓動應用,這樣不會引起端口衝突等問題,適合在持續集成系統中進行反覆調用。
把下面內容複製進去,一定要注意複製時不能少東西
#!/bin/bash
echo stop application
source stop.sh
echo start application
source start.sh
4,start .sh,stop.sh ,run.sh都創建後
./run.sh 運行run.sh腳本
如果遇到沒有權限運行的問題,就在run.sh所在目錄下執行
chmod u+x *.sh 這樣就ok了。
執行完以後,我們可以去nohup.out文件中查看啓動的log
cat nohup.out 這個命令可以查看jar啓動的log
到此我們的springboot項目就啓動了,可以通過你阿里雲的公網ip訪問你的網站了
由於我配置了https,所以這裏用IP訪問會顯示不安全,接下來給大家講解怎麼配置https訪問。
四,配置https
1,申請阿里雲免費的ca證書,ca證書是實現https必不可少的
通常審覈10分鐘左右就行,如果資料不全或者不真實可能就久些。
- 購買完ca證書,並且審覈通過後,就去下載相應的ca證書,由於我們springboot內置的是tomcat,所以我們這裏下載tomcat對於的ca證書
下載後解壓
然後在我們的springboot配置文件中配置
注意:214590826650132.pfx還需要在我們能阿里雲的home/jar目錄下放一份,即和我們的打包jar放在同一個目錄下
HTTP自動轉向HTTPS
實現http轉https就是我們訪問
30paotui.com
www.30paotui.com
http://30paotui.com
http://www.30paotui.com
都會指向https://30paotui.com
- 實現上面的功能需要我們在springboot的application中配置如下代碼
package com.qcl;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SellApplication implements EmbeddedServletContainerCustomizer {
public static void main(String[] args) {
SpringApplication.run(SellApplication.class, args);
}
//攔截所有請求
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
//配置http轉https
@Bean
public Connector httpConnector() {
Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
//Connector監聽的http的端口號
connector.setPort(80);
connector.setSecure(false);
//監聽到http的端口號後轉向到的https的端口號
connector.setRedirectPort(443);
return connector;
}
//這裏設置默認端口爲443,即https的,如果這裏不設置,會https和http爭奪80端口
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(443);
}
}
至此,我們的springboot就可以在阿里雲上運行了,同時支持http和https的訪問