spring cloud 问题记录之----Tomcat 端口无法改变

问题现象:

    在sping boot2.0.4版本中,搭建好配置中心,注册中心,通过cloud config client访问,在正常访问到配置中心后,cloud config client的端口始终为18061,无论怎么改变配置文件,都不生效。

 

解决办法:

      (1)自定义一个TomcatConfig类


import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;


/**
 * 使用tomcat配置
 *
 * @version 
 * @author 
 * 
 */
@Configuration
public class TomcatConfig {

    @Value("${spring.server.port}")
    private String port;
    @Value("${spring.server.acceptorThreadCount}")
    private String acceptorThreadCount;
    @Value("${spring.server.minSpareThreads}")
    private String minSpareThreads;
    @Value("${spring.server.maxSpareThreads}")
    private String maxSpareThreads;
    @Value("${spring.server.maxThreads}")
    private String maxThreads;
    @Value("${spring.server.maxConnections}")
    private String maxConnections;
    @Value("${spring.server.protocol}")
    private String protocol;
    @Value("${spring.server.redirectPort}")
    private String redirectPort;
    @Value("${spring.server.compression}")
    private String compression;
    @Value("${spring.server.connectionTimeout}")
    private String connectionTimeout;

    @Value("${spring.server.MaxFileSize}")
    private String MaxFileSize;
    @Value("${spring.server.MaxRequestSize}")
    private String MaxRequestSize;

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addConnectorCustomizers(new GwsTomcatConnectionCustomizer());
        return tomcat;
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //  单个数据大小
        factory.setMaxFileSize(MaxFileSize); // KB,MB
        /// 总上传数据大小
        factory.setMaxRequestSize(MaxRequestSize);
        return factory.createMultipartConfig();
    }
 
    public class GwsTomcatConnectionCustomizer implements TomcatConnectorCustomizer {

        public GwsTomcatConnectionCustomizer() {
        }

        @Override
        public void customize(Connector connector) {
            connector.setPort(Integer.valueOf(port));
            connector.setAttribute("connectionTimeout", connectionTimeout);
            connector.setAttribute("acceptorThreadCount", acceptorThreadCount);
            connector.setAttribute("minSpareThreads", minSpareThreads);
            connector.setAttribute("maxSpareThreads", maxSpareThreads);
            connector.setAttribute("maxThreads", maxThreads);
            connector.setAttribute("maxConnections", maxConnections);
            connector.setAttribute("protocol", protocol);
            connector.setAttribute("redirectPort", "redirectPort");
            connector.setAttribute("compression", "compression");
        }
    }
}

 

  (2)在application.yml添加如下配置,最后问题解决了

spring:
     server: 
         port: 8901
         acceptorThreadCount: 4
         minSpareThreads: 50
         maxSpareThreads: 50
         maxThreads: 1000
         maxConnections: 10000
         connectionTimeout: 10000
         protocol: org.apache.coyote.http11.Http11Nio2Protocol
         redirectPort: 443
         compression: on
         MaxFileSize: 300MB
         MaxRequestSize: 500MB 

 

 

参考https://blog.csdn.net/wd2014610/article/details/79587161

 

 

说明:这里的主要原因不在于不能修改,而是git中的配置项server.portt和本地的server.port不一致导致的。将git配置中的server.port去掉,问题解决

你可能感兴趣的:(Spring,Cloud)