SpringBoot如何配置Redisson呢?
下文笔者讲述SpringBoot配置Redisson的方法及示例分享,如下所示
借助spring-boot-starter-data-redis 然后编写application.yml 使用代码加载redisson配置
引入 redisson-spring-boot-starter 然后使用application.yml
法1:application.yml(通用配置) 直接使用spring-boot-starter-data-redis的配置。 spring: redis: host: 127.0.0.1 port: 6379 # password: # database: 0 #指定数据库,默认为0 # timeout: 3000 #连接超时时间,单位毫秒,默认为0。也可以这么写:3s # ssl: false # 是否启用SSL连接,默认false # pool: #连接池配置 # max-active: 8 #最大活跃连接数,默认8个。 # max-idle: 8 #最大空闲连接数,默认8个。 # max-wait: -1 #获取连接的最大等待时间,默认-1,表示无限制,单位毫秒。 # #默认值可能会因为获取不到连接,导致事务无法提交,数据库被锁,大量线程处于等待状态的情况。 # min-idle: 0 #最小空闲连接数,默认0。 # sentinel: # master: myMaster #哨兵master # nodes: host1:port,host2:port #哨兵节点 # cluster: # max-redirects: # 集群模式下,集群最大转发的数量 # nodes: host1:port,host2:port # 集群节点 法2:application.yml(redisson专用配置) 写法1:都写在application.yml spring: redis: redisson: config: | clusterServersConfig: idleConnectionTimeout: 10000 connectTimeout: 10000 timeout: 3000 retryAttempts: 3 retryInterval: 1500 failedSlaveReconnectionInterval: 3000 failedSlaveCheckInterval: 60000 password: null subscriptionsPerConnection: 5 clientName: null loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {} subscriptionConnectionMinimumIdleSize: 1 subscriptionConnectionPoolSize: 50 slaveConnectionMinimumIdleSize: 24 slaveConnectionPoolSize: 64 masterConnectionMinimumIdleSize: 24 masterConnectionPoolSize: 64 readMode: "SLAVE" subscriptionMode: "SLAVE" nodeAddresses: - "redis://127.0.0.1:7004" - "redis://127.0.0.1:7001" - "redis://127.0.0.1:7000" scanInterval: 1000 pingConnectionInterval: 0 keepAlive: false tcpNoDelay: false threads: 16 nettyThreads: 32 codec: !<org.redisson.codec.MarshallingCodec> {} transportMode: "NIO" 写法2:redisson配置单独拿出来 applicaion.yml spring: redis: redisson: config: classpath:redisson.yml redisson.yml clusterServersConfig: idleConnectionTimeout: 10000 connectTimeout: 10000 timeout: 3000 retryAttempts: 3 retryInterval: 1500 password: null subscriptionsPerConnection: 5 clientName: null loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {} slaveSubscriptionConnectionMinimumIdleSize: 1 slaveSubscriptionConnectionPoolSize: 50 slaveConnectionMinimumIdleSize: 32 slaveConnectionPoolSize: 64 masterConnectionMinimumIdleSize: 32 masterConnectionPoolSize: 64 readMode: "SLAVE" nodeAddresses: - "redis://127.0.0.1:7004" - "redis://127.0.0.1:7001" - "redis://127.0.0.1:7000" scanInterval: 1000 threads: 0 nettyThreads: 0 codec: !<org.redisson.codec.JsonJacksonCodec> {} transportMode:"NIO"
使用代码加载redisson配置
法1:编写配置类 需要在Resource目录下创建redisson.yml。 @Configuration public class RedssonConfig { @Bean(destroyMethod="shutdown") public RedissonClient redisson() throws IOException { RedissonClient redisson = Redisson.create( Config.fromYAML(new ClassPathResource("redisson-single.yml").getInputStream())); return redisson; } } 法2:用代码配置 import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RedissonConfig { @Bean public RedissonClient redissonClient() { Config config = new Config(); //指定编码,默认编码为org.redisson.codec.JsonJacksonCodec //config.setCodec(new org.redisson.client.codec.StringCodec()); config.useSingleServer() .setAddress("redis://47.108.105.159:6379") .setPassword("cen66lyz") .setConnectionPoolSize(50) .setIdleConnectionTimeout(10000) .setConnectTimeout(3000) .setTimeout(3000) .setDatabase(5); return Redisson.create(config); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。