Spring接入Redis哨兵集群

  • 导入依赖
<!-- Redis-Client -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.0</version>
</dependency>
        
<!-- spring-redis -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.6.4.RELEASE</version>
</dependency>
        
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.4.2</version>
</dependency>
  • 创建Redis配制文件
    sentinel.properties
#主节点redis名称
spring.redis.sentinel.master=master_server
#哨兵节点
spring.redis.sentinel.nodes=xxx.xxx.xxx.xxx:26379

conf.properties

#redis configuration
redis.maxTotal=200
redis.maxIdle=100
redis.minIdle=8
redis.maxWaitMillis=100000
redis.maxActive=300
redis.timeout=100000
  • redis工具类
    RedisUtil.java

import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

/**
 * @author: zhuocc3
 * @date: 2020-01-05 2:21:35 PM
 * @ClassName: RedisUtil.java
 * @Description: TODO Redis工具类
 */
public class RedisUtil { 

    private RedisTemplate<Serializable, Object> redisTemplate;  
  
    /** 
     * 批量删除 
     * @param keys 
     */  
    public void remove(final String... keys) {  
        for (String key : keys) {  
            remove(key);  
        }  
    }  
  
    /** 
     * 批量删除
     * @param pattern 
     */  
    public void removePattern(final String pattern) {  
        Set<Serializable> keys = redisTemplate.keys(pattern);  
        if (keys.size() > 0)  
            redisTemplate.delete(keys);  
    }  
  
   /**
    * @author: zhuocc3
    * @date: 2020-01-05 2:23:05 PM
    * @Description: TODO 删除指定的key
    * @param key
    */
    public void remove(final String key) {  
        if (exists(key)) {  
            redisTemplate.delete(key);  
        }  
    }  
  
    /**
     * @author: zhuocc3
     * @date: 2020-01-05 2:23:35 PM
     * @Description: TODO 是否存在key
     * @param key
     * @return
     */
    public boolean exists(final String key) {  
        return redisTemplate.hasKey(key);  
    }  
  
    /**
     * @author: zhuocc3
     * @date: 2020-01-05 2:23:53 PM
     * @Description: TODO 根据key取值
     * @param key
     * @return
     */
    public Object get(final String key) {  
        Object result = null;  
        ValueOperations<Serializable, Object> operations = redisTemplate  
                .opsForValue();  
        result = operations.get(key);  
        return result;  
    }  
  
    /**
     * @author: zhuocc3
     * @date: 2020-01-05 2:24:22 PM
     * @Description: TODO set string value of type
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {  
        boolean result = false;  
        try {  
            ValueOperations<Serializable, Object> operations = redisTemplate  
                    .opsForValue();  
            operations.set(key, value);  
            result = true;  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return result;  
    }  
  
    /**
     * @author: zhuocc3
     * @date: 2020-01-05 2:24:50 PM
     * @Description: TODO set value by key & attach expire time attribute
     * @param key
     * @param value
     * @param expireTime
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {  
        boolean result = false;  
        try {  
            ValueOperations<Serializable, Object> operations = redisTemplate  
                    .opsForValue();  
            operations.set(key, value);  
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);  
            result = true;  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return result;  
    }  
  
    public void setRedisTemplate(  
            RedisTemplate<Serializable, Object> redisTemplate) {  
        this.redisTemplate = redisTemplate;  
    }  
}

  • applicationContext-redis.xml配制
<context:property-placeholder location="classpath:conf/conf.properties" ignore-unresolvable="true" />

    <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <constructor-arg name="propertySource" ref="propertySource" />
    </bean>
    
    <bean name="propertySource" class="org.springframework.core.io.support.ResourcePropertySource">
        <constructor-arg name="location" value="classpath:conf/sentinel.properties" />
    </bean>

    <!-- jedis 配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="minIdle" value="${redis.minIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
    </bean>
    
    <!-- redis服务器中心 -->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg name="sentinelConfig"
            ref="redisSentinelConfiguration" />
        <constructor-arg name="poolConfig" ref="poolConfig" />
        <property name="port" value="${redis.sentinel.port}" />
        <property name="hostName" value="${redis.sentinel.ip}" />
        <property name="password" value="${redis.sentinel.pwd}" />
        <property name="timeout" value="${redis.timeout}"/>
    </bean>
    
    <bean id="redisTemplate"
        class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="keySerializer">
            <bean
                class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean
                class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
    </bean>

    <!-- cache配置 -->
    <bean id="redisUtil" class="RedisUtil">
        <property name="redisTemplate" ref="redisTemplate" />
    </bean>
  • 应用
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

@Controller
@RequestMapping("/test/")
public class RedistClusterTestController {
    
@Resource(name = "redisUtil")
    RedisUtil redisUtil;
    
    @RequestMapping("redis/cluster")
    @ResponseBody
    public String reqRedistCluster() {
        
        testRedisClusterSSM();
        return "";
    }
    
    public void testRedisClusterSSM() {
        redisUtil.set("XW", "18");
        String name = (String) redisUtil.get("XW");
        System.out.println(name);
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。