xxl-job定時任務配置應用及添加到springboot項目中實現(xiàn)動態(tài)API調(diào)用
簡介
XXL-JOB是一個分布式任務調(diào)度平臺,其核心設計目標是開發(fā)迅速、學習簡單、輕量級、易擴展?,F(xiàn)已開放源代碼并接入多家公司線上產(chǎn)品線,開箱即用。
本篇文章主要是對xuxueli的xxl-job做一個簡單的配置,以及將其添加到自己已有的項目中進行api調(diào)用。
xxl-job的安裝與配置
一、xxl-job安裝
1、首先訪問Gitee克隆/下載xxl-job的源碼
https://gitee.com/xuxueli0323/xxl-job
2、下載完解壓,在源碼文件夾xxl-job-master\xxl-job-master\doc\db里先導入SQL文件到數(shù)據(jù)庫
3、在IDEA中打開下載好的項目文件,使用maven進行編譯即可
xxl-job-admin:調(diào)度中心
xxl-job-core:公共依賴
xxl-job-executor-samples:執(zhí)行器Sample示例(選擇合適的版本執(zhí)行器,可直接使用,也可以參考其并將現(xiàn)有項目改造成執(zhí)行器)
xxl-job-executor-sample-springboot:Springboot版本,通過Springboot管理執(zhí)行器,推薦這種方式;
xxl-job-executor-sample-frameless:無框架版本;
二、xxl-job的配置
首先配置調(diào)度中心
### web server.port=9527 server.servlet.context-path=/xxl-job-admin ### actuator management.server.servlet.context-path=/actuator management.health.mail.enabled=false ### resources spring.mvc.servlet.load-on-startup=0 spring.mvc.static-path-pattern=/static/** spring.resources.static-locations=classpath:/static/ ### freemarker spring.freemarker.templateLoaderPath=classpath:/templates/ spring.freemarker.suffix=.ftl spring.freemarker.charset=UTF-8 spring.freemarker.request-context-attribute=request spring.freemarker.settings.number_format=0.########## ### mybatis mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml #mybatis.type-aliases-package=com.xxl.job.admin.core.model ### xxl-job, datasource spring.datasource.url=jdbc:mysql://192.168.3.140:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ### datasource-pool spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.minimum-idle=10 spring.datasource.hikari.maximum-pool-size=30 spring.datasource.hikari.auto-commit=true spring.datasource.hikari.idle-timeout=30000 spring.datasource.hikari.pool-name=HikariCP spring.datasource.hikari.max-lifetime=900000 spring.datasource.hikari.connection-timeout=10000 spring.datasource.hikari.connection-test-query=SELECT 1 spring.datasource.hikari.validation-timeout=1000 ### xxl-job, email spring.mail.host=smtp.qq.com spring.mail.port=25 spring.mail.username=xxx@qq.com spring.mail.from=xxx@qq.com spring.mail.password=xxx spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory ### xxl-job, access token xxl.job.accessToken=default_token ### xxl-job, i18n (default is zh_CN, and you can choose "zh_CN", "zh_TC" and "en") xxl.job.i18n=zh_CN ## xxl-job, triggerpool max size xxl.job.triggerpool.fast.max=200 xxl.job.triggerpool.slow.max=100 ### xxl-job, log retention days xxl.job.logretentiondays=30
這塊我設置了任務調(diào)度中心的admin端口是9527(端口號可以自定義),spring.datasource.url地址填你導入sql文件的數(shù)據(jù)庫。
接著設置xxl-job-executor-sample-springboot的配置文件
# web port server.port=9528 # no web #spring.main.web-environment=false # 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:9527/xxl-job-admin ### xxl-job, access token xxl.job.accessToken=default_token ### xxl-job executor appname xxl.job.executor.appname=test ### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null xxl.job.executor.address= ### xxl-job executor server-info xxl.job.executor.ip= xxl.job.executor.port=9999 ### xxl-job executor log-path xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler ### xxl-job executor log-retention-days xxl.job.executor.logretentiondays=30
xxl-job-executor-sample-springboot端口號為了方便區(qū)分我設置了9528(端口號不能與其他端口重復)
到此xxl-job的基本配置就設置好了
三、啟動項目
分別啟動xxl-job-admin調(diào)度中心和xxl-job-executor-sample-springboot的啟動命令
打開鏈接http://localhost:9527/xxl-job-admin/
執(zhí)行器管理新增執(zhí)行器
配置執(zhí)行器時,有幾個注意的地方
①執(zhí)行器的AppName必須與xxl.job.executor.appname保持一致
②這里新增執(zhí)行器建議手動錄入,自動注冊可能存在斷開連接的問題,機器地址后的端口號要與xxl.job.executor.port端口號保持一致,且要加前綴https://
配置好執(zhí)行器就可以編寫定時任務了
在這里使用xuxueli提供定時任務示例,具體位置如下:
注解中的值就是任務配置中的JobHandler
回到任務調(diào)度中心,打開任務管理,執(zhí)行器選擇剛才新建的測試執(zhí)行器,點擊右側新增任務
這里以BEAN運行模式為例,JobHandler中的值填方法上注解@XxlJob的值,Cron表達式可以根據(jù)需求生成,下面貼一個cron的鏈接,可以根據(jù)需要去編寫獲取,這里以兩秒鐘執(zhí)行一次為例,配置完后點擊保存。
回到任務管理,找到配置好的任務,點擊操作----啟動
定時任務就開始啟動了,具體效果可以打開IDEA的9528服務看效果
以此xxl-job的安裝與配置結束,現(xiàn)在開始引入到我們的springboot項目中
xxl-job添加到已有項目
一、引入POM文件
<!-- xxl-job-core --> <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.3.0</version> </dependency>
二、編寫XxlJob的配置文件
因為上面用過properties了,這里展示一下使用YML來進行配置
xxl: job: accessToken: default_token admin: addresses: http://192.168.3.27:9527/xxl-job-admin executor: appname: fnTest address: ip: port: 9999 logpath: D:\Work\logs logretentiondays: 30
三、編寫config配置類
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * XXL-JOB配置類 * * @author 單程車票 */ @Slf4j @Configuration public class XxlJobConfig { @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() { log.info(">>>>>>>>>>> xxl-job config init."); XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); xxlJobSpringExecutor.setAdminAddresses(adminAddresses); xxlJobSpringExecutor.setAppname(appname); xxlJobSpringExecutor.setAddress(address); xxlJobSpringExecutor.setIp(ip); xxlJobSpringExecutor.setPort(port); xxlJobSpringExecutor.setAccessToken(accessToken); xxlJobSpringExecutor.setLogPath(logPath); xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); return xxlJobSpringExecutor; } }
四、配置任務調(diào)度
還是需要打開xxl-job-admin任務調(diào)度中心服務
這里還是以上文說的9527為例
同樣新增執(zhí)行器
五、編寫自己的定時任務
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.annotation.XxlJob; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.Date; @Component @Slf4j public class TestJob { @XxlJob("FnHandler") public ReturnT<String> wechatActivityJobHandler(String param) { log.info(">>>定時任務開始出發(fā)<<<param:{}", param); log.info(">>>定時任務開始出發(fā)<<<param:{}", param); log.info(">>>定時任務開始出發(fā)<<<param:{}", param); log.info(">>>定時任務開始出發(fā)<<<param:{}", param); log.info(">>>定時任務開始出發(fā)<<<param:{}", param); log.info(">>>定時任務開始出發(fā)<<<param:{}", param); log.info(">>>定時任務開始出發(fā)<<<param:{}", param); return ReturnT.SUCCESS; } }
同樣再回到任務調(diào)度中心,任務管理---新增任務
JobHandler填寫注解@XxlJob中的值,Cron按自身要求編寫,保存
執(zhí)行定時任務
至此,xxl-job已經(jīng)添加到項目中了,可以去任務調(diào)度中心查看任務日志
這篇文章就先分享到這里了,感謝瀏覽到最后,有問題的話,歡迎大家留言。
到此這篇關于xxl-job定時任務配置應用以及添加到自己已有的springboot項目中實現(xiàn)動態(tài)API調(diào)用的文章就介紹到這了,更多相關xxl-job定時任務配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Spring Data JPA動態(tài)條件查詢的寫法
本篇文章主要介紹了Spring Data JPA動態(tài)條件查詢的寫法 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06SpringBoot如何利用Twilio?Verify發(fā)送驗證碼短信
Twilio提供了一個名為?Twilio?Verify?的服務,專門用于處理驗證碼的發(fā)送和驗證,下面我們就來看看如何使用Twilio?Verify實現(xiàn)發(fā)送驗證碼短信吧2025-03-03Java使用poi組件導出Excel格式數(shù)據(jù)
這篇文章主要介紹了Java使用poi組件導出Excel格式數(shù)據(jù),需要的朋友可以參考下2020-02-02