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,打上断点

    springboot使用随机端口暴露的问题解决方法_第1张图片

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

    springboot使用随机端口暴露的问题解决方法_第2张图片

  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 {

    /**
     * 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
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;

public class MyApplicationEnvironmentPreparedEventListener implements ApplicationListener {

    @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. 结果如下


    springboot使用随机端口暴露的问题解决方法_第3张图片

点击跳转


springboot使用随机端口暴露的问题解决方法_第4张图片

你可能感兴趣的:(springboot使用随机端口暴露的问题解决方法)