Skip to content

Commit

Permalink
supported spring-boot random port(server.port=0)
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Apr 5, 2024
1 parent 51a7d41 commit b07cdf2
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
package cn.ponfee.disjob.admin;

import cn.ponfee.disjob.common.base.IdGenerator;
import cn.ponfee.disjob.common.base.Symbol.Char;
import cn.ponfee.disjob.common.spring.EnableJacksonDateConfigurer;
import cn.ponfee.disjob.core.base.JobConstants;
import cn.ponfee.disjob.core.util.JobUtils;
import cn.ponfee.disjob.core.base.Supervisor;
import cn.ponfee.disjob.id.snowflake.db.DbDistributedSnowflake;
import cn.ponfee.disjob.supervisor.configuration.EnableSupervisor;
import cn.ponfee.disjob.worker.configuration.EnableWorker;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
Expand All @@ -46,12 +44,9 @@
public class DisjobAdminConfiguration {

@Bean
public IdGenerator idGenerator(@Qualifier(SPRING_BEAN_NAME_JDBC_TEMPLATE) JdbcTemplate jdbcTemplate,
@Value("${" + JobConstants.SPRING_WEB_SERVER_PORT + "}") int port,
@Value("${" + JobConstants.DISJOB_BOUND_SERVER_HOST + ":}") String boundHost) {
// serverTag = host:port
String serverTag = JobUtils.getLocalHost(boundHost) + Char.COLON + port;
return new DbDistributedSnowflake(jdbcTemplate, JobConstants.DISJOB_KEY_PREFIX, serverTag);
public IdGenerator idGenerator(Supervisor supervisor,
@Qualifier(SPRING_BEAN_NAME_JDBC_TEMPLATE) JdbcTemplate jdbcTemplate) {
return new DbDistributedSnowflake(jdbcTemplate, JobConstants.DISJOB_KEY_PREFIX, supervisor.serialize());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package cn.ponfee.disjob.common.spring;

import org.apache.commons.io.IOUtils;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.server.WebServer;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;
Expand All @@ -32,11 +34,27 @@
*/
public final class SpringUtils {

/**
* Spring-boot web server port name
*/
public static final String SERVER_PORT = "server.port";

public static Resource getResource(String resourceLocation) throws IOException {
// return new DefaultResourceLoader().getResource(resourceLocation);
URL url = ResourceUtils.getURL(resourceLocation);
byte[] bytes = IOUtils.toByteArray(url);
return new InputStreamResource(new ByteArrayInputStream(bytes));
}

public static int getActualWebServerPort(WebServerApplicationContext webServerApplicationContext) {
Integer port = webServerApplicationContext.getEnvironment().getProperty(SpringUtils.SERVER_PORT, Integer.class);
if (port != null && port > 0) {
return port;
}

WebServer webServer = webServerApplicationContext.getWebServer();
webServer.start();
return webServer.getPort();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ public class JobConstants {
*/
public static final int PROCESS_BATCH_SIZE = 200;

/**
* Spring web server port
*/
public static final String SPRING_WEB_SERVER_PORT = "server.port";

/**
* Disjob configuration key prefix
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public abstract class Server implements Serializable {
protected Server(String host, int port) {
Assert.isTrue(!host.contains(Str.COLON), "Host cannot contains symbol ':'");
// -1 for test case
Assert.isTrue(-1 <= port && port <= 65535, "Port must be range [-1, 65535].");
Assert.isTrue(0 < port && port <= 65535, "Port must be range (0, 65535].");
this.host = host;
this.port = port;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import cn.ponfee.disjob.common.collect.Collects;
import cn.ponfee.disjob.common.exception.Throwables.ThrowingRunnable;
import cn.ponfee.disjob.common.spring.RestTemplateUtils;
import cn.ponfee.disjob.common.spring.SpringUtils;
import cn.ponfee.disjob.common.spring.YamlProperties;
import cn.ponfee.disjob.common.util.ClassUtils;
import cn.ponfee.disjob.common.util.NetUtils;
Expand Down Expand Up @@ -83,7 +84,7 @@ public static void main(String[] args) throws Exception {
}

WorkerProperties workerProperties = props.extract(WorkerProperties.class, WORKER_KEY_PREFIX + ".");
int port = Optional.ofNullable(props.getInt("server.port")).orElse(NetUtils.findAvailablePort(10000));
int port = Optional.ofNullable(props.getInt(SpringUtils.SERVER_PORT)).orElse(NetUtils.findAvailablePort(10000));

String group = props.getString(WORKER_KEY_PREFIX + ".group");
Assert.hasText(group, "Worker group name cannot empty.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
package cn.ponfee.disjob.samples.merged;

import cn.ponfee.disjob.common.base.IdGenerator;
import cn.ponfee.disjob.common.base.Symbol.Char;
import cn.ponfee.disjob.core.base.JobConstants;
import cn.ponfee.disjob.core.util.JobUtils;
import cn.ponfee.disjob.core.base.Supervisor;
import cn.ponfee.disjob.id.snowflake.db.DbDistributedSnowflake;
import cn.ponfee.disjob.samples.common.AbstractSamplesApplication;
import cn.ponfee.disjob.supervisor.configuration.EnableSupervisor;
import cn.ponfee.disjob.worker.configuration.EnableWorker;
//import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
Expand All @@ -53,10 +51,9 @@ public static void main(String[] args) {
}

@Bean
public IdGenerator idGenerator(@Qualifier(SPRING_BEAN_NAME_JDBC_TEMPLATE) JdbcTemplate jdbcTemplate,
@Value("${" + JobConstants.SPRING_WEB_SERVER_PORT + "}") int port,
@Value("${" + JobConstants.DISJOB_BOUND_SERVER_HOST + ":}") String boundHost) {
return new DbDistributedSnowflake(jdbcTemplate, JobConstants.DISJOB_KEY_PREFIX, JobUtils.getLocalHost(boundHost) + Char.COLON + port);
public IdGenerator idGenerator(Supervisor supervisor,
@Qualifier(SPRING_BEAN_NAME_JDBC_TEMPLATE) JdbcTemplate jdbcTemplate) {
return new DbDistributedSnowflake(jdbcTemplate, JobConstants.DISJOB_KEY_PREFIX, supervisor.serialize());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
package cn.ponfee.disjob.samples.supervisor;

import cn.ponfee.disjob.common.base.IdGenerator;
import cn.ponfee.disjob.common.base.Symbol.Char;
import cn.ponfee.disjob.core.base.JobConstants;
import cn.ponfee.disjob.core.util.JobUtils;
import cn.ponfee.disjob.core.base.Supervisor;
import cn.ponfee.disjob.id.snowflake.db.DbDistributedSnowflake;
import cn.ponfee.disjob.samples.common.AbstractSamplesApplication;
import cn.ponfee.disjob.supervisor.configuration.EnableSupervisor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
Expand All @@ -49,10 +47,9 @@ public static void main(String[] args) {
}

@Bean
public IdGenerator idGenerator(@Qualifier(SPRING_BEAN_NAME_JDBC_TEMPLATE) JdbcTemplate jdbcTemplate,
@Value("${" + JobConstants.SPRING_WEB_SERVER_PORT + "}") int port,
@Value("${" + JobConstants.DISJOB_BOUND_SERVER_HOST + ":}") String boundHost) {
return new DbDistributedSnowflake(jdbcTemplate, JobConstants.DISJOB_KEY_PREFIX, JobUtils.getLocalHost(boundHost) + Char.COLON + port);
public IdGenerator idGenerator(Supervisor supervisor,
@Qualifier(SPRING_BEAN_NAME_JDBC_TEMPLATE) JdbcTemplate jdbcTemplate) {
return new DbDistributedSnowflake(jdbcTemplate, JobConstants.DISJOB_KEY_PREFIX, supervisor.serialize());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import cn.ponfee.disjob.common.spring.LocalizedMethodArgumentConfigurer;
import cn.ponfee.disjob.common.spring.RestTemplateUtils;
import cn.ponfee.disjob.common.spring.SpringContextHolder;
import cn.ponfee.disjob.common.spring.SpringUtils;
import cn.ponfee.disjob.common.util.ClassUtils;
import cn.ponfee.disjob.core.base.*;
import cn.ponfee.disjob.core.util.JobUtils;
Expand All @@ -39,15 +40,14 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -97,16 +97,15 @@ class EnableRetryProperties {
class EnableHttpProperties {
}

@ConditionalOnProperty(JobConstants.SPRING_WEB_SERVER_PORT)
class EnableSupervisorConfiguration {

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Order(Ordered.HIGHEST_PRECEDENCE)
@Bean(JobConstants.SPRING_BEAN_NAME_CURRENT_SUPERVISOR)
public Supervisor.Current currentSupervisor(@Value("${" + JobConstants.SPRING_WEB_SERVER_PORT + "}") int port,
public Supervisor.Current currentSupervisor(WebServerApplicationContext webServerApplicationContext,
@Value("${" + JobConstants.DISJOB_BOUND_SERVER_HOST + ":}") String boundHost) {
UnaryOperator<String> workerCtxPath = group -> SchedGroupService.getGroup(group).getWorkerContextPath();
Object[] args = {JobUtils.getLocalHost(boundHost), port, workerCtxPath};
String host = JobUtils.getLocalHost(boundHost);
int port = SpringUtils.getActualWebServerPort(webServerApplicationContext);
Object[] args = {host, port, workerCtxPath};
try {
// inject current supervisor: Supervisor.class.getDeclaredClasses()[0]
return ClassUtils.invoke(Class.forName(Supervisor.Current.class.getName()), "create", args);
Expand Down Expand Up @@ -194,7 +193,6 @@ public DoInLocked scanRunningInstanceLocker(@Qualifier(SPRING_BEAN_NAME_JDBC_TEM
}
}

@ConditionalOnProperty(JobConstants.SPRING_WEB_SERVER_PORT)
class EnableSupervisorAdapter {

@DependsOn(JobConstants.SPRING_BEAN_NAME_CURRENT_SUPERVISOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
// PER_METHOD(默认):每个测试方法都会创建一个新的测试类实例;PER_CLASS:所有测试方法只创建一个测试类的实例;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.MOCK,
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
classes = SpringBootTestApplication.class
)
//@ContextConfiguration(classes = { XXX.class })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
package cn.ponfee.disjob.supervisor.config;

import cn.ponfee.disjob.common.base.IdGenerator;
import cn.ponfee.disjob.common.base.Symbol.Char;
import cn.ponfee.disjob.core.base.JobConstants;
import cn.ponfee.disjob.core.util.JobUtils;
import cn.ponfee.disjob.core.base.Supervisor;
import cn.ponfee.disjob.id.snowflake.db.DbDistributedSnowflake;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
Expand All @@ -38,10 +36,9 @@
public class DisjobConfiguration {

@Bean
public IdGenerator idGenerator(@Qualifier(SPRING_BEAN_NAME_JDBC_TEMPLATE) JdbcTemplate jdbcTemplate,
@Value("${" + JobConstants.SPRING_WEB_SERVER_PORT + "}") int port,
@Value("${" + JobConstants.DISJOB_BOUND_SERVER_HOST + ":}") String boundHost) {
return new DbDistributedSnowflake(jdbcTemplate, JobConstants.DISJOB_KEY_PREFIX, JobUtils.getLocalHost(boundHost) + Char.COLON + port);
public IdGenerator idGenerator(Supervisor supervisor,
@Qualifier(SPRING_BEAN_NAME_JDBC_TEMPLATE) JdbcTemplate jdbcTemplate) {
return new DbDistributedSnowflake(jdbcTemplate, JobConstants.DISJOB_KEY_PREFIX, supervisor.serialize());
}

}
2 changes: 1 addition & 1 deletion disjob-supervisor/src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -1: 单元测试时关闭HTTP端点;0: 自动寻找一个空闲的端口;${random.int[1,65535]}: 随机端口但可能会有端口冲突;
server.port: -1
server.port: 0

# database configuration
disjob.datasource:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import cn.ponfee.disjob.common.spring.LocalizedMethodArgumentConfigurer;
import cn.ponfee.disjob.common.spring.RestTemplateUtils;
import cn.ponfee.disjob.common.spring.SpringContextHolder;
import cn.ponfee.disjob.common.spring.SpringUtils;
import cn.ponfee.disjob.common.util.ClassUtils;
import cn.ponfee.disjob.common.util.UuidUtils;
import cn.ponfee.disjob.core.base.*;
Expand All @@ -30,8 +31,8 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;
Expand Down Expand Up @@ -70,7 +71,6 @@ class EnableRetryProperties {
class EnableHttpProperties {
}

@ConditionalOnProperty(JobConstants.SPRING_WEB_SERVER_PORT)
class EnableWorkerConfiguration {

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
Expand All @@ -80,18 +80,18 @@ public TaskTimingWheel timingWheel(WorkerProperties config) {
return new TaskTimingWheel(config.getTimingWheelTickMs(), config.getTimingWheelRingSize());
}

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Order(Ordered.HIGHEST_PRECEDENCE)
@DependsOn(JobConstants.SPRING_BEAN_NAME_TIMING_WHEEL)
@Bean(JobConstants.SPRING_BEAN_NAME_CURRENT_WORKER)
public Worker.Current currentWorker(@Value("${" + JobConstants.SPRING_WEB_SERVER_PORT + "}") int port,
public Worker.Current currentWorker(WebServerApplicationContext webServerApplicationContext,
@Value("${" + JobConstants.DISJOB_BOUND_SERVER_HOST + ":}") String boundHost,
WorkerProperties config) {
config.check();
String host = JobUtils.getLocalHost(boundHost);
String workerToken = config.getWorkerToken();
String supervisorToken = config.getSupervisorToken();
String supervisorContextPath = config.getSupervisorContextPath();
int port = SpringUtils.getActualWebServerPort(webServerApplicationContext);

Object[] args = {config.getGroup(), UuidUtils.uuid32(), host, port, workerToken, supervisorToken, supervisorContextPath};
try {
// inject current worker: Worker.class.getDeclaredClasses()[0]
Expand Down

0 comments on commit b07cdf2

Please sign in to comment.