spring boot整合xxl-job任务调度平台实现分配定时任务集群

进入许雪里官网-点击项目-选择XXL-JOB|分布式任务调度平台,下载该模板

 

二/在idea中导入xxl-job-master项目,找到数据库脚本本导入到本地的数据库中

 

三/更改xxl-job-admin项目的DataSource

 

 

项目启动成功后访问地址:http://127.0.0.1:8080/xxl-job-admin/toLogin

 

账号 admin 密码:123456

也可以自己手动在数据库xxl-job-registry表中增加

登录成功后的界面:

四/相关代码

 

新建定时任务父类cyb-shop-service-job

 

再创建定时会员定时任务 cyb-shop-service-member-job

 

在父类pom文件中加入以下坐标

 

<dependencies>

    <dependency>
        <groupId>com.xuxueli</groupId>
        <artifactId>xxl-job-core</artifactId>
        <version>2.1.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.1</version>
    </dependency>
</dependencies>

 

新增application.properties

# web port
server.port=8083

# log config
logging.config=classpath:logback.xml


### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin

### xxl-job executor address
xxl.job.executor.appname=mayikt-member-executor-job
xxl.job.executor.ip=
xxl.job.executor.port=9991

### xxl-job, access token
xxl.job.accessToken=

### xxl-job log path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job log retention days
xxl.job.executor.logretentiondays=30

新增配置文件bootstrap.yml

spring:
  cloud:
    nacos:
      discovery:
        ##服务的注册
        server-addr: 127.0.0.1:8848
        ###  nacos 配置中心
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
  datasource:
    url: jdbc:mysql://localhost:3306/cyb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
  redis:
    host: 192.168.1.230
    port: 6369
  #    password: 123456
  application:
    name: cyb-member-job


cyb:
  member:
    job:
      WeChatActivitiePageSize: 2

新增logback.xml

    <contextName>logback</contextName>
    <property name="log.path" value="/data/applogs/xxl-job/xxl-job-executor-sample-springboot.log"/>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n
            </pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="console"/>
        <appender-ref ref="file"/>
    </root>

任务调用平台节目操作

1/新建执行器

 

 

 

新增任务

 

后端代码

 

XxlJobConfig

package com.cyb.job.jobhandler;

import com.alibaba.fastjson.JSONObject;
import com.cyb.job.entitydo.UserDo;
import com.cyb.job.mapper.UserMapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.util.ShardingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.nio.cs.US_ASCII;

import java.util.List;

/**
 * 定时任务类
 */
@Component
@Slf4j
public class WeChatActivitieJob {
    @Autowired
    private UserMapper userMapper;
    @Value("${cyb.member.job.WeChatActivitiePageSize}")
    private Integer pageSize;

    /**
     * @param param
     * @return
     * @XxlJob 该任务的名称id
     */
    @XxlJob("weChatActivitieJobHandler")
    public ReturnT<String> weChatActivitieJobHandler(String param) {
        ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
        int index = shardingVO.getIndex();
        int startIndex = ((index + 1) - 1) * pageSize;
        log.info(">>>定时任务开始出发<<<param:{},index:{}", param, index);
        List<UserDo> userDos = userMapper.selectByOpenIdNotIsNull(startIndex, pageSize);
        log.info("userDos:" + JSONObject.toJSONString(userDos));
        return ReturnT.SUCCESS;
    }

    /**
     * 假设现在我们的每个执行执行2条  pageSize
     * index =0  第index=0+1  satart =(1-1)*pageSize, pageSize
     *
     * 1.执行器发送一半如果失败的情况下 5000-1000
     * 执行宁愿配置多一台也能少配  人工补偿
     * 2.在方法上@XxlJob即可
     * 3. 每个执行器发送10万 100万
     *
     */

}

UserDo

package com.cyb.job.entitydo;

import lombok.Data;

import java.util.Date;


@Data
public class UserDo {

    /**
     * userid
     */

    private Long userId;
    /**
     * 手机号码
     */

    private String mobile;
    /**
     * 邮箱
     */

    private String email;
    /**
     * 密码
     */

    private String passWord;
    /**
     * 用户名称
     */

    private String userName;
    /**
     * 性别 0 男 1女
     */

    private char sex;
    /**
     * 年龄
     */

    private Long age;
    /**
     * 注册时间
     */

    private Date createTime;
    /**
     * 修改时间
     */

    private Date updateTime;
    /**
     * 账号是否可以用 1 正常 0冻结
     */

    private char isAvalible;
    /**
     * 用户头像
     */

    private String picImg;
    /**
     * 用户关联 QQ 开放ID
     */

    private String qqOpenId;
    /**
     * 用户关联 微信 开放ID
     */
    private String wxOpenId;


}

WeChatActivitieJob

package com.cyb.job.jobhandler;

import com.alibaba.fastjson.JSONObject;
import com.cyb.job.entitydo.UserDo;
import com.cyb.job.mapper.UserMapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.util.ShardingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.nio.cs.US_ASCII;

import java.util.List;

/**
 * 定时任务类
 */
@Component
@Slf4j
public class WeChatActivitieJob {
    @Autowired
    private UserMapper userMapper;
    @Value("${cyb.member.job.WeChatActivitiePageSize}")
    private Integer pageSize;

    /**
     * @param param
     * @return
     * @XxlJob 该任务的名称id
     */
    @XxlJob("weChatActivitieJobHandler")
    public ReturnT<String> weChatActivitieJobHandler(String param) {
        ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
        int index = shardingVO.getIndex();
        int startIndex = ((index + 1) - 1) * pageSize;
        log.info(">>>定时任务开始出发<<<param:{},index:{}", param, index);
        List<UserDo> userDos = userMapper.selectByOpenIdNotIsNull(startIndex, pageSize);
        log.info("userDos:" + JSONObject.toJSONString(userDos));
        return ReturnT.SUCCESS;
    }

    /**
     * 假设现在我们的每个执行执行2条  pageSize
     * index =0  第index=0+1  satart =(1-1)*pageSize, pageSize
     *
     * 1.执行器发送一半如果失败的情况下 5000-1000
     * 执行宁愿配置多一台也能少配  人工补偿
     * 2.在方法上@XxlJob即可
     * 3. 每个执行器发送10万 100万
     *
     */

}

UserMapper

package com.cyb.job.mapper;



import com.cyb.job.entitydo.UserDo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;



public interface UserMapper {

    @Update("\n" +
            "update meite_user set WX_OPENID=#{wxOpenId}  where user_id=#{userId};")
    int updateUseOpenId(@Param("userId") Long userId, @Param("wxOpenId") String wxOpenId);

    @Select("SELECT USER_ID AS USERID ,MOBILE AS MOBILE ,password as password\n" +
            ",user_name as username ,user_name as username,sex as sex \n" +
            ",age as age ,create_time as createtime,IS_AVALIBLE as ISAVALIBLE\n" +
            ",\n" +
            "pic_img  as picimg,qq_openid as qqopenid ,wx_openid as wxopenid\n" +
            "\n" +
            "from meite_user where trim(WX_OPENID)!=''  limit #{index},#{pageSize}; ")
    List<UserDo> selectByOpenIdNotIsNull(@Param("index") Integer index, @Param("pageSize") Integer pageSize);


}

 

posted @ 2020-03-29 02:19  陈远波  阅读(3275)  评论(0编辑  收藏  举报