SpringBoot项目jar包部署在阿里云上使用https和http方式访问

目录

准备工作

一、springboot项目中配置application.yml文件。

二、在springboot的application中配置。

三、我在运行后会报如下错误。

四、成功运行

总结


准备工作

  • ca证书一份(用来支持https,需要自行申请)
  • 本地打包好的springboot项目jar包
  • 阿里云服务器以及域名(暂在本地运行,有需要自行百度购买)

一、springboot项目中配置application.yml文件。

application.yml配置如下:

server:
  port: 443
  ssl:
    enabled: true
    key-store: *******.pfx    #CA证书中的pfx文件名,需要把该文件放到项目根目录
    key-store-password: *******    #CA证书中的password.txt文件内容

二、在springboot的application中配置。

application配置如下:

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.apache.catalina.connector.Connector;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* 单体启动类(采用此类启动为单体模式)
*/
@Slf4j
@SpringBootApplication
public class JeecgSystemApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(JeecgSystemApplication.class);
    }

    
    // 配置http
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
        return tomcat;
    }

    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(443);
        return connector;
    }

}

三、我在运行后会报如下错误。

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to multiple ServletWebServerFactory beans : tomcatServletWebServerFactory,webServerFactory

通过debug发现是以前写的方法与此方法冲突,导致系统不知道运行哪一个,找到并注释掉以前那个方法就能成功运行

解决方法:

代码搜索tomcat找到@Bean注解的那个TomcatServletWebServerFactory方法注释掉

四、成功运行

成功运行后网页输入https://本机ip:443/yhs/doc.html即可成功访问到我的springboot项目接口文档页面


总结

本文仅记录本人工作中遇到的难点与个人见解,如有问题请见谅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值