改造xxl

编程入门 行业动态 更新时间:2024-10-09 01:23:46

改造<a href=https://www.elefans.com/category/jswz/34/1734148.html style=xxl"/>

改造xxl

xxl-job并没有对nacos、zookeeper这一类注册中心进行适配,所以需要进行改造。

改造目标

1.对调度器,需要能注册到nacos上,并且执行器管理里的 机器地址 能使用 lb://serviceName 这种地址

2.对执行器,需要能注册到nacos上,对9999端口进行拦截,让其使用server.port的地址。

注意:nacos能兼做注册中心和配置中心,这里不改造配置中心的功能。

一、改造调度器

1.下载2.4.0源码,Release XXL-JOB v2.4.0,分布式任务调度平台 · xuxueli/xxl-job · GitHub

下载后使用idea打开,是一个多模块的maven项目,找到xxl-job-admin

2.加依赖,需要加springboot版本对应的依赖

<!-- 启用 nacos 服务发现 --><!-- 版本对应2.7.9对应 2021.0.x   --><!-- 2021.0.4.0 对应nacos client 版本 2.0.4 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2021.0.4.0</version></dependency><!-- 如果要启用配置中心,则引入 spring-cloud-starter-alibaba-nacos-config 和 spring-cloud-starter-bootstrap -->

boot和cloud版本对应关系可以查Spring Cloud

2.加配置

spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.application.name=xxl-job-admin

3.写代码

这里代码主要用于改造识别执行器的地址,这要就不用再找执行器的ip了,填写serviceName就可以了。

找到com.xxl.job.admin.core.model.XxlJobGroup#getRegistryList 方法

改写成

public List<String> getRegistryList() {if (addressList!=null && addressList.trim().length()>0) {String newAddressList = addressList;// address 执行器管理填入的机器地址 http://ip:port 这里自定义兼容 lb://servicename地址if(addressList.startsWith("lb:")){String serviceName =addressList.replace("lb://","");DiscoveryClient discoveryClient = SpringContextUtil.getBean(DiscoveryClient.class);List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);List<String> uriList = instances.stream().map(a -> a.getUri().toString()).collect(Collectors.toList());newAddressList = StringUtils.join(uriList, ",");}registryList = new ArrayList<String>(Arrays.asList(newAddressList.split(",")));}return registryList;}

这里用到了工具类SpringContextUtil 

package com.xxl.job.admin.util;import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;@Component
public class SpringContextUtil implements ApplicationContextAware {private static ApplicationContext applicationContext;public static ApplicationContext getApplicationContext() {return applicationContext;}// 下面的这个方法上加了@Override注解,原因是继承ApplicationContextAware接口是必须实现的方法@Overridepublic void setApplicationContext(ApplicationContext applicationContext) {SpringContextUtil.applicationContext = applicationContext;}public static Object getBean(String name) {return applicationContext.getBean(name);}public static Object getBean(String name, Class<?> requiredType) {return applicationContext.getBean(name, requiredType);}public static <T> T getBean(Class<T> clazz) {return applicationContext.getBean(clazz);}public static boolean containsBean(String name) {return applicationContext.containsBean(name);}public static boolean isSingleton(String name) {return applicationContext.isSingleton(name);}public static Class<?> getType(String name) {return applicationContext.getType(name);}public static String[] getAliases(String name) {return applicationContext.getAliases(name);}
}

改造完成后,执行器地址填写如下

二、改造执行器

这里借助一个项目justtoplay/xxl-job-plus,项目地址:xxl-job-plus: xxl-job-plus是xxl-job的增强包,提供对接注册中心能力,支持nacos,springboot,spring cloud,支持监测注册中心xxl-job-admin服务上下线,executor服务上下线,实现executor向xxl-job-admin自动启动、刷新、停止,完美兼容xxl-job-admin

下面是改造spring-boot项目示例,spring-cloud项目改造同理

1.加依赖

这里加xxl-job-plus和boot版nacos依赖

        <!-- xxl-job-core --><dependency><groupId>com.xuxueli</groupId><artifactId>xxl-job-core</artifactId><version>2.4.0</version></dependency><!-- 2.3.1 理论上xxl-job-core的版本也要是2.3.1 实测支持2.4.0 --><!-- 2.3.1 对应 nacos client 版本1.x --><!-- 2.3.1-nacos2 对应 nacos client 版本2.x --><dependency><groupId>com.justtoplay</groupId><artifactId>xxl-job-plus</artifactId><version>2.3.1-nacos2</version></dependency><!-- starter 0.2.12 对应nacos client 版本2.1.0  --><dependency><groupId>com.alibaba.boot</groupId><artifactId>nacos-discovery-spring-boot-starter</artifactId><version>0.2.12</version></dependency>

2.加配置

原配置下的xxl.job删掉,配置改成xxl.job.plus下的配置,addresses和executor.port不用配了

增加nacos.discovery.server-addr配置和spring.application.name配置

改造前

server: port: 8081
logging: config: classpath:logback.xml
xxl: job: admin: addresses: http://127.0.0.1:8080/xxl-job-adminaccessToken: xxl_tokenexecutor: appname: xxl-job-demoaddress: ''ip: ''port: 9999logpath: /data/applogs/xxl-job/jobhandlerlogretentiondays: 30

改造后

server: port: 8082
logging: config: classpath:logback.xml
xxl: job:plus:admin:access-token: default_tokenservice-name: xxl-job-adminexecutor:service-name:
nacos:discovery:server-addr: 127.0.0.1:8848auto-register: truespring:application:name: xxl-job-demo

3.写代码

原来的jobhander不用动,删掉配置类XxlJobConfig,否则会和plus的配置类冲突。

三、改造结果

nacos可以看到这两个服务

服务调用正常,启动多个同服务执行器,无需修改机器地址也能正常调用

更多推荐

改造xxl

本文发布于:2023-12-06 07:08:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1666817.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:xxl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!