欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Boot 自定义端口等

程序员文章站 2022-05-02 11:41:13
...

一、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);
    }
}

个人微信公众号:
Spring Boot 自定义端口等

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