Spring Cloud整合XXL-Job的示例代码
作者:冷艳无情的小妈 时间:2021-06-09 06:01:59
第一步:
整合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;
}
}
重点:
来源:https://blog.csdn.net/wuhahaq/article/details/130583300
标签:Spring,Cloud,整合,XXL-Job
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现支付宝支付密码输入界面
2023-06-20 14:57:15
![](https://img.aspxhome.com/file/2023/8/83508_0s.gif)
浅谈redis key值内存消耗以及性能影响
2022-11-09 20:35:17
聊聊Java Double相加出现的怪事
2023-07-22 22:46:39
浅谈Maven的安装及修改为阿里云下载依赖
2023-08-05 08:30:37
MyBatis-Plus框架整合详细方法
2022-12-14 12:00:46
![](https://img.aspxhome.com/file/2023/6/73046_0s.png)
java显示当前运行时的参数(java运行参数)
2023-09-07 10:03:22
Java实现JDK动态代理的原理详解
2021-09-19 08:17:43
![](https://img.aspxhome.com/file/2023/1/69291_0s.png)
一文搞懂Spring循环依赖的原理
2023-07-24 19:03:24
Java 十大排序算法之插入排序刨析
2021-12-07 02:48:54
![](https://img.aspxhome.com/file/2023/5/68135_0s.jpg)
Java使用备忘录模式实现过关类游戏功能详解
2022-11-30 08:52:51
![](https://img.aspxhome.com/file/2023/2/60782_0s.png)
详解spring boot引入外部jar包的坑
2021-07-29 10:49:49
![](https://img.aspxhome.com/file/2023/4/61314_0s.jpg)
Java 类与对象超基础讲解
2023-06-12 00:03:22
![](https://img.aspxhome.com/file/2023/8/70938_0s.png)
formfile文件上传使用示例
2023-06-23 03:41:58
Java与Oracle实现事务(JDBC事务)实例详解
2023-02-12 12:47:22
Java基于深度优先遍历的随机迷宫生成算法
2022-06-01 22:18:50
![](https://img.aspxhome.com/file/2023/7/62977_0s.jpg)
C++实现LeetCode(131.拆分回文串)
2023-07-24 09:58:42
SpringBoot整合canal实现数据同步的示例代码
2022-05-07 19:51:24
![](https://img.aspxhome.com/file/2023/1/61661_0s.png)
SpringBoot AOP控制Redis自动缓存和更新的示例
2023-08-31 17:34:37
Java 高并发五:JDK并发包1详细介绍
2022-11-29 08:56:26
![](https://img.aspxhome.com/file/2023/4/62814_0s.gif)
一文带你学会规则引擎Drools的应用
2022-04-03 08:40:59
![](https://img.aspxhome.com/file/2023/0/66670_0s.png)