最近在學習springboot,感受springboot開發後臺,提供api接口太方便了。簡直是傻瓜式開發,一直都是本地在跑springboot項目。夢想着有一天,項目能在阿里雲上跑。只有在阿里雲上跑纔是真正的java服務器項目。這裏就帶你們一塊兒把springboot項目部署到阿里雲,而且支持https ####準備工做java
至於域名怎麼買,我就不囉嗦了,不會的自行百度 30paotui.com我買的域名 linux
#!/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 輸入這個命令後而後把下面的內容複製進去web
#!/bin/bash
nohup java -jar qcl80.jar --server.port=80 &
複製代碼
3,建立run.shspring
整合了關閉和啓動的腳本:run.sh,因爲會先執行關閉應用,而後再啓動應用,這樣不會引發端口衝突等問題,適合在持續集成系統中進行反覆調用。 把下面內容複製進去,必定要注意複製時不能少東西shell
#!/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啓動的logapache
到此咱們的springboot項目就啓動了,能夠經過你阿里雲的公網ip訪問你的網站了 vim
下載後解壓 api
而後在咱們的springboot配置文件中配置 tomcat
注意:214590826650132.pfx還須要在咱們能阿里雲的home/jar目錄下放一份,即和咱們的打包jar放在同一個目錄下 安全
實現http轉https就是咱們訪問 30paotui.com www.30paotui.com 30paotui.com www.30paotui.com 都會指向30paotui.com
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的訪問