All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dengchen2020.lock.config.LockAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 0.0.31
Show newest version
package io.github.dengchen2020.lock.config;

import io.github.dengchen2020.lock.LockAop;
import io.github.dengchen2020.lock.api.DLock;
import io.github.dengchen2020.lock.api.RedissonLock;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.util.concurrent.Executors;

/**
 * 锁自动配置
 * @author dengchen
 * @since 2024/7/1
 */
@Configuration(proxyBeanMethods = false)
public class LockAutoConfiguration {

    @ConditionalOnMissingBean
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient(@Value("${spring.data.redis.host}") String host, @Value("${spring.data.redis.port:6379}") String port, @Value("${spring.data.redis.password}") String password, @Value("${spring.data.redis.database:0}") int database, @Value("${spring.data.redis.username:#{null}}") String username) {
        Config config = new Config();
        config.setExecutor(Executors.newThreadPerTaskExecutor(Thread.ofVirtual().name("redisson-", 0).factory()));
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setKeepAlive(true).setAddress("redis://" + host + ":" + port).setPassword(password).setDatabase(database);
        if(StringUtils.hasText(username)) singleServerConfig.setUsername(username);
        config.setUseScriptCache(true);
        return Redisson.create(config);
    }

    @Bean
    public DLock dLock(RedissonClient redissonClient){
        return new RedissonLock(redissonClient);
    }

    @Bean
    public LockAop lockAop(RedissonClient redissonClient){
        return new LockAop(redissonClient);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy