From a9d589167daf96c273e3e3235f8f87640f13a406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Mon, 5 Sep 2022 14:10:53 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20ruoyi-common-job?= =?UTF-8?q?=20=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87=E8=B0=83=E5=BA=A6?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=E6=9C=8D=E5=8A=A1=E5=90=8D=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=20xxl-job-admin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/dev/ruoyi-job.yml | 4 +++- ruoyi-common/ruoyi-common-job/pom.xml | 10 +++++++++ .../ruoyi/common/job/config/XxlJobConfig.java | 21 ++++++++++++++++++- .../config/properties/XxlJobProperties.java | 2 ++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/config/dev/ruoyi-job.yml b/config/dev/ruoyi-job.yml index 987b423a..ed0bf037 100644 --- a/config/dev/ruoyi-job.yml +++ b/config/dev/ruoyi-job.yml @@ -16,7 +16,9 @@ xxl: # 执行器开关 enabled: true # 调度中心地址:如调度中心集群部署存在多个地址则用逗号分隔。 - admin-addresses: http://localhost:9900 + # admin-addresses: http://localhost:9900 + # 调度中心应用名 通过服务名连接调度中心(启用admin-appname会导致admin-addresses不生效) + admin-appname: ruoyi-xxl-job-admin # 执行器通讯TOKEN:非空时启用 access-token: xxl-job # 执行器配置 diff --git a/ruoyi-common/ruoyi-common-job/pom.xml b/ruoyi-common/ruoyi-common-job/pom.xml index e63930e6..fee125a6 100644 --- a/ruoyi-common/ruoyi-common-job/pom.xml +++ b/ruoyi-common/ruoyi-common-job/pom.xml @@ -22,6 +22,12 @@ spring-boot-autoconfigure + + + org.springframework.cloud + spring-cloud-commons + + com.xuxueli @@ -33,6 +39,10 @@ lombok + + com.ruoyi + ruoyi-common-core + diff --git a/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/XxlJobConfig.java b/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/XxlJobConfig.java index 94e7e4e8..5dfc2ff9 100644 --- a/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/XxlJobConfig.java +++ b/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/XxlJobConfig.java @@ -1,14 +1,21 @@ package com.ruoyi.common.job.config; +import cn.hutool.core.collection.CollUtil; +import com.ruoyi.common.core.utils.StreamUtils; import com.ruoyi.common.job.config.properties.XxlJobProperties; import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.context.annotation.Bean; +import java.util.List; + /** * xxl-job config * @@ -23,11 +30,23 @@ public class XxlJobConfig { private final XxlJobProperties xxlJobProperties; + private final DiscoveryClient discoveryClient; + @Bean public XxlJobSpringExecutor xxlJobExecutor() { log.info(">>>>>>>>>>> xxl-job config init."); XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); - xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminAddresses()); + if (StringUtils.isNotBlank(xxlJobProperties.getAdminAppname())) { + List instances = discoveryClient.getInstances(xxlJobProperties.getAdminAppname()); + if (CollUtil.isEmpty(instances)) { + throw new RuntimeException("调度中心不存在!"); + } + String serverList = StreamUtils.join(instances, instance -> + String.format("http://%s:%s", instance.getHost(), instance.getPort())); + xxlJobSpringExecutor.setAdminAddresses(serverList); + } else { + xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminAddresses()); + } xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken()); XxlJobProperties.Executor executor = xxlJobProperties.getExecutor(); xxlJobSpringExecutor.setAppname(executor.getAppname()); diff --git a/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/properties/XxlJobProperties.java b/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/properties/XxlJobProperties.java index fc89a2f3..47d0ce05 100644 --- a/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/properties/XxlJobProperties.java +++ b/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/properties/XxlJobProperties.java @@ -17,6 +17,8 @@ public class XxlJobProperties { private String adminAddresses; + private String adminAppname; + private String accessToken; private Executor executor;