springboot集成elastic

Easter79
• 阅读 483

1、首先添加引用

com.dangdang elastic-job-lite-spring 2.1.5

2、添加配置文件信息。

server: port: 8081 elasticjob: regCenter: serverLists: 127.0.0.1:2181 namespace: elasticjob-lite-springboot job: simpleJob: cron: 0/5 * * * * ? shardingTotalCount: 3 shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou

3、配置zk的注册中心。

package com.uwith.elasticjob.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("'${elasticjob.regCenter.serverLists}'.length() > 0") public class RegistryCenterConfig {

@Bean(initMethod \= "init")
public ZookeeperRegistryCenter regCenter(@Value("${elasticjob.regCenter.serverLists}") final String serverList, @Value("${elasticjob.regCenter.namespace}") final String namespace) {
    return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));

} }

4、配置一个简单job任务。

package com.uwith.elasticjob.config; 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 com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler; import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; 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;

@Configuration public class SimpleJobConfig {

@Autowired

private ZookeeperRegistryCenter regCenter; @Bean(initMethod = "init") public JobScheduler simpleJobScheduler(final SimpleJob simpleJob, @Value("${elasticjob.job.simpleJob.cron}") final String cron, @Value("${elasticjob.job.simpleJob.shardingTotalCount}") final int shardingTotalCount, @Value("${elasticjob.job.simpleJob.shardingItemParameters}") final String shardingItemParameters) { return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters)); }

private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
    // 定义作业核心配置

JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount). shardingItemParameters(shardingItemParameters).build(); // 定义SIMPLE类型配置 SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, jobClass.getCanonicalName()); // 定义Lite作业根配置 LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build(); return simpleJobRootConfig; } }

5、添加一个任务的执行类。

package com.uwith.elasticjob.job; import com.dangdang.ddframe.job.api.ShardingContext; import com.dangdang.ddframe.job.api.simple.SimpleJob; import org.springframework.stereotype.Component; @Component public class MyJob implements SimpleJob {

@Override

public void execute(ShardingContext shardingContext) { System.out.println(shardingContext.getShardingItem()); System.out.println(shardingContext.getShardingParameter()); } }

如果多个job执行的话,首先需要添加一个job类,也就是第5步操作。然后添加一个job配置类,也就是第4步操作。需要注意:

1、simpleJobScheduler方法得换一个,因为注入时相同会有问题。

2、如果多个job,simpleJobScheduler方法的参数(SimpleJob)simpleJob就不能随便写了,得写具体的job类,来区分完成注入。

zookeeper可参考: https://my.oschina.net/uwith/blog/3196640

elastic-job官网: https://shardingsphere.apache.org/elasticjob/current/cn/overview/

点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
springboot整合netty实现TCP服务端
1、导入依赖_<!https://mvnrepository.com/artifact/io.netty/nettyall_<dependency\<groupId\io.netty</groupId\<artifactId\nettyall</artifact
Wesley13 Wesley13
2年前
jdk动态代理和cglib动态代理底层实现原理详细解析(cglib动态代理篇)
  代理模式是一种很常见的模式,本文主要分析cglib动态代理的过程1\.举例使用cglib代理需要引入两个包,maven的话包引入如下<!https://mvnrepository.com/artifact/cglib/cglib<dependency
Easter79 Easter79
2年前
springboot备份mysql后发送邮件并删除备份文件,支持win和Linux
首先加入springboot的邮箱依赖<!邮箱依赖<!https://mvnrepository.com/artifact/org.springframework.boot/springbootstartermail<dependency<groupIdorg.springf
Wesley13 Wesley13
2年前
java将数据库中查询到的数据导入到Excel表格
1.Maven需要的依赖<!https://mvnrepository.com/artifact/org.apache.poi/poi<dependency<groupIdorg.apache.poi</groupId<artifactIdpoi</artifactId
Stella981 Stella981
2年前
SpringBoot 整合 caffeine
1、pom加入<!https://mvnrepository.com/artifact/org.springframework.boot/springbootstartercache<dependency<groupIdorg.springfra
Stella981 Stella981
2年前
ElasticSearch Bboss + SpringBoot
maven引入bboss包<!https://mvnrepository.com/artifact/com.bbossgroups.plugins/bbosselasticsearchspringbootstarter<dependency<gro
Stella981 Stella981
2年前
SpringBoot 整合 Kafka
1、加入POM<!https://mvnrepository.com/artifact/org.springframework.kafka/springkafka<dependency<groupIdorg.springframework.kafka<
Stella981 Stella981
2年前
Maven添加jar包到 Ubuntu本地中心仓库
中心仓库中的jar包如下:<!https://mvnrepository.com/artifact/org.apache.hadoop/hadoopaws<dependency  <groupIdorg.apache.hadoop</groupId  <artifactIdhado
Stella981 Stella981
2年前
SpringBoot 整合 Flyway
1、启动时migration,在dependencies增加flyway<!https://mvnrepository.com/artifact/com.google.guava/guava<dependency<groupIdcom.google
Stella981 Stella981
2年前
SpringBoot 中使用 H2 数据库
1、加入POM<!https://mvnrepository.com/artifact/com.h2database/h2<dependency<groupIdcom.h2database</groupId<artif
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k