update 优化 ruoyi-common-job 支持通过调度中心服务名注册 xxl-job-admin

2.X
疯狂的狮子li 2 years ago
parent 43a2d0030b
commit a9d589167d

@ -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
# 执行器配置

@ -22,6 +22,12 @@
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- 服务发现组件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<!-- xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
@ -33,6 +39,10 @@
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-core</artifactId>
</dependency>
</dependencies>
</project>

@ -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<ServiceInstance> 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());

@ -17,6 +17,8 @@ public class XxlJobProperties {
private String adminAddresses;
private String adminAppname;
private String accessToken;
private Executor executor;

Loading…
Cancel
Save