其他分享
首页 > 其他分享> > Springboot(2.2.2)+Mybatis-Plus(3.0-RC3)+shedLock

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

作者:互联网

主要架构选型

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

开发注意事项

Q&A

项目ShedLock地址

pom主要jar引用


 <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 主要配置

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类的

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

}

启动类

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);
    }

}

至此整合完毕。
本案例适用于小型分布式定时任务,不想用框架的。
注意:
多节点需要时间同步,否则会出现任务重复执行问题。

标签:Springboot,springframework,RC3,public,Plus,org,import,com,annotation
来源: https://blog.51cto.com/4534309/2487473