-
Notifications
You must be signed in to change notification settings - Fork 110
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
yaohuitc
wants to merge
4
commits into
alibaba:master
Choose a base branch
from
yaohuitc:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,63 +10,79 @@ | |
### 如何接入 | ||
|
||
1. 登录阿里云分布式任务调度[Schedulerx控制台](https://schedulerx2.console.aliyun.com),点击开通服务 | ||
|
||
2. pom增加依赖aliyun-schedulerx-spring-boot-starter | ||
|
||
2. 进入任务调度控制台->应用管理,点击"创建应用",配置应用分组信息保存(PS:本地开发测试环境仅支持公网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; | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...-sample/src/main/java/com/alibaba/cloud/examples/schedulerx/job/annotation/SpringJob.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
region字符前有异常字符,需要删除一下。