Springboot(2.2.2)+Mybatis-Plus(3.0-RC3)+shedLock

主要架構選型

Springboot(2.2.2)+Mybatis-Plus(3.0-RC3)+shedLock(4.0.1)java

開發注意事項

Q&A node

  • 一、 ShedLock 部署時須要建立表 (特別注意不一樣庫建立的sql)git

    Mysql:
    CREATE TABLE shedlock(
          name VARCHAR(64), 
          lock_until TIMESTAMP(3) NULL, 
          locked_at TIMESTAMP(3) NULL, 
          locked_by  VARCHAR(255), 
          PRIMARY KEY (name)
      ) 
    
    Oracel:
    DROP TABLE ICC_UNION.SHEDLOCK CASCADE CONSTRAINTS;
    CREATE TABLE SHEDLOCK (
     name VARCHAR2(64 CHAR),
     lock_until TIMESTAMP,
     locked_at TIMESTAMP,
     locked_by  VARCHAR2(255 CHAR),
     PRIMARY KEY (name)
    );

    @SchedulerLock(name = "任務名稱 不要重複", lockAtMostFor = "見下", lockAtLeastFor ="見下")github

    /**
     *You can also set lockAtMostFor attribute which specifies how long the lock should be kept in case the executing node dies.
     *  This is just a fallback, under normal circumstances the lock is released as soon the tasks finishes.
     *  You have to set lockAtMostFor to a value which is much longer than normal execution time.
     *  If the task takes longer than lockAtMostFor the resulting behavior may be unpredictable
     *  (more then one process will effectively hold the lock).
     *  lockAtMostFor:鎖的最大時間單位爲毫秒
     *
     *Lastly, you can set lockAtLeastFor attribute which specifies minimum amount of time for which the lock should be kept.
     *  Its main purpose is to prevent execution from multiple nodes in case of really short tasks and clock difference between the nodes.
     *  lockAtLeastFor:鎖的最小時間單位爲毫秒
     */

項目ShedLock地址spring

pom主要jar引用sql

<dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0-RC3</version>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.14</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.6.0.RELEASE</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>net.javacrumbs.shedlock</groupId>
            <artifactId>shedlock-spring</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>net.javacrumbs.shedlock</groupId>
            <artifactId>shedlock-provider-jdbc-template</artifactId>
            <version>4.0.1</version>
        </dependency>

SchedLockConfig 主要配置mybatis

import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.sql.DataSource;

/**
 * @author nopsmile
 * defaultLockAtMostFor 指定在執行節點結束時應保留鎖的默認時間使用ISO8601 Duration格式
 * 做用就是在被加鎖的節點掛了時,沒法釋放鎖,形成其餘節點沒法進行下一任務
 * 這裏默認30s
 * 關於ISO8601 Duration格式用的不到,具體可上網查詢下相關資料,應該就是一套規範,規定一些時間表達方式
 */
@Configuration
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "30s")
public  class SchedLockConfig {

    @Bean
    public LockProvider lockProvider(DataSource dataSource) {
        return new JdbcTemplateLockProvider(dataSource);
    }

}

mybatis主要配置架構

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Properties;
/**
 * @author nopsmile
 */
@Configuration
@EnableTransactionManagement
@ComponentScan
//@MapperScan("com.itcc.mva.mapper*")
public class MybatisPlusConfig {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
    /**
     * 打印 sql
     */
    @Bean
    @Profile({"test"})// 設置 test 環境開啓
    public PerformanceInterceptor performanceInterceptor() {
        logger.info("loading the config of MybatisPlusConfig");
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        //格式化sql語句
        Properties properties = new Properties();
        properties.setProperty("format", "true");
        performanceInterceptor.setProperties(properties);
        return performanceInterceptor;
    }
}

其中job類的oracle

@Component
public class demoJob {
@Scheduled(cron = "0/5 * * * * ?")
    @SchedulerLock(name = "demoJobName", lockAtMostFor = "3s", lockAtLeastFor = "3s")
    public void pushInfo() {
        ...
        }

}

啓動類app

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@MapperScan("com.mapper")
@SpringBootApplication
public class MvaApplication {

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

}

至此整合完畢。本案例適用於小型分佈式定時任務,不想用框架的。注意:多節點須要時間同步,不然會出現任務重複執行問題。

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息