美文网首页
SpringBoot 整合 xxl-job (执行器)

SpringBoot 整合 xxl-job (执行器)

作者: 索性流年 | 来源:发表于2020-06-17 18:15 被阅读0次

其他xxl-job 相关内容

pom 引入相关依赖

  • 版本要和注册中心对应
        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
            <version>2.1.2</version>
        </dependency>

.yml 添加配置

server:
  port: 7100
spring:
  application:
    name: xxl-job-member


  datasource:
    url: jdbc:mysql://192.168.112.139:3306/ly_shop_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: root
    password: Ly1234560
    driver-class-name: com.mysql.cj.jdbc.Driver

xxl:
  job:
    admin:
      addresses: http://192.168.112.145:8080/xxl-job-admin
    executor:
      appname: xxl-job-member
      logpath: /data/applogs/xxl-job/jobhandler
      logretentiondays: 30
      port: 9999
      address: http://192.168.112.145:8080/xxl-job-admin
      ip:
    accessToken:

新建配置文件

@Configuration
public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @Value("${xxl.job.executor.appname}")
    private String appname;

    @Value("${xxl.job.executor.address}")
    private String address;

    @Value("${xxl.job.executor.ip}")
    private String ip;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;


    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppName(appname);
        xxlJobSpringExecutor.setAdminAddresses(address);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);

        return xxlJobSpringExecutor;
    }

    /**
     * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
     *
     *      1、引入依赖:
     *          <dependency>
     *             <groupId>org.springframework.cloud</groupId>
     *             <artifactId>spring-cloud-commons</artifactId>
     *             <version>${version}</version>
     *         </dependency>
     *
     *      2、配置文件,或者容器启动变量
     *          spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
     *
     *      3、获取IP
     *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
     */


}

新建 Handler

/**
 * @author 索悻流年
 * @title:
 */
@Component
@Slf4j
public class Indexhandler {
    /**
     * 2、分片广播任务
     */
    @XxlJob("shardingJobHandler")
    public ReturnT<String> shardingJobHandler(String param) throws Exception {

        // 分片参数
        ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
        XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());

        // 业务逻辑
        for (int i = 0; i < shardingVO.getTotal(); i++) {
            if (i == shardingVO.getIndex()) {
                XxlJobLogger.log("第 {} 片, 命中分片开始处理", i);
            } else {
                XxlJobLogger.log("第 {} 片, 忽略", i);
            }
        }

        return ReturnT.SUCCESS;
    }

}

MemberJobApp

/**
 * @author 索悻流年
 * @title:
 */
@SpringBootApplication
public class MemberJobApp {
    public static void main(String[] args) {
        SpringApplication.run(MemberJobApp.class,args);
    }
}

控制台打印成功信息

>>>>>>>>>>> xxl-rpc, provider factory add service success. serviceKey = com.xxl.job.core.biz.ExecutorBiz, serviceBean = class com.xxl.job.core.biz.impl.ExecutorBizImpl

相关文章

网友评论

      本文标题:SpringBoot 整合 xxl-job (执行器)

      本文链接:https://www.haomeiwen.com/subject/yzesxktx.html