提交 597c24dc 编写于 作者: 如梦技术's avatar 如梦技术 🐛

开源 mica 2.0 mica-jobs 模块.

上级 63e7172a
......@@ -7,6 +7,7 @@ ext {
swaggerAnnotationsVersion = "1.5.22"
okhttpVersion = "3.14.4"
jsoupVersion = "1.13.1"
xxlJobVersion = "2.2.0"
lombokVersion = "1.18.12"
findbugsVersion = "3.0.2"
}
......
......@@ -21,6 +21,7 @@ dependencyManagement {
dependency "net.dreamlu:mica-laytpl:${VERSION}"
dependency "net.dreamlu:mica-swagger:${VERSION}"
dependency "net.dreamlu:mica-captcha:${VERSION}"
dependency "net.dreamlu:mica-jobs:${VERSION}"
// commons
dependency "com.google.code.findbugs:jsr305:${findbugsVersion}"
dependency "io.swagger:swagger-annotations:${swaggerAnnotationsVersion}"
......
# xxl-job stater
xxl-job 官方地址:https://github.com/xuxueli/xxl-job
xxl-job 官方文档:https://www.xuxueli.com/xxl-job/
## 添加依赖
### maven
```xml
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-jobs</artifactId>
<version>${version}</version>
</dependency>
```
### gradle
```groovy
compile("net.dreamlu:mica-jobs:${version}")
```
## 配置
| 配置项 | 默认值 | 说明 |
| ----- | ------ | ------ |
| xxl.job.enabled | true | 是否启用分布式调度任务,默认:开启 |
| xxl.job.admin.address | | 调度中心地址,如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;支持配置,{@code lb:// + ${service_name}} 从注册中心动态获取地址 |
| xxl.job.admin.access-token | | 与调度中心交互的accessToken,非空时启用 |
| xxl.job.admin.context-path | | job admin 的 context-path |
| xxl.job.executor.app-name | | 执行器名称,执行器心跳注册分组依据;为空则关闭自动注册 |
| xxl.job.executor.ip | | 执行器 IP,默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务" |
| xxl.job.executor.log-path | | 执行器日志位置 |
| xxl.job.executor.log-retention-days | -1 | 执行器日志保留天数,默认值:-1,值大于3时生效,启用执行器Log文件定期清理功能,否则不生效 |
| xxl.job.executor.port | -1 | 执行器端口,小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口 |
dependencies {
api "com.xuxueli:xxl-job-core:${xxlJobVersion}"
api "org.springframework.cloud:spring-cloud-commons"
implementation "org.springframework.boot:spring-boot-autoconfigure"
compileOnly "org.springframework.cloud:spring-cloud-context"
annotationProcessor "net.dreamlu:mica-auto:${micaAutoVersion}"
}
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dreamlu.mica.jobs.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.jobs.properties.XxlJobClientProperties;
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.cloud.commons.util.InetUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.net.URI;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static net.dreamlu.mica.jobs.properties.XxlJobClientProperties.XxlJobAdminProps;
import static net.dreamlu.mica.jobs.properties.XxlJobClientProperties.XxlJobExecutorProps;
/**
* xxl-job client config
*
* @author L.cm
*/
@Slf4j
@Configuration
@EnableConfigurationProperties(XxlJobClientProperties.class)
public class XxlJobClientAutoConfiguration {
private static final String LB_PREFIX = "lb://";
@Bean
@ConditionalOnProperty(prefix = "xxl.job", value = "enabled", havingValue = "true")
public XxlJobSpringExecutor xxlJobExecutor(XxlJobClientProperties properties,
DiscoveryClient discoveryClient,
Environment environment,
InetUtils inetUtils) {
log.info(">>>>>>>>>>> xxl-job client config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
XxlJobAdminProps admin = properties.getAdmin();
xxlJobSpringExecutor.setAccessToken(admin.getAccessToken());
// 获取 admin 管理端的地址
xxlJobSpringExecutor.setAdminAddresses(getAdminServiceUrl(discoveryClient, admin));
XxlJobExecutorProps executor = properties.getExecutor();
// 微服务 服务名
String serviceId = getAppName(environment);
xxlJobSpringExecutor.setAppname(getExecutorName(executor, serviceId));
String ipAddress = executor.getIp();
if (StringUtils.isEmpty(ipAddress)) {
ipAddress = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
}
xxlJobSpringExecutor.setIp(ipAddress);
xxlJobSpringExecutor.setPort(executor.getPort());
xxlJobSpringExecutor.setLogPath(getLogPath(executor, environment, serviceId));
xxlJobSpringExecutor.setLogRetentionDays(executor.getLogRetentionDays());
return xxlJobSpringExecutor;
}
private static String getAdminServiceUrl(DiscoveryClient discoveryClient, XxlJobAdminProps admin) {
String addresses = admin.getAddress();
Assert.hasText(addresses, "xxl-job admin addresses is empty.");
// 处理 context-path,trim,补充前缀
String contextPath = Optional.ofNullable(admin.getContextPath())
.map(String::trim)
.map(path -> path.startsWith("/") ? path : "/" + path)
.orElse("");
return StringUtils.commaDelimitedListToSet(addresses).stream()
.flatMap(address -> {
// lb://xxx
if (address.startsWith(LB_PREFIX)) {
return discoveryClient.getInstances(address.substring(LB_PREFIX.length()))
.stream()
.map(ServiceInstance::getUri)
.map(URI::toString);
}
// ip:port,补充 http:// 前缀
if (!address.startsWith("http")) {
address = "http://" + address;
}
// 其他 protocol://ip:port
return Stream.of(address);
}).map(url -> url + contextPath)
.collect(Collectors.joining(","));
}
private static String getExecutorName(XxlJobExecutorProps executor, String serviceId) {
String appName = executor.getAppName();
if (StringUtils.hasText(appName)) {
return appName;
}
return serviceId;
}
private static String getLogPath(XxlJobExecutorProps executor, Environment environment, String serviceId) {
String logPath = executor.getLogPath();
if (!StringUtils.hasText(logPath)) {
logPath = environment.getProperty("LOGGING_PATH", "logs")
.concat("/").concat(serviceId).concat("/jobs");
}
return logPath;
}
private static String getAppName(Environment environment) {
return environment.getProperty("spring.application.name", "");
}
}
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dreamlu.mica.jobs.properties;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
/**
* <p>
* xxl-job 配置类
* </p>
*
* @author yangkai.shen
* @author L.cm
*/
@Getter
@Setter
@RefreshScope
@ConfigurationProperties(prefix = "xxl.job")
public class XxlJobClientProperties {
/**
* 是否启用分布式调度任务,默认:开启
*/
private boolean enabled = true;
/**
* 调度中心配置
*/
private XxlJobAdminProps admin = new XxlJobAdminProps();
/**
* 执行器配置
*/
private XxlJobExecutorProps executor = new XxlJobExecutorProps();
@Getter
@Setter
public static class XxlJobAdminProps {
/**
* 调度中心地址,如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;支持配置,{@code lb:// + ${service_name}} 从注册中心动态获取地址
*/
private String address;
/**
* 与调度中心交互的accessToken,非空时启用
*/
private String accessToken;
/**
* job admin 的 context-path
*/
private String contextPath;
}
@Getter
@Setter
public static class XxlJobExecutorProps {
/**
* 执行器名称,执行器心跳注册分组依据;为空则关闭自动注册
*/
private String appName;
/**
* 执行器 IP,默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务"
*/
private String ip;
/**
* 执行器端口,小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口
*/
private int port = -1;
/**
* 执行器日志位置
*/
private String logPath;
/**
* 执行器日志保留天数,默认值:-1,值大于3时生效,启用执行器Log文件定期清理功能,否则不生效
*/
private int logRetentionDays = -1;
}
}
......@@ -6,4 +6,4 @@ include "mica-spider"
include "mica-laytpl"
include "mica-swagger"
include "mica-captcha"
include "mica-jobs"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册