应用场景:微信小程序,需要一个https端口,正常后台是http端口
1.修改配置文件(.yml)
# 开发环境配置
server:
# 服务器的HTTP端口,默认为8080
port: 8080
# 新增开始
# https证书配置,不写就是俩http端口
# 证书放到 src/main/resources
ssl:
key-store: classpath:证书名.pfx
key-alias: alias
key-store-password: 秘钥
key-store-type: PKCS12
# 新增结束
servlet:
# 应用的访问路径
context-path: /
tomcat:
# tomcat的URI编码
uri-encoding: UTF-8
# 连接数满后的排队数,默认为100
accept-count: 1000
threads:
# tomcat最大线程数,默认为200
max: 800
# Tomcat启动初始化的线程数,默认值10
min-spare: 100
# 新增,可以不加,新端口可以直接写到启动类里面
http: # 新加一个http的端口号配置
port: 8081
2.修改启动类(XxApplication)
package com.xx;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
/**
* 启动程序
*
* @author
*/
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class XxApplication
{
//新增开始
@Value("${http.port}")
private Integer port;
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
return tomcat;
}
// 配置http
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(port);
return connector;
}
//新增结束
public static void main(String[] args)
{
// System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(XskApplication.class, args);
}
}