Skip to content
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

new features: add distributed scheduling support. #3732

Merged
merged 5 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions spring-cloud-alibaba-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<spring.ai.version>0.8.1</spring.ai.version>
<dashscope-sdk-java.version>2.14.0</dashscope-sdk-java.version>

<!-- scheduling -->
<shedlock.version>4.23.0</shedlock.version>
<schedulerx.worker.version>1.11.4</schedulerx.worker.version>

<!-- Maven Plugin Versions -->
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
<maven-javadoc-plugin.version>3.1.1</maven-javadoc-plugin.version>
Expand Down Expand Up @@ -185,6 +189,23 @@
<version>${rocketmq.version}</version>
</dependency>

<!-- Alibaba scheduling -->
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>${shedlock.version}</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-jdbc-template</artifactId>
<version>${shedlock.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun.schedulerx</groupId>
<artifactId>schedulerx2-worker</artifactId>
<version>${schedulerx.worker.version}</version>
</dependency>

<!-- Own dependencies -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
Expand Down Expand Up @@ -258,6 +279,12 @@
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-schedulerx</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
Expand Down
8 changes: 4 additions & 4 deletions spring-cloud-alibaba-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
<module>rocketmq-example/rocketmq-example-common</module>
<module>rocketmq-example/rocketmq-tx-example</module>
<module>rocketmq-example/rocketmq-pollable-consume-example</module>

<module>spring-cloud-bus-rocketmq-example</module>
<module>spring-cloud-alibaba-sidecar-examples/spring-cloud-alibaba-sidecar-nacos-example</module>
<module>spring-cloud-alibaba-sidecar-examples/spring-cloud-alibaba-sidecar-consul-example</module>
Expand All @@ -51,9 +50,10 @@
<module>integrated-example/integrated-praise-provider</module>
<module>integrated-example/integrated-praise-consumer</module>
<module>integrated-example/integrated-common</module>
<module>integrated-example/integrated-frontend</module>
<module>spring-cloud-ai-example</module>
</modules>
<module>integrated-example/integrated-frontend</module>
<module>spring-cloud-ai-example</module>
<module>spring-cloud-scheduling-example</module>
</modules>


<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-alibaba-examples</artifactId>
<groupId>com.alibaba.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-cloud-scheduling-example</artifactId>
<name>Spring Cloud Starter Alibaba Scheduling Example</name>
<description>Example demonstrating how to use Spring Cloud Schedule</description>
<packaging>jar</packaging>

<dependencies>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-schedulerx</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- database for shedlock start, JDBC data source configuration should be added -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- database for shedlock end -->

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024-2025 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.schedule;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
* ScheduleApplication.
*
* @author yaohui
*/
@SpringBootApplication
@EnableScheduling
public class ScheduleApplication {

public static void main(String[] args) {
SpringApplication.run(ScheduleApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024-2025 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.schedule.job;

import java.util.concurrent.TimeUnit;

import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
* @author yaohui
**/
@Component
public class SimpleJob {

private static final Logger logger = LoggerFactory.getLogger(SimpleJob.class);

/**
* run without lock, all instance running at the same time.
*/
@Scheduled(cron = "0 */1 * * * ?")
public void job1() {
logger.info("time=" + DateTime.now().toString("YYYY-MM-dd HH:mm:ss") + " do job1...");
}


/**
* run with lock, only one instance running at the same time.
*
* @throws InterruptedException interrupted exception
*/
@Scheduled(cron = "0 */1 * * * ?")
@SchedulerLock(name = "lock-job2", lockAtMostFor = "10s")
public void job2() throws InterruptedException {
logger.info("time=" + DateTime.now().toString("YYYY-MM-dd HH:mm:ss") + " do job2...");
TimeUnit.SECONDS.sleep(1L);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
spring:
cloud:
scheduling:
# Distributed mode: shedlock, schedulerx
distributed-mode: schedulerx
schedulerx:
# This configuration is required, Please get it from aliyun schedulerx console
endpoint: acm.aliyun.com
namespace: aad167f6-xxxx-xxxx-xxxx-xxxxxxxxx
groupId: xxxxx
appKey: PZm1XXXXXXXXXXXX
# Optional config, if you need to sync task to schedulerx
# task-sync: true
# region-id: public
# aliyun-access-key: XXXXXXXXXXXX
# aliyun-secret-key: XXXXXXXXXXXX
# task-model-default: standalone
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
spring:
cloud:
scheduling:
# Distributed mode: shedlock, schedulerx
distributed-mode: shedlock
datasource:
driver-class: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
server:
port: 18080
spring:
profiles:
active: shedlock
application:
name: spring-cloud-alibaba-schedule-example
# Spring task scheduling config
task:
scheduling:
thread-name-prefix: sca-schedule-
pool:
size: 5
shutdown:
await-termination: true
await-termination-period: 60s
3 changes: 2 additions & 1 deletion spring-cloud-alibaba-starters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
<module>spring-cloud-alibaba-sentinel-gateway</module>
<module>spring-cloud-alibaba-commons</module>
<module>spring-cloud-starter-alibaba-ai</module>
</modules>
<module>spring-cloud-starter-alibaba-schedulerx</module>
</modules>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-starters</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>spring-cloud-starter-alibaba-schedulerx</artifactId>
<name>Spring Cloud Starter Alibaba Scheduling</name>

<dependencies>

<!--spring boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
</dependency>

<!-- schedulerx dependency -->
<dependency>
<groupId>com.aliyun.schedulerx</groupId>
<artifactId>schedulerx2-worker</artifactId>
</dependency>

<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-schedulerx2</artifactId>
<version>1.1.0</version>
</dependency>

<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.6</version>
</dependency>
<!-- schedulerx dependency end-->

<!-- shedlock dependency -->
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
</dependency>

<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-jdbc-template</artifactId>
</dependency>
<!-- shedlock dependency end -->
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024-2025 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.scheduling;

/**
* @author yaohui
**/
public final class SchedulingConstants {

/**
* Scheduling config prefix.
*/
public static final String SCHEDULING_CONFIG_PREFIX = "spring.cloud.scheduling";

/**
* Scheduling distributed mode.
*/
public static final String SCHEDULING_CONFIG_DISTRIBUTED_MODE_KEY = SCHEDULING_CONFIG_PREFIX + ".distributed-mode";

private SchedulingConstants() {
throw new AssertionError("Must not instantiate constant utility class");
}

}
Loading
Loading