最近在学习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的访问