springboot使用随机端口暴露的问题解决方法

随机端口

我们知道为spring boot配置随机端口非常简单,有两种办法:

  1. 设置端口为0
server.port=0
eureka.instance.instance-id=${spring.application.name}-${spring.cloud.client.ipAddress}:${random.int}
  1. 设置端口为随机值
server.port=${random.int[1000,1999]}

问题

我们无论使用上述的哪种办法,都会存在一些问题,有且不限于如下:

  1. eureka界面上的Status无法显示真实的端口
  2. 点击Status列的链接会跳转到错误的端口
  3. 如果使用了LoadBalancerInterceptor会导致连接异常,原因也是导向了错误的端口
  4. ...

解决思路

不难发现问题的根本就是每个地方都会再调用一次random.int生成不同的随机数,如果新生成的随机数被赋予端口意义使用,那么会因为这个数和真实端口并不一致导致发生异常。

random.int 是springboot默认提供的,我想实现一个自己的random.int,只赋值一次即可,往后再调用便返回已生成的值。

解决方法

  1. random.int 是springboot默认提供的,Ctrl+Click 发现实现类是RandomValuePropertySource.

  2. 我尝试把server.port=${random.int[1000,2000]}改成server.port=${randomServerPort.value[1000,2000]}${randomServerPort.value[1000,2000]}是我们要自定义的),运行后得到异常的堆栈信息

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'randomServerPort.value[1000,2000]' in value "${randomServerPort.value[1000,2000]}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.core.env.AbstractPropertyResolver.resolveNestedPlaceholders(AbstractPropertyResolver.java:227) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:66) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:537) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.boot.bind.RelaxedPropertyResolver.getProperty(RelaxedPropertyResolver.java:84) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.bind.RelaxedPropertyResolver.getProperty(RelaxedPropertyResolver.java:74) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ManagementServerPort.getPortProperty(EndpointWebMvcAutoConfiguration.java:409) ~[spring-boot-actuator-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ManagementServerPort.get(EndpointWebMvcAutoConfiguration.java:361) ~[spring-boot-actuator-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$OnManagementMvcCondition.getMatchOutcome(EndpointWebMvcAutoConfiguration.java:345) ~[spring-boot-actuator-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    ... 16 more
  1. 从堆栈信息中找到解析的类PropertyPlaceholderHelper,打上断点

  2. 跳转到PropertySourcesPropertyResolver,找到了真正的解析器propertySources

  3. 我们只要自己写一个PropertySource,然后放进去就可以了。照着RandomValuePropertySource写一个自己的

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;

@Slf4j
public class RandomServerPortPropertySource extends PropertySource<RandomServerPort> {

    /**
     * Name of the random {@link PropertySource}.
     */
    public static final String RANDOM_SERVER_PORT_PROPERTY_SOURCE_NAME = "randomServerPort";

    private static final String PREFIX = "randomServerPort.";

    public RandomServerPortPropertySource(String name) {
        super(name, new RandomServerPort());
    }

    public RandomServerPortPropertySource() {
        this(RANDOM_SERVER_PORT_PROPERTY_SOURCE_NAME);
    }

    public RandomServerPortPropertySource(String name, RandomServerPort source) {
        super(name, source);
    }

    @Override
    public Object getProperty(String name) {
        if (!name.startsWith(PREFIX)) {
            return null;
        }
        if (log.isTraceEnabled()) {
            log.trace("Generating randomServerPort property for '" + name + "'");
        }
        return getRandomServerPortValue(name.substring(PREFIX.length()));
    }

    private Object getRandomServerPortValue(String type) {
        String range = getRange(type, "value");
        if (range != null) {
            return getNextValueInRange(range);
        }
        return null;
    }

    private String getRange(String type, String prefix) {
        if (type.startsWith(prefix)) {
            int startIndex = prefix.length() + 1;
            if (type.length() > startIndex) {
                return type.substring(startIndex, type.length() - 1);
            }
        }
        return null;
    }

    private int getNextValueInRange(String range) {
        String[] tokens = StringUtils.commaDelimitedListToStringArray(range);
        int start = Integer.parseInt(tokens[0]);
        if (tokens.length == 1) {
            return getSource().nextValue(start);
        }
        return getSource().nextValue(start, Integer.parseInt(tokens[1]));
    }
}
import org.apache.commons.lang3.RandomUtils;

/**
 * 随机生成一个端口,并只生成一次
 */
public class RandomServerPort {

    private int serverPort;

    private final int start = 0;
    private final int end = 65535;

    public int nextValue(int start) {
        return nextValue(start, end);
    }

    public int nextValue(int start, int end) {
        start = start < this.start? this.start: start;
        end = end > this.end? this.end: end;

        if (serverPort == 0){
            synchronized (this){
                if (serverPort == 0){
                    serverPort = RandomUtils.nextInt(start, end);
                }
            }
        }
        return serverPort;
    }
}

  1. 这种解析器是解析application.properties的,所以需要很早就加载上去,不难想到用ApplicationListener<ApplicationEnvironmentPreparedEvent>
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;

public class MyApplicationEnvironmentPreparedEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        event.getEnvironment().getPropertySources().addLast(new RandomServerPortPropertySource());
    }
}
  1. 添加监听器
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.addListeners(new MyApplicationEnvironmentPreparedEventListener());
        app.run(args);
    }
}
  1. 配置文件修改一下
server.port=${randomServerPort.value[1000,2000]}
eureka.instance.instance-id=${spring.application.name}-${spring.cloud.client.ipAddress}:${randomServerPort.value[1000,2000]}

# 省略...
  1. 结果如下


点击跳转


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,001评论 19 139
  • 方法1 (数据类型)(最小值+Math.random()*(最大值-最小值+1)) 例: (int)(1+Math...
    GB_speak阅读 41,206评论 2 6
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,779评论 18 399
  • 一、這章大概講了什麼? 談到了什麼是python語言、python起源、與相關的特性 首先,python是一門編程...
    小慷阅读 213评论 0 0
  • 二话不说,先上思维导图。因为发现大家更愿意看图,一目了然,哈哈。上节课讲了救拖的2个方法----时间交替法(番茄工...
    熙沐2017阅读 277评论 0 0