redission在项目中的应用

原项目中的netty是这个版本的
在这里插入图片描述
引入redission依赖
在这里插入图片描述

然后由于项目中已经写好redis配置文件
在这里插入图片描述
在RedissionConfig配置类中直接复制粘贴集群

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    @Value("${redis.auth}")
    private String password;
    @Value("${cluster.host1}")
    private String host1;
    @Value("${cluster.host2}")
    private String host2;
    @Value("${cluster.host3}")
    private String host3;
    @Value("${cluster.host4}")
    private String host4;
    @Value("${cluster.host5}")
    private String host5;
    @Value("${cluster.host6}")
    private String host6;
    @Value("${cluster.port1}")
    private String port1;
    @Value("${cluster.port2}")
    private String port2;
    @Value("${cluster.port3}")
    private String port3;
    @Value("${cluster.port4}")
    private String port4;
    @Value("${cluster.port5}")
    private String port5;
    @Value("${cluster.port6}")
    private String port6;



    @Bean
    public RedissonClient redissonClient(){
        // 配置
        Config config = new Config();
        // 集群模式
         config.useClusterServers().addNodeAddress("redis://"+host1+":"+port1)
                 .addNodeAddress("redis://"+host2+":"+port2)
                 .addNodeAddress("redis://"+host3+":"+port3)
                 .addNodeAddress("redis://"+host4+":"+port4)
                 .addNodeAddress("redis://"+host5+":"+port5)
                 .addNodeAddress("redis://"+host6+":"+port6)
                .setPassword(password);
        // 创建RedissonClient对象
        return Redisson.create(config);
    }
}

如果不配置密码的话,启动项目会报错

2024-02-22 13:49:07.375 WARN [XmlWebApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘partyBoService’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘repairDataService’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘repairUtil’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘partyInfoBaseService’: Unsatisfied dependency expressed through field ‘partyService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘partyService’: Unsatisfied dependency expressed through field ‘partyCompanyService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘partyCompanyService’: Unsatisfied dependency expressed through field ‘partyCompanyBoService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘partyCompanyBoService’: Unsatisfied dependency expressed through field ‘partyLogBoService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘partyLogBoService’: Unsatisfied dependency expressed through field ‘partyPersonnelBoService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘partyPersonnelBoService’: Unsatisfied dependency expressed through field ‘redissonClient’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘redissonClient’ defined in com.axatp.party.config.RedissonConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.redisson.api.RedissonClient]: Factory method ‘redissonClient’ threw exception; nested exception is org.redisson.client.RedisConnectionException: Can’t connect to servers!

接下来就是业务层的引入
在这里插入图片描述
在这里插入图片描述
最后在finally块中释放锁
在这里插入图片描述

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值