Spring Boot 自定义端口等

版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking 本文版权归作者和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。 https://blog.csdn.net/xunzaosiyecao/article/details/81806200

一、Spring Boot 1.X

/**
 * Created by jiankunking on 2018/2/26.
 */
@RestController
@EnableAutoConfiguration
public class CustomPortController implements EmbeddedServletContainerCustomizer {
    /**
     * 自定义端口
     *
     * @param container
     */
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setPort(EnvionmentVariables.PORT);
    }

    @RequestMapping("/")
    public String setPort(int port) {
        return String.valueOf(port);
    }

}

二、Spring Boot 2.X

package com.jiankunking.elasticsearch.extension.customizer;

import com.jiankunking.elasticsearch.extension.config.EnvionmentVariables;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jiankunking.
 * @date:2018/8/17 10:05
 * @description:
 */
@RestController
@EnableAutoConfiguration
public class CustomPortController implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        //设置自定义监听端口,我这里是从环境变量取的
        factory.setPort(EnvionmentVariables.LISTEN_PORT);
    }
}

个人微信公众号:
这里写图片描述

作者:jiankunking 出处:http://blog.csdn.net/jiankunking

猜你喜欢

转载自blog.csdn.net/xunzaosiyecao/article/details/81806200