zoukankan      html  css  js  c++  java
  • springboot 2 修改端口号

    springboot 废弃了EmbeddedServletContainerCustomizer ,修改端口,从官方文档上看到的方法,

    import org.springframework.boot.web.server.WebServerFactoryCustomizer;
    import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
    import org.springframework.stereotype.Component;
    
    @Component
    public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
    
        @Override
        public void customize(ConfigurableServletWebServerFactory server) {
            server.setPort(9000);
        }
    
    }

    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.setPort(9000);
        factory.setSessionTimeout(10, TimeUnit.MINUTES);
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
        return factory;
    }

    3

    75.13 Enable Multiple Connectors with Tomcat
    You can add an org.apache.catalina.connector.Connector to the TomcatServletWebServerFactory, which can allow multiple connectors, including HTTP and HTTPS connectors, as shown in the following example:
    
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createSslConnector());
        return tomcat;
    }
    
    private Connector createSslConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
        try {
            File keystore = new ClassPathResource("keystore").getFile();
            File truststore = new ClassPathResource("keystore").getFile();
            connector.setScheme("https");
            connector.setSecure(true);
            connector.setPort(8443);
            protocol.setSSLEnabled(true);
            protocol.setKeystoreFile(keystore.getAbsolutePath());
            protocol.setKeystorePass("changeit");
            protocol.setTruststoreFile(truststore.getAbsolutePath());
            protocol.setTruststorePass("changeit");
            protocol.setKeyAlias("apitester");
            return connector;
        }
        catch (IOException ex) {
            throw new IllegalStateException("can't access keystore: [" + "keystore"
                    + "] or truststore: [" + "keystore" + "]", ex);
        }
    }

    个人比较喜欢第二种方法。

    真的是和1 的变化太大,踩了好多的坑

    如果要在application.properties 修改端口号,很简单,只需要写上

    server.port= 端口号,就可以了
  • 相关阅读:
    【原】Storm序列化
    【原】Storm学习资料推荐
    【原】Storm实战
    【原】Storm环境搭建
    Autofac基本使用(转载)
    Autofac学习之三种生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency
    RabbitMQ 默认端口号
    RabbitMQ-客户端
    百度地图-生成器
    uploadify 配置后,页面显示无效果
  • 原文地址:https://www.cnblogs.com/zwb1234/p/9209997.html
Copyright © 2011-2022 走看看