《微服务之SpringBoot与XXL-JOB集成》

前言:

        XXL-JOB是一个轻量级的分布式任务调度框架,其设计的核心目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。

正文:

    一。新建工程-在pom.xml引入依赖

        
        
            com.xuxueli
            xxl-job-core
        

    二。配置XXL-JOB的数据源 ---application.properties

# web port
server.port=8081
# log config
logging.config=classpath:logback-spring.xml
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://ip:端口/xxl-job-admin
### xxl-job executor address
xxl.job.executor.appname=integral-zentao-job
xxl.job.executor.ip=IP地址(这里指的是XXL-JOB部署的机器,如果需要在本地测试,将此IP换成本地IP)
xxl.job.executor.port=9999
### 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=-1

 

    三。配置日志文件 




    
    
        
            
                %d - %msg%n
            
        
    
    
        
            
                %msg%n
            
        
    

    
    
        
            ERROR
        
        
            
                %msg%n
            
        
        
        
            
            D:/integral/zentao-provider-error.%d.log
        
    

    
    
        
            
            ERROR
            DENY
            ACCEPT
        
        
            
                %msg%n
            
        
        
        
            
            D:/integral/zentao-provider-info.%d.log
        
    


    
        
    

    
    
        
        
        
    



    四。创建XXL-JOB配置类

package com.dmsdbj.integral.zentao.config;

import com.xxl.job.core.executor.XxlJobExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * xxl-job config
 *
 * @author xuxueli 2017-04-28
 */
@Configuration
@ComponentScan(basePackages = "com.dmsdbj.integral.zentao.provider.job")
public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

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

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

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

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

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

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

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


    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
        xxlJobExecutor.setAdminAddresses(adminAddresses);
        xxlJobExecutor.setAppName(appName);
        xxlJobExecutor.setIp(ip);
        xxlJobExecutor.setPort(port);
        xxlJobExecutor.setAccessToken(accessToken);
        xxlJobExecutor.setLogPath(logPath);
        xxlJobExecutor.setLogRetentionDays(logRetentionDays);

        return xxlJobExecutor;
    }

}

    五。创建JobHandler

package com.dmsdbj.integral.zentao.provider.job;

import com.dmsdbj.integral.zentao.provider.service.ZtUserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;

@JobHandler(value = "SyncUserJob")
@Component
@Slf4j
public class SyncUserJob extends IJobHandler implements Serializable {

    @Resource
    private ZtUserService ztUserService;

    @Override
    public ReturnT execute(String s){
        XxlJobLogger.log("SyncUserJob--start");
        ztUserService.syncUser();
        XxlJobLogger.log("SyncUserJob--end");
        return ReturnT.SUCCESS;
    }
}

    六。访问任务调度中心管理平台 

         1.增加定时器 


        2.配置任务管理器 


 

结语:

        用自己的实力,索要自己的未来

《微服务之SpringBoot与XXL-JOB集成》_第1张图片

你可能感兴趣的:(JAVA)