程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

系列学习分布式任务调度 XXL-JOB 之第 3 篇 —— SpringBoot 整合 XXL-JOB(完结)

发布于2021-05-29 21:28     阅读(1276)     评论(0)     点赞(4)     收藏(4)


 

之前我们使用 XXL-JOB 是使用官方自带的代码模块,我们可以自己将 XXL-JOB 的核心代码整理出来,整合到我们的实际项目中。

比如官网自带的 SpringBoot 项目的 pom.xml 配置,使用的 parent 如图所示,我们在自己的公司里,有自己的 parent 依赖,因此我们需要把核心的依赖提取出来。

 

我们先创建一个项目:

修改 pom.xml 配置

①我们把 XXL-JOB 的 SpringBoot 除 parent 节点以外的内容拷贝过来。即 dependencyManagement 节点和 build 节点。

②找到  XXL-JOB 的 SpringBoot 最外层的 pom.xml 的 parent,点击进入,把  <properties> 节点的内容拷贝过来。

③最后缺少 xxl-job-core 核心包,如图:

我们可以把它的核心包安装到我们本地 Maven 仓库,也可以写固定。比如我使用的版本号是 2.3.0

完整的 pom.xml 配置如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.study.xxljob</groupId>
  7. <artifactId>xxl-job-study</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  12. <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
  13. <maven.compiler.source>1.8</maven.compiler.source>
  14. <maven.compiler.target>1.8</maven.compiler.target>
  15. <maven.test.skip>true</maven.test.skip>
  16. <netty-all.version>4.1.58.Final</netty-all.version>
  17. <gson.version>2.8.6</gson.version>
  18. <spring.version>5.3.3</spring.version>
  19. <spring-boot.version>2.4.2</spring-boot.version>
  20. <mybatis-spring-boot-starter.version>2.1.4</mybatis-spring-boot-starter.version>
  21. <mysql-connector-java.version>8.0.23</mysql-connector-java.version>
  22. <slf4j-api.version>1.7.30</slf4j-api.version>
  23. <junit.version>5.7.1</junit.version>
  24. <javax.annotation-api.version>1.3.2</javax.annotation-api.version>
  25. <groovy.version>3.0.7</groovy.version>
  26. <maven-source-plugin.version>3.2.1</maven-source-plugin.version>
  27. <maven-javadoc-plugin.version>3.2.0</maven-javadoc-plugin.version>
  28. <maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
  29. <maven-war-plugin.version>3.3.1</maven-war-plugin.version>
  30. </properties>
  31. <dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <!-- Import dependency management from Spring Boot (依赖管理:继承一些默认的依赖,工程需要依赖的jar包的管理,申明其他dependency的时候就不需要version) -->
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-parent</artifactId>
  37. <version>${spring-boot.version}</version>
  38. <type>pom</type>
  39. <scope>import</scope>
  40. </dependency>
  41. </dependencies>
  42. </dependencyManagement>
  43. <dependencies>
  44. <!-- spring-boot-starter-web (spring-webmvc + tomcat) -->
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-web</artifactId>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter-test</artifactId>
  52. <scope>test</scope>
  53. </dependency>
  54. <!-- xxl-job-core -->
  55. <dependency>
  56. <groupId>com.xuxueli</groupId>
  57. <artifactId>xxl-job-core</artifactId>
  58. <version>2.3.0</version>
  59. </dependency>
  60. </dependencies>
  61. <build>
  62. <plugins>
  63. <!-- spring-boot-maven-plugin (提供了直接运行项目的插件:如果是通过parent方式继承spring-boot-starter-parent则不用此插件) -->
  64. <plugin>
  65. <groupId>org.springframework.boot</groupId>
  66. <artifactId>spring-boot-maven-plugin</artifactId>
  67. <version>${spring-boot.version}</version>
  68. <executions>
  69. <execution>
  70. <goals>
  71. <goal>repackage</goal>
  72. </goals>
  73. </execution>
  74. </executions>
  75. </plugin>
  76. </plugins>
  77. </build>
  78. </project>

我们的项目结构如图:

 

 

配置文件:XxlJobConfig,完全是复制 XXL-JOB 的配置文件,完整代码:

  1. package com.study.config;
  2. import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. /**
  9. * xxl-job config
  10. *
  11. * @author xuxueli 2017-04-28
  12. */
  13. @Configuration
  14. public class XxlJobConfig {
  15. private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
  16. @Value("${xxl.job.admin.addresses}")
  17. private String adminAddresses;
  18. @Value("${xxl.job.accessToken}")
  19. private String accessToken;
  20. @Value("${xxl.job.executor.appname}")
  21. private String appname;
  22. @Value("${xxl.job.executor.address}")
  23. private String address;
  24. @Value("${xxl.job.executor.ip}")
  25. private String ip;
  26. @Value("${xxl.job.executor.port}")
  27. private int port;
  28. @Value("${xxl.job.executor.logpath}")
  29. private String logPath;
  30. @Value("${xxl.job.executor.logretentiondays}")
  31. private int logRetentionDays;
  32. @Bean
  33. public XxlJobSpringExecutor xxlJobExecutor() {
  34. logger.info(">>>>>>>>>>> xxl-job config init.");
  35. XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
  36. xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
  37. xxlJobSpringExecutor.setAppname(appname);
  38. xxlJobSpringExecutor.setAddress(address);
  39. xxlJobSpringExecutor.setIp(ip);
  40. xxlJobSpringExecutor.setPort(port);
  41. xxlJobSpringExecutor.setAccessToken(accessToken);
  42. xxlJobSpringExecutor.setLogPath(logPath);
  43. xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
  44. return xxlJobSpringExecutor;
  45. }
  46. }

JobController 代码如下(只是为了验证我们的微服务是否启动成功):

  1. package com.study.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * @author biandan
  6. * @description
  7. * @signature 让天下没有难写的代码
  8. * @create 2021-05-26 下午 5:27
  9. */
  10. @RestController
  11. public class JobController {
  12. @RequestMapping(value = "/index")
  13. public String test() {
  14. return "Welcome! 如果看到此信息,说明服务启动成功!";
  15. }
  16. }

任务类:MyJob,代码如下:

  1. package com.study.job;
  2. import com.xxl.job.core.context.XxlJobHelper;
  3. import com.xxl.job.core.handler.annotation.XxlJob;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Component;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. /**
  9. * @author biandan
  10. * @description
  11. * @signature 让天下没有难写的代码
  12. * @create 2021-05-26 下午 5:33
  13. */
  14. @Component
  15. public class MyJob{
  16. @Value("${xxl.job.executor.port}")
  17. private int port;
  18. SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  19. /**
  20. * 我的个人测试 handler
  21. *
  22. * @throws Exception
  23. */
  24. @XxlJob("jobStudyHandler")
  25. public void jobStudyHandler(){
  26. String param = XxlJobHelper.getJobParam();
  27. String msg = "执行器端口号:" + port + ",当前时间:" + SDF.format(new Date()) + ",获取到的参数:" + param;
  28. XxlJobHelper.log(msg);
  29. System.out.println(msg);
  30. }
  31. }

说明:获取参数使用 XxlJobHelper.getJobParam() 方法获取。

启动类:StudyJobApplication

  1. package com.study;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * @author biandan
  6. * @description
  7. * @signature 让天下没有难写的代码
  8. * @create 2021-05-26 下午 5:25
  9. */
  10. @SpringBootApplication
  11. public class StudyJobApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(StudyJobApplication.class,args);
  14. }
  15. }

配置信息 application.properties:

  1. # web port
  2. server.port=9001
  3. # no web
  4. #spring.main.web-environment=false
  5. # log config
  6. logging.config=classpath:logback.xml
  7. ### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
  8. # 调度中心地址
  9. xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
  10. ### xxl-job, access token
  11. xxl.job.accessToken=
  12. ### xxl-job executor appname
  13. # 执行器应用的名字
  14. # xxl.job.executor.appname=xxl-job-executor-sample
  15. xxl.job.executor.appname=job-study-appname
  16. ### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
  17. xxl.job.executor.address=
  18. ### xxl-job executor server-info
  19. xxl.job.executor.ip=
  20. xxl.job.executor.port=9090
  21. ### xxl-job executor log-path
  22. xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
  23. ### xxl-job executor log-retention-days
  24. xxl.job.executor.logretentiondays=30

logback.xml 文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration debug="false" scan="true" scanPeriod="1 seconds">
  3. <contextName>logback</contextName>
  4. <property name="log.path" value="/data/applogs/xxl-job/xxl-job-executor-sample-springboot.log"/>
  5. <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
  6. <encoder>
  7. <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
  8. </encoder>
  9. </appender>
  10. <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
  11. <file>${log.path}</file>
  12. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  13. <fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern>
  14. </rollingPolicy>
  15. <encoder>
  16. <pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n
  17. </pattern>
  18. </encoder>
  19. </appender>
  20. <root level="info">
  21. <appender-ref ref="console"/>
  22. <appender-ref ref="file"/>
  23. </root>
  24. </configuration>

OK,我们启动我们的微服务,访问地址:http://127.0.0.1:9001/index

 

然后我们启动调度中心微服务,并配置我们的执行器信息、任务信息。

1、启动调度中心微服务

2、配置执行器信息

3、创建任务

 

4、启动任务,可以看到执行器的微服务不断的打印如下信息:(拿到了参数,我们可以根据一定的规则转成我们想要的格式,比如 JSON 格式、数组格式等)

OK,SpringBoot 整合 XXL-JOB 搞定。我们可以移植到我们的实际项目中。

本篇博客源代码:https://pan.baidu.com/s/1SFvX76UQ3JVoQtg6oCi5Sw   提取码:edk3

原文链接:https://blog.csdn.net/BiandanLoveyou/article/details/117294100



所属网站分类: 技术文章 > 博客

作者:我很伤感

链接:http://www.javaheidong.com/blog/article/207441/693b93d2e0c3793b242f/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

4 0
收藏该文
已收藏

评论内容:(最多支持255个字符)