加入pom依赖
<!-- redisson -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.12.4</version>
</dependency>
application.properties
配置文件
package cn.witsky.rcs_backend;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@EnableTransactionManagement
@EnableAsync
@EnableScheduling
@EnableFeignClients
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEncryptableProperties
public class RcsBackendApplication {
public static void main(String[] args) {
SpringApplication.run(RcsBackendApplication.class, args);
}
/**
* 用于任务指派和任务通知的线程池
*/
@Bean
public ThreadPoolExecutor threadPoolExecutor() {
return new ThreadPoolExecutor(50, 50, 10, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.AbortPolicy());
}
/**
* 用于数据库导入的线程池
*/
@Bean
public ThreadPoolExecutor threadPoolExecutorImport() {
return new ThreadPoolExecutor(20, 20, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(40), new ThreadPoolExecutor.CallerRunsPolicy());
}
@Bean
public ThreadPoolExecutor threadPoolExecutorOne() {
return new ThreadPoolExecutor(10, 10, 10, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.AbortPolicy());
}
@Bean
public Cache cache() {
return CacheBuilder.newBuilder().maximumSize(30000).expireAfterWrite(3, TimeUnit.MINUTES).build();
}
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduling = new ThreadPoolTaskScheduler();
scheduling.setPoolSize(10);
scheduling.initialize();
return scheduling;
}
/**
* redisson 哨兵和单机模式自适应
*/
@Autowired
private Environment environment;//注入配置项,也可以换成下面的redisProperties
@Autowired
private RedisProperties redisProperties;//直接用这个对象注入获取
@Bean
public RedissonClient redisson() {
String master=environment.getProperty("spring.redis.sentinel.master");
//redisProperties.getSentinel().getMaster();
String nodes=environment.getProperty("spring.redis.sentinel.nodes");
String password=environment.getProperty("spring.redis.password");
String database=environment.getProperty("spring.redis.database");
String host=environment.getProperty("spring.redis.host");
String port=environment.getProperty("spring.redis.port");
if(StringUtils.isNotBlank(master)&&StringUtils.isNotBlank(nodes)){//哨兵
System.out.println("======哨兵");
Config config = new Config();
String [] adds=nodes.split(",");
for(int i=0;i<adds.length;i++){
adds[i]="redis://"+adds[i];
}
SentinelServersConfig serverConfig=config.useSentinelServers()
.addSentinelAddress(adds)
.setMasterName(master);
if(StringUtils.isNotBlank(password)){
serverConfig.setPassword(password);
}
if(StringUtils.isNotBlank(database)){
serverConfig.setDatabase(Integer.parseInt(database));
}
return Redisson.create(config);
}else if (StringUtils.isNotBlank(host)){//单机
System.out.println("======单机");
Config config = new Config();
SingleServerConfig singleServerConfig = config.useSingleServer()
.setAddress("redis://"+host+":"+port);
if (!StringUtils.isEmpty(password)) {
singleServerConfig.setPassword(password);
}
if(StringUtils.isNotBlank(database)){
singleServerConfig.setDatabase(Integer.parseInt(database));
}
return Redisson.create(config);
}else {//集群
System.out.println("======集群");
String[] nodesList = nodes.split(",");
for(int i=0;i<nodesList.length;i++) {
nodesList[i] = "redis://" + nodesList[i];
}
Config config = new Config();
ClusterServersConfig serverConfig = config.useClusterServers()
.addNodeAddress(nodesList);
if (!StringUtils.isEmpty(password)) {
serverConfig.setPassword(password);
}
return Redisson.create(config);
}
}
}
redisson简单应用
@Autowired
private RedissonClient redissonClient;
int count=500;
public void decrementProductStore(RedissonClient redissonClient) {
String key = "zpy";
RLock lock = redissonClient.getLock(key);
try {
lock.lock();
Thread.sleep(1000);
if (count > 0) {
count = count - 1;
}
System.out.println("------" + count);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
lock.unlock();
}
}
re