Spring Boot/Cloud 端口设置、端口随机、端口自增如何实现?
Spring Boot 、Spring Cloud支持固定端口和随机端口2种方式,但是这两种方式都不利于管理 固定端口容易冲突,特别是在Spring Cloud下,用起来很麻烦 随机端口看似解决了端口冲突的问题,但是天知道它随机到哪个端口去了 本文介绍3种端口设置的方式:固定端口、范围内随机端口、范围内自增长端口 本文均以properties文件的方式描述配置 搜索微信小程序【数字续坚】 在资源中搜索【端口自增】 可下载本文所述源代码
1. 固定端口
固定端口是最常见的方式,在配置文件中配置即可,properties的方式如下
# 默认固定端口方式
server.port=8080
# 或从指定环境变量取值
server.port=${env.name}
2.随机端口
在指定范围内随机使用一个端口,这也是官方支持的一种方式
# 端口从 10000 到 19999 随机取一个
server.port=${random.int[10000,19999]}
3.按照自定义策略选择端口
实现方式是结合“1.固定端口”的从环境变量取值的变种 在搜索微信小程序【数字续坚】 在资源中搜索【端口自增】 可下载本文所述源代码DEMO 实现方式如下:
3.1 配置文件
# 环境变量可以修改,写死为8080或其他也行
server.port=${env.name}
3.2 代码实现逻辑
以下方法为main方法中的 以下代码可以再优化优化哈 DEMO中词此部分代码注释掉了
@SpringBootApplication
public class L3Application {
public static void main ( String [ ] args) {
System . setProperty ( "env.name" , L3Application . getPort ( ) ) ;
SpringApplication . run ( L3Application . class , args) ;
}
public static String getPort ( ) {
return "44455" ;
}
}
4.自增长端口
实际上,实现方式可以参考“3.按照自定义策略选择端口”的方式,把端口逻辑改为递增就行了 本文介绍采用Spring Boot/Cloud的方式实现
4.1 配置文件
# 以下内容和规则都是自定义的
server.port=${randomServerPort.value:8888,9999}
4.2 SpringApplication Main方法中
添加ApplicationListener的ApplicationEnvironmentPreparedEvent
@SpringBootApplication
public class L3Application {
public static void main ( String [ ] args) {
SpringApplication app = new SpringApplication ( L3Application . class ) ;
app. addListeners ( new ApplicationListener < ApplicationEnvironmentPreparedEvent > ( ) {
@Override
public void onApplicationEvent ( ApplicationEnvironmentPreparedEvent event) {
event. getEnvironment ( ) . getPropertySources ( ) . addLast ( new IncrServerPort ( ) ) ;
}
} ) ;
app. run ( args) ;
}
}
4.3 IncrServerPort(PropertySource)实现
import lombok. extern. slf4j. Slf4j ;
import org. springframework. core. env. PropertySource ;
@Slf4j
public class IncrServerPort extends PropertySource < RandomServerPort > {
private final static String RANDOM_SERVER_PORT_PROPERTY_SOURCE_NAME = "randomServerPort" ;
private static final String PREFIX = "randomServerPort." ;
public IncrServerPort ( ) {
super ( RANDOM_SERVER_PORT_PROPERTY_SOURCE_NAME , new RandomServerPort ( ) ) ;
}
@Override
public Object getProperty ( String name) {
if ( ! name. startsWith ( PREFIX ) ) {
return null ;
}
return getRandomServerPortValue ( name. substring ( PREFIX . length ( ) ) ) ;
}
private int getRandomServerPortValue ( String values) {
values = values. replace ( "value:" , "" ) ;
String [ ] ports = values. split ( "," ) ;
try {
if ( ports. length == 0 ) {
return new RandomServerPort ( ) . value ( Integer . parseInt ( ports[ 0 ] ) ) ;
} else {
return new RandomServerPort ( ) . value ( Integer . parseInt ( ports[ 0 ] ) , Integer . parseInt ( ports[ 1 ] ) ) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
return 8080 ;
}
}
}
4.4 详细逻辑实现
@Slf4j
public class RandomServerPort {
public int value ( int start) {
return value ( start, 65535 ) ;
}
public int value ( int start, int end) {
log. info ( "Now checking server port from {} to {}." , start, end) ;
for ( int port = start; port < end; port ++ ) {
if ( ! checkIpPort ( "127.0.0.1" , port) ) {
return port;
} else {
log. error ( "Port {} is not available, now checking the next port." , port) ;
}
}
log. error ( "No available port for this project." ) ;
return start;
}
private boolean checkIpPort ( String ip, int port) {
Socket socket = new Socket ( ) ;
try {
socket. connect ( new InetSocketAddress ( ip, port) , 10 ) ;
return true ;
} catch ( Exception e) {
return false ;
} finally {
if ( socket != null ) {
try {
socket. close ( ) ;
} catch ( IOException e) {
}
}
}
}
}
5.自增长端口
在搜索微信小程序【数字续坚】 在资源中搜索【端口自增】 可下载本文所述源代码demo demo中示例内容比较多,您从main方法看就行了