赞
踩
Redission是什么就不在这里多说了,可以自己百度或者查看最强分布式锁工具
看了很多博主对 Redission配置基本都是单机模式,实际生产环境Redis部署有主从、哨兵、集群; Redission对不同Redis模式的配置区别挺大;同时,现在大部分项目都是SpringBoot项目,对Redis的使用基本都是使用Spring官网的starter;
所以这里基于SpringBoot-1.5.22.RELEASE,解释一下结合spring-boot-starter-data-redis集成Redission的用法
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.22.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.idto</groupId>
- <artifactId>redis-demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>redis-demo</name>
- <description>redis-demo</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.redisson</groupId>
- <artifactId>redisson</artifactId>
- <version>3.15.3</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.12.0</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
针对Redis不同的部署模式,对RedissonClient进行不同的初始化
- package com.idto.config;
-
- 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.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- import java.util.List;
- import java.util.Objects;
-
-
- @Configuration
- public class RedissonConfig {
-
- Logger log = LoggerFactory.getLogger(RedissonConfig.class);
-
-
- # 直接使用starter注入的redis配置信息
- @Autowired
- private RedisProperties redisProperties;
-
- private int timeout = 3000;
- private int connectionPoolSize = 64;
- private int connectionMinimumIdleSize = 10;
- private int pingConnectionInterval = 60000;
- private static String ADDRESS_PREFIX = "redis://";
-
- /**
- * 单机模式
- */
- @Bean
- public RedissonClient initBean() {
- // 哨兵模式
- RedisProperties.Sentinel sentinel = redisProperties.getSentinel();
- if (Objects.nonNull(sentinel)) {
- log.info("redis is sentinel mode");
- return redissonSentinel();
- }
- // 集群模式
- RedisProperties.Cluster cluster = redisProperties.getCluster();
- if (Objects.nonNull(cluster)) {
- log.info("redis is cluster mode");
- return redissonCluster();
- }
- // 单机模式
- String host = redisProperties.getHost();
- if (StringUtils.isNotBlank(host)) {
- log.info("redis is single mode");
- return redissonSingle();
- }
-
- log.error("redisson config can not support this redis mode");
- return null;
- }
-
- /**
- * 单机模式
- */
- private RedissonClient redissonSingle() {
- String host = redisProperties.getHost();
- String password = redisProperties.getPassword();
- int port = redisProperties.getPort();
- // 设置超时时间
- if (redisProperties.getTimeout() > 0) {
- timeout = redisProperties.getTimeout();
- }
- // 声明一个配置类
- Config config = new Config();
- SingleServerConfig serverConfig = config.useSingleServer()
- .setAddress(ADDRESS_PREFIX + host + ":" + port)
- .setTimeout(timeout)
- .setPingConnectionInterval(pingConnectionInterval)
- .setConnectionPoolSize(this.connectionPoolSize)
- .setConnectionMinimumIdleSize(this.connectionMinimumIdleSize);
- // 判断密码
- if (!StringUtils.isEmpty(password)) {
- serverConfig.setPassword(password);
- }
- return Redisson.create(config);
- }
-
-
- /**
- * 哨兵模式
- */
- private RedissonClient redissonSentinel() {
- // mymaster
- String masterName = redisProperties.getSentinel().getMaster();
- // 127.0.0.1:26389,127.0.0.1:26379
- String nodes = redisProperties.getSentinel().getNodes();
- String password = redisProperties.getPassword();
- // 设置超时时间
- if (redisProperties.getTimeout() > 0) {
- timeout = redisProperties.getTimeout();
- }
- // 声明一个配置类
- Config config = new Config();
- SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
- // 扫描间隔
- sentinelServersConfig.setScanInterval(2000);
- // 判断密码
- if (!StringUtils.isEmpty(password)) {
- sentinelServersConfig.setPassword(password);
- }
- sentinelServersConfig.setMasterName(masterName);
- String[] nodeArr = StringUtils.split(nodes, ",");
- for (int i = 0; i < nodeArr.length; i++) {
- nodeArr[i] = ADDRESS_PREFIX + nodeArr[i];
- }
- // 添加redis节点
- sentinelServersConfig.addSentinelAddress(nodeArr);
- return Redisson.create(config);
- }
-
- /**
- * 集群模式
- */
- private RedissonClient redissonCluster() {
- // 192.168.116.156:1901,192.168.116.156:1902
- List<String> nodes = redisProperties.getCluster().getNodes();
- String password = redisProperties.getPassword();
- // 设置超时时间
- if (redisProperties.getTimeout() > 0) {
- timeout = redisProperties.getTimeout();
- }
- // 声明一个配置类
- Config config = new Config();
- ClusterServersConfig clusterServersConfig = config.useClusterServers();
- // 扫描间隔
- clusterServersConfig.setScanInterval(2000);
- // 判断密码
- if (!StringUtils.isEmpty(password)) {
- clusterServersConfig.setPassword(password);
- }
- // 添加redis节点
- for (String node : nodes) {
- clusterServersConfig.addNodeAddress(ADDRESS_PREFIX + node);
- }
- return Redisson.create(config);
- }
- }
- spring:
- redis:
-
- # 单机/主从模式
- # host: 192.168.8.129
- # port: 7001
-
- # 哨兵模式
- # sentinel:
- # master: mymaster
- # nodes: 192.168.8.129:27001,192.168.8.129:27002,192.168.8.129:27003
-
- # 集群模式
- cluster:
- nodes: 192.168.8.129:8001,192.168.8.129:8002,192.168.8.129:8003,192.168.8.129:8004,192.168.8.129:8005,192.168.8.129:8006
-
- # redis服务数据库下标
- database: 2
- # redis服务密码
- # password: 111111
- pool:
- max-idle: 100
- min-idle: 1
- max-active: 10
- max-wait: 5000
- package com.idto.controller;
-
- import org.redisson.api.RLock;
- import org.redisson.api.RedissonClient;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.concurrent.TimeUnit;
-
-
- @RestController
- public class RedissionController {
-
- Logger log = LoggerFactory.getLogger(RedissionController.class);
-
- @Autowired
- private RedissonClient redissonClient;
-
- @GetMapping("/lock")
- public String lock() throws InterruptedException {
- String lockKey = "lock";
- RLock lock = redissonClient.getLock(lockKey);
- boolean lockResult = lock.tryLock();
- log.info("lock key {} lock result {}", lockKey, lockResult);
- TimeUnit.SECONDS.sleep(5);
- lock.unlock();
- log.info("lock key {} unlock ", lockKey);
- return System.currentTimeMillis() + "---" + lockResult;
- }
-
-
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。