Skip to content

update aliyun-schedulerx-spring-boot-sample #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@
<artifactId>aliyun-schedulerx-spring-boot-starter</artifactId>
</dependency>

<!-- 添加springboot的log4j2依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.26</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,79 @@
### 如何接入

1. 登录阿里云分布式任务调度[Schedulerx控制台](https://schedulerx2.console.aliyun.com),点击开通服务

2. pom增加依赖aliyun-schedulerx-spring-boot-starter

2. 进入任务调度控制台->应用管理,点击"创建应用",配置应用分组信息保存(PS:本地开发测试环境仅支持公网region创建分组对接)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

region字符前有异常字符,需要删除一下。


3. pom增加依赖aliyun-schedulerx-spring-boot-starter
```xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-schedulerx-spring-boot-starter</artifactId>
</dependency>
```

3. 配置application.yml
4. 应用配置application.properties,可在控制台->应用管理->XXX应用(接入配置)点击获取
```properties
## 请填写不同region的endpoint
spring.schedulerx2.endpoint=acm.aliyun.com
## region内全局唯一,控制台上创建命名空间时自动生成
spring.schedulerx2.namespace=aad167f6-8bee-xxxx-xxxx-xxxxxxxxx
## 应用的id标识,从控制台上创建应用分组时自定义填写,一个命名空间下需要唯一
spring.schedulerx2.groupId=qianxi.dev
## 应用的key,从控制台上获取
spring.schedulerx2.appKey=xxxxxxxxxxxxxxxxxxxxxxxx
## 接管Spring定时任务
spring.schedulerx2.task.scheduling.scheduler=schedulerx

# 可选,开启自动同步Spring注解任务
#spring.schedulerx2.task.scheduling.sync=true
#spring.schedulerx2.task.scheduling.overwrite=false
#spring.schedulerx2.regionId=public
#spring.schedulerx2.aliyunAccessKey=XXXXXXXXX
#spring.schedulerx2.aliyunSecretKey=XXXXXXXXX
```

通过配置文件定义任务并自动同步创建,配置application.yml (可选模式)
```yaml
spring:
schedulerx2:
endpoint: acm.aliyun.com #请填写不同regin的endpoint
namespace: 433d8b23-06e9-xxxx-xxxx-90d4d1b9a4af #region内全局唯一,建议使用UUID生成
namespaceName: 学仁测试
appName: myTest
groupId: myTest.group #同一个命名空间下需要唯一
appKey: myTest123@alibaba #应用的key,不要太简单,注意保管好
regionId: public #填写对应的regionId
aliyunAccessKey: xxxxxxx #阿里云账号的ak
aliyunSecretKey: xxxxxxx #阿里云账号的sk
alarmChannel: sms,ding #报警通道:短信和钉钉
jobs:
endpoint: acm.aliyun.com #请填写不同region的endpoint
namespace: 433d8b23-xxx-xxx-xxx-90d4d1b9a4af #region内全局唯一,推荐控制台手动创建,如控制台region下不存在且存在任务配置时会自动创建(建议使用UUID生成
namespaceName: 测试环境
namespaceSource: schedulerx
appName: myTest #应用名称,如控制台手动创建应用分组则直接复制过来配置即可
groupId: myTest.group #应用的id标识,一个命名空间下需要唯一,推荐控制台手动创建,如控制台region下不存在且存在任务配置时会自动创建
appKey: myTest123@alibaba #应用的key,不要太简单,注意保管好,如控制台手动创建应用分组则直接复制过来配置即可
regionId: public #请填写不同的regionId,用于自动同步下方的任务信息至指定的region
aliyunAccessKey: xxxxxxxxx
aliyunSecretKey: xxxxxxxxx
jobs:
simpleJob:
jobModel: standalone
className: com.aliyun.schedulerx.example.processor.SimpleJob
className: com.alibaba.cloud.examples.schedulerx.job.processor.SimpleJob
cron: 0/30 * * * * ? # cron表达式
jobParameter: hello
overwrite: true
shardingJob:
jobModel: sharding
className: ccom.aliyun.schedulerx.example.processor.ShardingJob
className: com.alibaba.cloud.examples.schedulerx.job.processor.ShardingJob
oneTime: 2022-06-02 12:00:00 # 一次性任务表达式
jobParameter: 0=Beijing,1=Shanghai,2=Guangzhou
overwrite: true
broadcastJob: # 不填写cron和oneTime,表示api任务
jobModel: broadcast
className: com.aliyun.schedulerx.example.processor.BroadcastJob
className: com.alibaba.cloud.examples.schedulerx.job.processor.BroadcastJob
jobParameter: hello
overwrite: true
mapReduceJob:
jobModel: mapreduce
className: com.aliyun.schedulerx.example.processor.MapReduceJob
className: com.alibaba.cloud.examples.schedulerx.job.processor.MapReduceJob
cron: 0 * * * * ?
jobParameter: 100
overwrite: true
alarmUsers: #报警联系人
user1:
userName: 张三
userPhone: 12345678900
user2:
userName: 李四
ding: https://oapi.dingtalk.com/robot/send?access_token=xxxxx
```

4. 实现任务接口,以单机任务为例,更多任务模型请看examples
5. 实现任务接口,以单机任务为例,更多任务模型请看examples
```java
package com.alibaba.cloud.examples.schedulerx.job;

Expand Down Expand Up @@ -95,9 +111,39 @@ public class SimpleJob extends JavaProcessor {
public void kill(JobContext context) {
}
}
```
```
支持Spring原生定时任务调度
```java
@Component
public class SpringJob {

/**
* log4j2/logback配置schedulerxLogAppender,可以进行日志采集
*/
private static final Logger logger = LoggerFactory.getLogger("schedulerx");

/**
* An spring scheduled job
*/
@Scheduled(cron = "0/5 * * * * ?")
public void simpleJob() {
logger.info("hello schedulerx! this's an spring simple job.");
}

/**
* An spring scheduled job
* Can use @SchedulerX annotation set job config when open sync(true)
*/
@Scheduled(cron = "0/5 * * * * ?")
@SchedulerX(name = "simpleJobWithAnnotation", model = ExecuteMode.STANDALONE)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

该处需要跟实际代码保持一致。

public void simpleJobWithAnnotation() {
logger.info("hello schedulerx! this's an spring simple job with SchedulerX annotation.");
}

}
```

5. 启动你的springboot启动类,大概1分钟左右,你的定时任务就可以正常调度起来了
6. 启动你的springboot启动类,大概1分钟左右,你的定时任务就可以正常调度起来了
```
hello schedulerx!
hello schedulerx!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
* SchedulerxApplication.
*
* @author xiaomeng.hxm
*/
@SpringBootApplication
@ComponentScan(value="com.alibaba.schedulerx.*")
@EnableScheduling
public class SchedulerxApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package com.alibaba.cloud.examples.schedulerx.domain;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.schedulerx.worker.processor.BizSubTask;
import com.google.common.collect.Maps;

/**
* AccountInfo.
Expand All @@ -44,7 +44,7 @@ public AccountInfo(long id, String name, String accountId) {
*/
@Override
public Map<String, String> labelMap() {
Map<String, String> labelMap = Maps.newHashMap();
Map<String, String> labelMap = new HashMap<>();
labelMap.put("user", name);
return labelMap;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.examples.schedulerx.job.annotation;

import com.alibaba.schedulerx.common.domain.ExecuteMode;
import com.alibaba.schedulerx.scheduling.annotation.SchedulerX;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
* Spring scheduled job
* @author yaohui
* @create 2022/10/17 下午2:51
**/
@Component
public class SpringJob {

/**
* log4j2/logback配置schedulerxLogAppender,可以进行日志采集
*/
private static final Logger logger = LoggerFactory.getLogger("schedulerx");

/**
* An spring scheduled job
*/
@Scheduled(cron = "0/5 * * * * ?")
public void simpleJob() {
logger.info("hello schedulerx! this's an spring simple job.");
}

/**
* An spring scheduled job
* Can use @SchedulerX annotation set job config when open sync(true)
*/
@SchedulerX(name = "simpleJobWithAnnotation", cron = "0 0/1 * * * ?", model = ExecuteMode.STANDALONE)
public void simpleJobWithAnnotation() {
logger.info("hello schedulerx! this's an spring simple job with SchedulerX annotation.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.alibaba.cloud.examples.schedulerx.job;
package com.alibaba.cloud.examples.schedulerx.job.processor;

import java.util.Map;
import java.util.Map.Entry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.alibaba.cloud.examples.schedulerx.job;
package com.alibaba.cloud.examples.schedulerx.job.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

package com.alibaba.cloud.examples.schedulerx.job;
package com.alibaba.cloud.examples.schedulerx.job.processor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -24,7 +25,6 @@
import com.alibaba.schedulerx.worker.domain.JobContext;
import com.alibaba.schedulerx.worker.processor.MapReduceJobProcessor;
import com.alibaba.schedulerx.worker.processor.ProcessResult;
import com.google.common.collect.Lists;

/**
* MapReduce任务Demo:分发50条消息,分布式并行处理
Expand All @@ -43,7 +43,7 @@ public ProcessResult process(JobContext context) throws Exception {
}
if (isRootTask(context)) {
System.out.println("start root task");
List<String> msgList = Lists.newArrayList();
List<String> msgList = new ArrayList<>();
for (int i = 0; i <= dispatchNum; i++) {
msgList.add("msg_" + i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.alibaba.cloud.examples.schedulerx.job;
package com.alibaba.cloud.examples.schedulerx.job.processor;

import java.util.LinkedList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

package com.alibaba.cloud.examples.schedulerx.job;
package com.alibaba.cloud.examples.schedulerx.job.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
Expand All @@ -26,7 +27,6 @@
import com.alibaba.schedulerx.worker.domain.JobContext;
import com.alibaba.schedulerx.worker.processor.MapJobProcessor;
import com.alibaba.schedulerx.worker.processor.ProcessResult;
import com.google.common.collect.Lists;

/**
* 通过Map模型扫描分库分表
Expand Down Expand Up @@ -57,7 +57,7 @@ public ProcessResult process(JobContext context) throws Exception {
Pair<Long, Long> idPair = queryMinAndMaxId(tableName);
long minId = idPair.getFirst();
long maxId = idPair.getSecond();
List<PageTask> tasks = Lists.newArrayList();
List<PageTask> tasks = new ArrayList<>();
int step = (int) ((maxId - minId) / PAGE_SIZE); //计算分页数量
for (long i = minId; i < maxId; i+=PAGE_SIZE) {
long startId = i;
Expand All @@ -79,13 +79,13 @@ public ProcessResult process(JobContext context) throws Exception {
}

private List<String> getDbList() {
List<String> dbList = Lists.newArrayList();
List<String> dbList = new ArrayList<>();
//TODO 返回分库列表
return dbList;
}

private List<String> getTableList(String dbName) {
List<String> tableList = Lists.newArrayList();
List<String> tableList = new ArrayList<>();
//TODO 返回分表列表
return tableList;
}
Expand All @@ -96,7 +96,7 @@ private Pair<Long, Long> queryMinAndMaxId(String tableName) {
}

private List<Record> queryRecord(String tableName, long startId, long endId) {
List<Record> records = Lists.newArrayList();
List<Record> records = new ArrayList<>();
//TODO select * from [tableName] where id>=[startId] and id<[endId]
return records;
}
Expand Down
Loading