【Java】Springboot使用Redisson配置
2022/1/11 19:03:33
本文主要是介绍【Java】Springboot使用Redisson配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、单机配置
@Component @ConfigurationProperties(prefix = "spring.redis") public class RedisConfigProperties { private String password; private String database; private int port; private String host; /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /** * @return the database */ public String getDatabase() { return database; } /** * @param database the database to set */ public void setDatabase(String database) { this.database = database; } /** * @return the port */ public int getPort() { return port; } /** * @param port the port to set */ public void setPort(int port) { this.port = port; } /** * @return the host */ public String getHost() { return host; } /** * @param host the host to set */ public void setHost(String host) { this.host = host; } }
@Configuration public class RedissonConfig { @Autowired private RedisConfigProperties redisConfigProperties; //添加redisson的bean @Bean public Redisson redisson() { //redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加 String redissonAddr = "redis://" + redisConfigProperties.getHost()+":"+redisConfigProperties.getPort(); Config config = new Config(); SingleServerConfig singleConfig = config.useSingleServer().setAddress(redissonAddr). //心跳检测,定时与redis连接,可以防止一段时间过后,与redis的连接断开 setPingConnectionInterval(1000); singleConfig.setPassword(redisConfigProperties.getPassword());//设置密码 return (Redisson) Redisson.create(config); } }
2、集群配置
@Component @ConfigurationProperties(prefix = "spring.redis") public class RedisConfigProperties { private String password; private cluster cluster; public static class cluster { private List<String> nodes; public List<String> getNodes() { return nodes; } public void setNodes(List<String> nodes) { this.nodes = nodes; } } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public RedisConfigProperties.cluster getCluster() { return cluster; } public void setCluster(RedisConfigProperties.cluster cluster) { this.cluster = cluster; } }
@Configuration public class RedissonConfig { @Autowired private RedisConfigProperties redisConfigProperties; //添加redisson的bean @Bean public Redisson redisson() { //redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加 List<String> clusterNodes = new ArrayList<>(); for (int i = 0; i < redisConfigProperties.getCluster().getNodes().size(); i++) { clusterNodes.add("redis://" + redisConfigProperties.getCluster().getNodes().get(i)); } Config config = new Config(); ClusterServersConfig clusterServersConfig = config.useClusterServers() .addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()])); //心跳检测,定时与redis连接,可以防止一段时间过后,与redis的连接断开 clusterServersConfig.setPingConnectionInterval(1000); clusterServersConfig.setPassword(redisConfigProperties.getPassword());//设置密码 return (Redisson) Redisson.create(config); } }
3、使用方法
public boolean test1() throws Exception { String LOCK_STATUS = "lock_status"; RLock lock = null; try { lock = redissonClient.getLock(LOCK_STATUS); if (lock.tryLock()) { //业务..... } else { return false; } } catch (Exception e) { logger.error("err",e); }finally { if(null != lock && lock.isHeldByCurrentThread()) { lock.unlock(); } } return true; }
4、maven
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.13.1</version> </dependency>
这篇关于【Java】Springboot使用Redisson配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-10百万架构师第十三课:源码分析:Spring 源码分析:Spring核心IOC容器及依赖注入原理|JavaGuide
- 2025-01-10便捷好用的电商API工具合集
- 2025-01-09必试!帮 J 人团队解决物流错发漏发的软件神器!
- 2025-01-09不容小觑!助力 J 人物流客服安抚情绪的软件!
- 2025-01-09为什么医疗团队协作离不开智能文档工具?
- 2025-01-09惊叹:J 人团队用啥软件让物流服务快又准?
- 2025-01-09如何利用数据分析工具优化项目资源分配?4种工具推荐
- 2025-01-09多学科协作难?这款文档工具可以帮你省心省力
- 2025-01-09团队中的技术项目经理TPM:工作内容与资源优化策略
- 2025-01-09JIT生产管理法:优化流程,提升竞争力的秘诀