<!-- SpringBoot依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- Feign依赖包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- XXL-JOB依赖包 -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.1</version>
</dependency>
@Slf4j
@Service("xxlJobAdminLoginService")
public class XxlJobAdminLoginServiceImpl implements XxlJobAdminLoginService {
private static final String LOGIN_PATH = "login";
@Value("${xxl.job.admin.addresses:}")
private String adminAddresses;
@Value("${xxl.job.admin.username:}")
private String username;
@Value("${xxl.job.admin.password:}")
private String password;
private final Cache<String, String> cache = new MemoryCache();
@Override
public String readLoginCookie() throws BusinessException {
String cookie = cache.get(LOGIN_PATH);
return StrUtil.isBlank(cookie) ? readCookieFromLogin() : cookie;
}
private String readCookieFromLogin() {
Map<String, String> params = new HashMap<>(2);
params.put("userName", username);
params.put("password", password);
Map<String, Object> respAndHeaders = HttpClientUtils.sendPostThenRespAndHeaders(
adminAddresses + LOGIN_PATH, params, "UTF-8");
log.info("response {}", respAndHeaders);
Map<String, String> headers = (Map<String, String>) respAndHeaders.get("headers");
String setCookie = headers.get("Set-Cookie");
log.info("set cookie {}", setCookie);
cache.put(LOGIN_PATH, setCookie, System.currentTimeMillis() + 3600000);
return setCookie;
}
}
@FeignClient(name = "xxl-job-admin", qualifiers = "xxlJobAdminJobGroupService", url = "${xxl.job.admin.addresses}",
configuration = XxlJobAdminFeignConfiguration.class, fallbackFactory = XxlJobAdminJobGroupServiceFallbackFactory.class)
public interface XxlJobAdminJobGroupService {
/**
* save
* @param jobGroupParams
* @return
*/
@PostMapping(value = "jobgroup/save", consumes = {"application/x-www-form-urlencoded"})
public XxlJobAdminResult<String> save(@RequestBody JobGroupParams jobGroupParams);
/**
* update
* @param jobGroupParams
* @return
*/
@PostMapping(value = "jobgroup/update", consumes = {"application/x-www-form-urlencoded"})
public XxlJobAdminResult<String> update(@RequestBody JobGroupParams jobGroupParams);
/**
* read pagination
* @param jobGroupPageParams
* @return
*/
@PostMapping(value = "jobgroup/pageList", consumes = {"application/x-www-form-urlencoded"})
public XxlJobAdminPageResult<JobGroup> readPagination(@RequestBody JobGroupPageParams jobGroupPageParams);
}
@Slf4j
@Component("xxlJobAdminJobGroupServiceFallbackFactory")
class XxlJobAdminJobGroupServiceFallbackFactory implements FallbackFactory<XxlJobAdminJobGroupService> {
@Override
public XxlJobAdminJobGroupService create(Throwable throwable) {
log.error(throwable.getMessage(), throwable);
return new XxlJobAdminJobGroupService() {
@Override
public XxlJobAdminResult<String> save(JobGroupParams jobGroupParams) {
return null;
}
@Override
public XxlJobAdminResult<String> update(JobGroupParams jobGroupParams) {
return null;
}
@Override
public XxlJobAdminPageResult<JobGroup> readPagination(JobGroupPageParams jobGroupPageParams) {
return null;
}
};
}
}
@FeignClient(name = "xxl-job-admin", qualifiers = "xxlJobAdminJobInfoService", url = "${xxl.job.admin.addresses}",
configuration = XxlJobAdminFeignConfiguration.class, fallbackFactory = XxlJobAdminJobInfoServiceFallbackFactory.class)
public interface XxlJobAdminJobInfoService {
/**
* save
* @param jobInfoParams
* @return
*/
@PostMapping(value = "jobinfo/add", consumes = {"application/x-www-form-urlencoded"})
public XxlJobAdminResult<String> save(@RequestBody JobInfoParams jobInfoParams);
/**
* update
* @param jobInfoParams
* @return
*/
@PostMapping(value = "jobinfo/update", consumes = {"application/x-www-form-urlencoded"})
public XxlJobAdminResult<String> update(@RequestBody JobInfoParams jobInfoParams);
/**
* start
* @param id
* @return
*/
@PostMapping(value = "jobinfo/start", consumes = {"application/x-www-form-urlencoded"})
public XxlJobAdminResult<String> start(@RequestParam("id") Integer id);
/**
* stop
* @param id
* @return
*/
@PostMapping(value = "jobinfo/stop", consumes = {"application/x-www-form-urlencoded"})
public XxlJobAdminResult<String> stop(@RequestParam("id") Integer id);
/**
* read pagination
* @param jobInfoPageParams
* @return
*/
@PostMapping(value = "jobinfo/pageList", consumes = {"application/x-www-form-urlencoded"})
public XxlJobAdminPageResult<JobInfo> readPagination(@RequestBody JobInfoPageParams jobInfoPageParams);
}
@Slf4j
@Component("xxlJobAdminJobInfoServiceFallbackFactory")
class XxlJobAdminJobInfoServiceFallbackFactory implements FallbackFactory<XxlJobAdminJobInfoService> {
@Override
public XxlJobAdminJobInfoService create(Throwable throwable) {
log.error(throwable.getMessage(), throwable);
return new XxlJobAdminJobInfoService() {
@Override
public XxlJobAdminResult<String> save(JobInfoParams jobInfoParams) {
return null;
}
@Override
public XxlJobAdminResult<String> update(JobInfoParams jobInfoParams) {
return null;
}
@Override
public XxlJobAdminResult<String> start(Integer id) {
return null;
}
@Override
public XxlJobAdminResult<String> stop(Integer id) {
return null;
}
@Override
public XxlJobAdminPageResult<JobInfo> readPagination(JobInfoPageParams jobInfoPageParams) {
return null;
}
};
}
}
@RequiredArgsConstructor
public class FeignRequestInterceptor implements RequestInterceptor {
private static final String HEADER_COOKIE = "Cookie";
private final XxlJobAdminLoginService xxlJobAdminLoginService;
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header(HEADER_COOKIE, xxlJobAdminLoginService.readLoginCookie());
}
}
public class XxlJobAdminFeignConfiguration {
@Bean
public Retryer xxlJobAdminFeignRetryer() {
return new Retryer.Default(100, SECONDS.toMillis(1), 1);
}
@Bean
public RequestInterceptor xxlJobAdminFeignRequestInterceptor(XxlJobAdminLoginService xxlJobAdminLoginService) {
return new FeignRequestInterceptor(xxlJobAdminLoginService);
}
}
xxl:
job:
enable: true
admin:
# 调度中心部署跟地址
addresses: http://192.168.1.1:28080/xxl-job-admin/
username: admin
password: 123456
executor:
# 执行器AppName
app-name: xxxxxx-platform
# 执行器名称
name: XXXXXX平台
# 服务注册地址,优先使用该配置作为注册地址 为空时使用内嵌服务 ”IP:PORT“ 作为注册地址 从而更灵活的支持容器类型执行器动态IP和动态映射端口问题
address:
# 执行器IP[选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP ,该IP不会绑定Host仅作为通讯实用;地址信息用于执行器注册和调度中心请求并触发任务
ip:
# 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9099
port: 9099
# 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径
log-path: handler_log
# 执行器日志保存天数 [选填] :值大于3时生效,启用执行器Log文件定期清理功能,否则不生效
log-retention-days: -1
# 执行器通讯TOKEN
access-token: xxxxxxxx
# 执行器地址类型:0=自动注册、1=手动录入,默认为0
address-type: 0
# 执行器地址列表:在执行器地址类型为1的情况下,手动录入执行器地址列表,多地址逗号分隔
address-list: http://192.168.1.1:9099
自此,便可以基于代码的方式新建、修改任务。