赞
踩
# 默认固定端口方式
server.port=8080
# 或从指定环境变量取值
server.port=${env.name}
# 端口从 10000 到 19999 随机取一个
server.port=${random.int[10000,19999]}
# 环境变量可以修改,写死为8080或其他也行
server.port=${env.name}
@SpringBootApplication
public class L3Application {
public static void main(String[] args) {
//设置对应的环境变量,如果没有特别设置,就写server.port
System.setProperty("env.name", L3Application.getPort());
SpringApplication.run(L3Application.class,args);
}
public static String getPort(){
//TODO 按照你的逻辑返回端口即可
//某个条件
// if(CONDITION){
// //某个端口
// return "5555";
// }
return "44455";
}
}
# 以下内容和规则都是自定义的
server.port=${randomServerPort.value:8888,9999}
@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) {
//IncrServerPort见下一节内容
event.getEnvironment().getPropertySources().addLast(new IncrServerPort());
}
});
app.run(args);
}
}
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()));
}
/**
* 本方法实际上即为,自增端口的一种逻辑:
* 通过轮询的方式检查本地端口是否可用,不可用则+1再试
* */
private int getRandomServerPortValue(String values){
values = values.replace("value:","");
String[] ports = values.split(",");
try{
if(ports.length == 0){
//详细逻辑实现代码:ports[0] 到 65535
return new RandomServerPort().value(Integer.parseInt(ports[0]));
}else{
//详细逻辑实现代码:ports[0] 到 ports[1]
return new RandomServerPort().value(Integer.parseInt(ports[0]),Integer.parseInt(ports[1]));
}
}catch (Exception e){
e.printStackTrace();
return 8080;
}
}
}
@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;
}
/**
* 检测Ip和端口是否可用
*
* @param ip
* @param port
* @return
*/
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) {
}
}
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。