diff --git a/pom.xml b/pom.xml index 99022ea4..484a8834 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ powerjob-common powerjob-client powerjob-worker-samples + powerjob-worker-starter powerjob-worker-agent diff --git a/powerjob-worker-starter/pom.xml b/powerjob-worker-starter/pom.xml new file mode 100644 index 00000000..8f281119 --- /dev/null +++ b/powerjob-worker-starter/pom.xml @@ -0,0 +1,45 @@ + + + + powerjob + com.github.kfcfans + 1.0.0 + + + 4.0.0 + powerjob-worker-starter + 3.2.0 + jar + + + 3.2.0 + 2.2.6.RELEASE + + + + + + com.github.kfcfans + powerjob-worker + ${powerjob.worker.version} + + + + org.springframework.boot + spring-boot-starter + ${springboot.version} + compile + + + + org.springframework.boot + spring-boot-starter-test + ${springboot.version} + test + + + + + \ No newline at end of file diff --git a/powerjob-worker-starter/src/main/java/com/github/kfcfans/powerjob/worker/autoconfigure/PowerJobAutoConfiguration.java b/powerjob-worker-starter/src/main/java/com/github/kfcfans/powerjob/worker/autoconfigure/PowerJobAutoConfiguration.java new file mode 100644 index 00000000..14391bcb --- /dev/null +++ b/powerjob-worker-starter/src/main/java/com/github/kfcfans/powerjob/worker/autoconfigure/PowerJobAutoConfiguration.java @@ -0,0 +1,52 @@ +package com.github.kfcfans.powerjob.worker.autoconfigure; + +import com.github.kfcfans.powerjob.worker.OhMyWorker; +import com.github.kfcfans.powerjob.worker.common.OhMyConfig; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.StringUtils; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * PowerJob 自动装配 + * + * @author songyinyin + * @date 2020/7/26 16:37 + */ +@Configuration +@EnableConfigurationProperties(PowerJobProperties.class) +public class PowerJobAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public OhMyWorker initPowerJob(PowerJobProperties properties) { + + // 服务器HTTP地址(端口号为 server.port,而不是 ActorSystem port),请勿添加任何前缀(http://) + List serverAddress = StringUtils.hasText(properties.getServerAddress()) + ? Arrays.asList(properties.getServerAddress().split(",")) + : Collections.singletonList("127.0.0.1:7700"); + + // 1. 创建配置文件 + OhMyConfig config = new OhMyConfig(); + // 可以不显示设置,默认值 27777 + config.setPort(properties.getAkkaPort()); + // appName,需要提前在控制台注册,否则启动报错 + config.setAppName(properties.getAppName()); + config.setServerAddress(serverAddress); + // 如果没有大型 Map/MapReduce 的需求,建议使用内存来加速计算 + // 有大型 Map/MapReduce 需求,可能产生大量子任务(Task)的场景,请使用 DISK,否则妥妥的 OutOfMemeory + config.setStoreStrategy(properties.getStoreStrategy()); + // 启动测试模式,true情况下,不再尝试连接 server 并验证appName + config.setEnableTestMode(properties.isEnableTestMode()); + + // 2. 创建 Worker 对象,设置配置文件 + OhMyWorker ohMyWorker = new OhMyWorker(); + ohMyWorker.setConfig(config); + return ohMyWorker; + } +} diff --git a/powerjob-worker-starter/src/main/java/com/github/kfcfans/powerjob/worker/autoconfigure/PowerJobProperties.java b/powerjob-worker-starter/src/main/java/com/github/kfcfans/powerjob/worker/autoconfigure/PowerJobProperties.java new file mode 100644 index 00000000..220f260d --- /dev/null +++ b/powerjob-worker-starter/src/main/java/com/github/kfcfans/powerjob/worker/autoconfigure/PowerJobProperties.java @@ -0,0 +1,44 @@ +package com.github.kfcfans.powerjob.worker.autoconfigure; + +import com.github.kfcfans.powerjob.common.RemoteConstant; +import com.github.kfcfans.powerjob.worker.common.constants.StoreStrategy; +import com.github.kfcfans.powerjob.worker.core.processor.ProcessResult; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * PowerJob 配置项 + * + * @author songyinyin + * @date 2020/7/26 16:37 + */ +@Data +@ConfigurationProperties(prefix = "powerjob") +public class PowerJobProperties { + /** + * 应用名称,需要提前在控制台注册,否则启动报错 + */ + private String appName; + /** + * 启动 akka 端口 + */ + private int akkaPort = RemoteConstant.DEFAULT_WORKER_PORT; + /** + * 调度服务器地址,ip:port 或 域名,多个用英文逗号分隔 + */ + private String serverAddress = "127.0.0.1:7700"; + /** + * 本地持久化方式,默认使用磁盘 + */ + private StoreStrategy storeStrategy = StoreStrategy.DISK; + /** + * 最大返回值长度,超过会被截断 + * {@link ProcessResult}#msg 的最大长度 + */ + private int maxResultLength = 8096; + /** + * 启动测试模式,true情况下,不再尝试连接 server 并验证appName。 + * true -> 用于本地写单元测试调试; false -> 默认值,标准模式 + */ + private boolean enableTestMode = false; +} diff --git a/powerjob-worker-starter/src/main/resources/META-INF/spring-configuration-metadata.json b/powerjob-worker-starter/src/main/resources/META-INF/spring-configuration-metadata.json new file mode 100644 index 00000000..6a012148 --- /dev/null +++ b/powerjob-worker-starter/src/main/resources/META-INF/spring-configuration-metadata.json @@ -0,0 +1,51 @@ +{ + "groups": [ + { + "name": "powerjob", + "type": "com.github.kfcfans.powerjob.worker.autoconfigure.PowerJobProperties", + "sourceType": "com.github.kfcfans.powerjob.worker.autoconfigure.PowerJobProperties" + } + ], + "properties": [ + { + "name": "powerjob.app-name", + "type": "java.lang.String", + "description": "应用名称,需要提前在控制台注册,否则启动报错", + "sourceType": "com.github.kfcfans.powerjob.worker.autoconfigure.PowerJobProperties" + }, + { + "name": "powerjob.max-result-length", + "type": "java.lang.Integer", + "description": "最大返回值长度,超过会被截断 {@link ProcessResult}#msg 的最大长度", + "sourceType": "com.github.kfcfans.powerjob.worker.autoconfigure.PowerJobProperties", + "defaultValue": 8096 + }, + { + "name": "powerjob.akka-port", + "type": "java.lang.Integer", + "description": "启动 akka 端口", + "sourceType": "com.github.kfcfans.powerjob.worker.autoconfigure.PowerJobProperties" + }, + { + "name": "powerjob.server-address", + "type": "java.lang.String", + "description": "调度服务器地址,ip:port 或 域名,多个用英文逗号分隔", + "sourceType": "com.github.kfcfans.powerjob.worker.autoconfigure.PowerJobProperties", + "defaultValue": "127.0.0.1:7700" + }, + { + "name": "powerjob.store-strategy", + "type": "com.github.kfcfans.powerjob.worker.common.constants.StoreStrategy", + "description": "本地持久化方式,默认使用磁盘", + "sourceType": "com.github.kfcfans.powerjob.worker.autoconfigure.PowerJobProperties" + }, + { + "name": "powerjob.enable-test-mode", + "type": "java.lang.Boolean", + "description": "启动测试模式,true情况下,不再尝试连接 server 并验证appName。true -> 用于本地写单元测试调试; false -> 默认值,标准模式", + "sourceType": "com.github.kfcfans.powerjob.worker.autoconfigure.PowerJobProperties", + "defaultValue": false + } + ], + "hints": [] +} \ No newline at end of file diff --git a/powerjob-worker-starter/src/main/resources/META-INF/spring.factories b/powerjob-worker-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..19eea33f --- /dev/null +++ b/powerjob-worker-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.github.kfcfans.powerjob.worker.autoconfigure.PowerJobAutoConfiguration \ No newline at end of file diff --git a/powerjob-worker-starter/src/test/java/com/github/kfcfans/powerjob/worker/autoconfigure/PowerJobAutoConfigurationTest.java b/powerjob-worker-starter/src/test/java/com/github/kfcfans/powerjob/worker/autoconfigure/PowerJobAutoConfigurationTest.java new file mode 100644 index 00000000..dd90d395 --- /dev/null +++ b/powerjob-worker-starter/src/test/java/com/github/kfcfans/powerjob/worker/autoconfigure/PowerJobAutoConfigurationTest.java @@ -0,0 +1,22 @@ +package com.github.kfcfans.powerjob.worker.autoconfigure; + +import com.github.kfcfans.powerjob.worker.OhMyWorker; +import org.junit.Assert; +import org.junit.jupiter.api.Test; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableAutoConfiguration +class PowerJobAutoConfigurationTest { + + @Test + void testAutoConfiguration() { + ConfigurableApplicationContext run = SpringApplication.run(PowerJobAutoConfigurationTest.class); + OhMyWorker worker = run.getBean(OhMyWorker.class); + Assert.assertNotNull(worker); + } + +} diff --git a/powerjob-worker-starter/src/test/resources/application.properties b/powerjob-worker-starter/src/test/resources/application.properties new file mode 100644 index 00000000..fbcb42fa --- /dev/null +++ b/powerjob-worker-starter/src/test/resources/application.properties @@ -0,0 +1,2 @@ + +powerjob.enable-test-mode=true \ No newline at end of file