Spring-boot整合elastic-job分布式调度解决方案,附相关架构及资料

@ElasticSimpleJob(cron = “* * * * * ?”, jobName = “test123”, shardingTotalCount = 2, jobParameter = “测试参数”, shardingItemParameters = “0=A,1=B”)

@Component

public class MySimpleJob implements com.dangdang.ddframe.job.api.simple.SimpleJob {

@Override

public void execute(ShardingContext shardingContext) {

System.out.println(String.format("------Thread ID: %s, 任务总片数: %s, " +

“当前分片项: %s.当前参数: %s,” +

“当前任务名称: %s.当前任务参数: %s”

,

Thread.currentThread().getId(),

shardingContext.getShardingTotalCount(),

shardingContext.getShardingItem(),

shardingContext.getShardingParameter(),

shardingContext.getJobName(),

shardingContext.getJobParameter()

));

}

}

4. 启动2个不同端口,查看执行结果

执行: java -jar xxx.jar --server.port=8081

------Thread ID: 83, 任务总片数: 2, 当前分片项: 0.当前参数: A,当前任务名称: com.willow.elasticJob.MySimpleJob.当前任务参数:

------Thread ID: 84, 任务总片数: 2, 当前分片项: 1.当前参数: B,当前任务名称: com.willow.elasticJob.MySimpleJob.当前任务参数:

------Thread ID: 89, 任务总片数: 2, 当前分片项: 0.当前参数: A,当前任务名称: com.willow.elasticJob.MySimpleJob.当前任务参数:

------Thread ID: 90, 任务总片数: 2, 当前分片项: 1.当前参数: B,当前任务名称: com.willow.elasticJob.MySimpleJob.当前任务参数:

1.2动态添加elastic-job任务

当前暂未解决的问题: 动态添加的任务只能在添加的机器上运行,平行部署的其他机器上不会运行该任务

在上边配置的基础上添加以下配置:

1. 添加zookeeper配置类,和动态添加方法

import com.dangdang.ddframe.job.api.simple.SimpleJob;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;

import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;

import com.dangdang.ddframe.job.lite.api.JobScheduler;

import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;

import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;

@Configuration

public class ElasticJobConfig {

@Bean(initMethod = “init”)

public ZookeeperRegistryCenter regCenter(@Value(“ e l a t i c j o b . z o o k e e p e r . s e r v e r − l i s t s " ) f i n a l S t r i n g s e r v e r L i s t , @ V a l u e ( " {elaticjob.zookeeper.server-lists}") final String serverList, @Value(" elaticjob.zookeeper.serverlists")finalStringserverList,@Value("{elaticjob.zookeeper.namespace}”) final String namespace) {

return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));

}

@Autowired

private ZookeeperRegistryCenter regCenter;

/**

  • 动态添加

  • @param jobClass

  • @param cron

  • @param shardingTotalCount

  • @param shardingItemParameters

*/

public void addSimpleJobScheduler(final Class<? extends SimpleJob> jobClass,

final String cron,

final int shardingTotalCount,

final String shardingItemParameters){

JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).jobParameter(“job参数”).build();

SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(coreConfig, jobClass.getCanonicalName());

JobScheduler jobScheduler = new JobScheduler(regCenter, LiteJobConfiguration.newBuilder(simpleJobConfig).build());

jobScheduler.init();

}

}

2. 动态添加任务逻辑

import com.willow.elasticJob.MySimpleJob;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class TestController {

@Autowired

private ElasticJobConfig elasticJobConfig;

@RequestMapping(“/addJob”)

public void addJob() {

int shardingTotalCount = 2;

elasticJobConfig.addSimpleJobScheduler(new MySimpleJob().getClass(),“* * * * * ?”,shardingTotalCount,“0=A,1=B”);

}

}

二、Spring 集成 elastic-job

2.1添加POM依赖

com.dangdang

elastic-job-lite-core

2.1.5

com.dangdang

elastic-job-lite-spring

2.1.5

2.2添加配置

server.port=8766

spring.application.name=scheduler-service

zookeeper注册中心

spring.elasticjob.serverList = 192.168.7.108:2181

spring.elasticjob.namespace = elastic-job-lite-springboot

stockJob.cron = 0/5 * * * * ?

stockJob.shardingTotalCount = 2

stockJob.shardingItemParameters = 0=Chengdu0,1=Chengdu1

2.3 添加zookeeper注册中心

package com.willow.config;

import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;

import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

@ConditionalOnExpression(“‘${spring.elasticjob.serverList}’.length() > 0”) //判断是否配置了zookeeper 地址

public class JobRegistryCenterConfig {

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
img

最后

作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料


1711154697372)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-SYmS9yWq-1711154697372)]

最后

作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料

[外链图片转存中…(img-nLZyTP0v-1711154697373)]
[外链图片转存中…(img-ZNrswt2h-1711154697373)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

Spring Boot项目的pom.xml文件中,parent依赖项用于指定项目的父级依赖。引用中提到的spring-boot-starter-parent是Spring Boot官方提供的一个父级依赖,它包含了许多默认的配置和插件,可以方便地创建和构建Spring Boot项目。这个父级依赖主要用于简化项目配置和提供一致性的构建环境。 而spring-boot-parent是Spring Boot老版本中使用的父级依赖,不过在当前的Spring Boot版本中已经不再推荐使用。所以,spring-boot-starter-parent是更常用和推荐的选择,它提供了更多的功能和更新的版本。 总的来说,spring-boot-starter-parent是官方推荐和常用的Spring Boot父级依赖,而spring-boot-parent是老版本中的父级依赖。因此,如果你正在使用较新的Spring Boot版本,建议使用spring-boot-starter-parent来构建你的项目。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [spring-boot-starter-parent-1.5.13.RELEASE.zip](https://download.csdn.net/download/a1135318908/12292638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [elasticjob-spring-boot-starter:elasticjob-spring-boot-starter](https://download.csdn.net/download/weixin_42120563/16210105)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [IDEA创建springboot项目spring-boot-starter-parent、spring-boot-starter-web爆红「简单」](https://blog.csdn.net/m0_62600503/article/details/127854979)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值