Spring Cloud整合XXL-Job的示例代码

 更新时间:2023年05月15日 10:25:22   作者:冷艳无情的小妈  
这篇文章主要介绍了springcloud整合xxl-job的示例代码,主要分为四个过程,本文给大家介绍的非常详细,需要的朋友可以参考下

第一步:

整合pom文件,在Spring Cloud中添加XXL-Job的依赖

<!-- xxl-job-core -->
<dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-core</artifactId>
    <version>2.0.1</version>
</dependency>

第二步:

把XXL-Job中的lohback.xml导入到Spring Cloud 项目中

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="1 seconds">
 
    <contextName>logback</contextName>
<!--    日志文件位置-->
    <property name="log.path" value="D:/ZM/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>
 
</configuration>

第三步:

application.properties整合

server.port=8091
server.tomcat.max-threads=10
spring.application.name=service-order
 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/shop?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&userSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456
 
spring.jpa.properties.show-sql=true
spring.jpa.properties.database=mysql
spring.jpa.properties.database-platform=mysql
spring.jpa.properties.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
 
spring.cloud.nacos.discovery.server-addr=192.168.136.1:8848
#跟控制台交流的端口,随意制定一个未使用的端口即可
spring.cloud.sentinel.transport.port=9999
#指定控制台服务的地址
spring.cloud.sentinel.transport.dashboard=localhost:8080
 
#关闭sentinel的CommonFilter实列化
spring.cloud.sentinel.filter.enabled=false
 
feign.sentinel.enabled=true
 
#通过修改配置来调整Ribbon的负载均衡策略
service-product.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
 
#rocketMQ服务的地址
rocketmq.name-server=192.168.136.111:9876
# 生产者组
rocketmq.producer.group=shop-order
spring.cloud.nacos.config.namespace=public
spring.cloud.nacos.config.group=SEATE_GROUP
spring.cloud.alibaba.seata.tx-service-group=${spring.application.name}
spring.cloud.nacos.config.env=dev
 
logging.config=classpath:logback.xml
 
 
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://192.168.136.111:8080/xxl-job-admin
 
### xxl-job executor address
xxl.job.executor.appname=xxl-job-executor-sample
xxl.job.executor.ip=
xxl.job.executor.port=9999
 
### xxl-job, access token
xxl.job.accessToken=
 
### xxl-job log path
xxl.job.executor.logpath=/opt/module/xxl-job-2.0.1/jobhandler
### xxl-job log retention days
xxl.job.executor.logretentiondays=-1

第四步:

向Spring Cloud中导入XxlJobConfig.class

@Configuration
@ComponentScan(basePackages = "com.xxl.job.executor.service.jobhandler")
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 XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppName(appName);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);

        return xxlJobSpringExecutor;
    }

}

重点:

到此这篇关于Spring Cloud整合XXL-Job的示例代码的文章就介绍到这了,更多相关Spring Cloud整合XXL-Job内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JNDI,JTA和JMS简介

    JNDI,JTA和JMS简介

    这篇文章主要介绍了JNDI,JTA和JMS的相关内容,包括中文释义,概念解释等,需要的朋友可以了解下。
    2017-09-09
  • java实现双向链表的增删改

    java实现双向链表的增删改

    这篇文章主要为大家详细介绍了java实现双向链表的增删改,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Mybatis sql与xml文件读取方法详细分析

    Mybatis sql与xml文件读取方法详细分析

    这篇文章主要介绍了Mybatis sql与xml文件读取方法,在执行一个自定义sql语句时,dao对应的代理对象时如何找到sql,也就是dao的代理对象和sql之间的关联关系是如何建立的
    2023-01-01
  • Java加速读取复制超大文件

    Java加速读取复制超大文件

    这篇文章主要为大家详细介绍了Java加速读取复制超大文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • Dwr3.0纯注解(纯Java Code配置)配置与应用浅析二之前端调用后端

    Dwr3.0纯注解(纯Java Code配置)配置与应用浅析二之前端调用后端

    我们讲到了后端纯Java Code的Dwr3配置,完全去掉了dwr.xml配置文件,但是对于使用注解的类却没有使用包扫描,而是在Servlet初始化参数的classes里面加入了我们的Service组件的声明暴露,对于这个问题需要后面我们再细细研究下这篇文章,主要分析介绍前端怎么直接调用后端
    2016-04-04
  • SpringBoot监控模块Actuator的用法详解

    SpringBoot监控模块Actuator的用法详解

    Spring Boot Actuator 是 Spring Boot 自带的一个功能模块,提供了一组已经开箱即用的生产环境下常用的特性和服务,比如应用程序的健康检查、信息暴露、度量收集、日志记录等,本文将给大家详细SpringBoot监控模块Actuator的用法
    2023-06-06
  • java实现航班信息查询管理系统

    java实现航班信息查询管理系统

    这篇文章主要为大家详细介绍了java实现航班信息查询管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • 使用@Validated 和 BindingResult 遇到的坑及解决

    使用@Validated 和 BindingResult 遇到的坑及解决

    这篇文章主要介绍了使用@Validated 和 BindingResult 遇到的坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • JAVA返回PDF文件流并进行下载的实现方法

    JAVA返回PDF文件流并进行下载的实现方法

    这篇文章主要给大家介绍了关于JAVA返回PDF文件流并进行下载的实现方法,PDF文件流下载是通过HTTP协议将服务器上的PDF文件以流的方式发送给客户端,供客户端保存到本地磁盘或直接在浏览器中打开,需要的朋友可以参考下
    2024-02-02
  • 基于Java设计一个高并发的秒杀系统

    基于Java设计一个高并发的秒杀系统

    这篇文章主要为大家详细介绍了如何基于Java设计一个高并发的秒杀系统,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下
    2023-10-10

最新评论