springboot 集成redis 哨兵

redis作为缓存中间件, 在数据访问频繁的场景下很是受到开发者的青睐,传统的redis都是单例模式,但是单例很容易出问题,得不到保障,目前市面流行的主要有两个模式,

  • 哨兵模式 sentinel
  • 集群模式 cluster

本文着重讲一下redis的哨兵模式
首先 先配置redis的xml文件,虽然springboot 可以不用xml,但是个人觉得xml的看起来更加简洁明了
本文的前提需要配置disconf,redis sentinel 的相关配置我都放在了disconf上了, disconf 的文章见 springboot 集成 disconf

并且在导入redis配置


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <aop:aspectj-autoproxy proxy-target-class="true"/>

    
    <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
          destroy-method="destroy">
        <property name="scanPackage" value="suamg.service"/>
    bean>
    <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
          init-method="init" destroy-method="destroy">
    bean>

    <bean id="configproperties_disconf"
          class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>smaug_application.propertiesvalue>
                <value>smaug_redis.propertiesvalue>
            list>
        property>
    bean>

    <bean id="propertyConfigurer"
          class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer">
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="propertiesArray">
            <list>
                <ref bean="configproperties_disconf"/>
            list>
        property>
    bean>
    
beans>```

package smaug.util.cache;

/**
 * Created by naonao on 17/8/20.
 */
public interface CacheUtil {
    /**
     * 默认的超时时间
     * */
    long defaultExpiredSeconds = 300;

    /**
     * redis 锁前缀
     * */
    String lockPrefix = "lock:";

    /**
     * get
     * @param key
     * @return
     */
    String get(String key);
}

并且写一下实现这个util的工具类

package smaug.util.cache;

import lombok.Setter;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.retry.RetryContext;
import org.springframework.retry.support.RetryTemplate;
import smaug.util.json.JsonUtil;

/**
 * Created by naonao on 17/8/20.
 */
@Setter
public class RedisTemplateUtil implements CacheUtil {
    private static final JsonUtil jsonUtil = new JsonUtil();


    private StringRedisTemplate stringRedisTemplate;
    private RetryTemplate retryTemplate;

    @Override
    public String get(String key) {
        String result = retryTemplate.execute(
                (RetryContext context) -> {
                    return stringRedisTemplate.opsForValue().get(key);
                },
                context -> {
                    return stringRedisTemplate.opsForValue().get(key);
                }
        );
        return result;
    }
}

当然不要忘记了加载重要的依赖哦,看看我的依赖哈

dependencies {
        compile 'org.springframework.boot:spring-boot-starter:1.5.2.RELEASE'
        compile 'com.alibaba:dubbo:2.8.5-SNAPSHOT'
        compile "org.springframework.boot:spring-boot-starter-jersey:1.5.1.RELEASE"
        compile "com.baidu.disconf:disconf-client:2.6.36"
        compile 'mysql:mysql-connector-java:6.0.6'
        compile 'org.mybatis.generator:mybatis-generator-core:1.3.5'
        compile "com.101tec:zkclient:0.8.1"
        compile 'org.apache.zookeeper:zookeeper:3.4.8'
        compile 'org.projectlombok:lombok:1.16.8'
        compile 'org.springframework:spring-core:4.2.5.RELEASE'
        compile 'org.springframework:spring-context-support:4.2.5.RELEASE'
        compile 'org.springframework:spring-beans:4.2.5.RELEASE'
        compile 'org.springframework:spring-oxm:4.2.5.RELEASE'
        compile "org.springframework:spring-jms:4.2.5.RELEASE"
        compile 'org.springframework.retry:spring-retry:1.1.2.RELEASE'
        compile 'org.springframework:spring-context:4.2.5.RELEASE'
        compile 'org.springframework.data:spring-data-commons-core:1.9.2.RELEASE'
        compile 'org.slf4j:slf4j-api:1.7.15'
        compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.5'
        compile 'org.apache.logging.log4j:log4j-core:2.5'

        compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.9'

        compile 'org.springframework.boot:spring-boot-starter-redis:1.4.3.RELEASE'

        compile 'org.springframework.boot:spring-boot-starter-data-redis:1.5.4.RELEASE'

        testCompile "junit:junit:4.12"
        testCompile 'com.googlecode.thread-weaver:threadweaver:0.2'

    }

然后写一个例子

public ShopRestItem getShopDetail(int shopId, String shopName) {
        Map<String, Object> map = new HashMap<>();
        map.put("shopId", shopId);
        map.put("shopName", shopName);
        ShopEntity entity= shopEntityMapper.selectShopEntity(map);
        String s = cacheUtilSmaugServer.get("mall:order:detail:1026338");
        ShopRestItem item = new ShopRestItem();
        item.setMShopId(entity.getShopid());
        item.setShopName(entity.getShopname());
        return item;
    }

注意 cacheUtilSmaugServer 需要提前注入哦


    @Resource(name = "cacheUtilSmaugServer")
    protected CacheUtil cacheUtilSmaugServer;

你可能感兴趣的:(springboot,redis)