配置方式

redis的部署方式有单节点部署、哨兵方式部署、集群方式部署3种方式

各种配置方式可以去看xsd文件:redisson-1.1.xsd

<xsd:element name="single-server"> // 单节点
<xsd:element name="sentinel-servers"> // 哨兵
<xsd:element name="cluster-servers"> // 集群
<xsd:element name="master-slave-servers"> // 主从

等其他方式

单节点配置standalone

单纯的java代码,基于单节点部署为了保证数据的备份,一般会添加一个节点作为slave来备份master节点上的数据

//创建配置  
Config config = new Config();  
//指定使用单节点部署方式  
config.useSingleServer().setAddress("redis://127.0.0.1:6379");  
//创建客户端(发现这一非常耗时,基本在2秒-4秒左右)  
RedissonClient redisson = Redisson.create(config);  
  
//首先获取redis中的key-value对象,key不存在没关系  
RBucket<String> keyObject = redisson.getBucket("key");  
//如果key存在,就设置key的值为新值value  
//如果key不存在,就设置key的值为value  
keyObject.set("value");  
  
//最后关闭RedissonClient  
redisson.shutdown(); 

spring整合pom

<!--redisson-->  
<dependency>  
    <groupId>org.redisson</groupId>  
    <artifactId>redisson</artifactId>  
    <version>2.10.4</version>  
</dependency> 
<!--redis-->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>1.7.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.1.RELEASE</version>
</dependency>

spring整合redisson.xml

    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:redisson="http://redisson.org/schema/redisson"  
        xsi:schemaLocation="  
           http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context.xsd  
           http://redisson.org/schema/redisson  
           http://redisson.org/schema/redisson/redisson.xsd">  
        <redisson:client id="standalone" name="aliasName1,aliasName2">  
            <redisson:single-server address="redis://127.0.0.1:6379" />  
        </redisson:client>  
    </beans>  

哨兵配置sentinel

纯java代码

//创建配置  
Config config = new Config();  
//指定使用哨兵部署方式  
config.useSentinelServers()  
    //设置sentinel.conf配置里的sentinel别名  
    //比如sentinel.conf里配置为sentinel monitor my-sentinel-name 127.0.0.1 6379 2,那么这里就配置my-sentinel-name  
    .setMasterName("my-sentinel-name")  
    //这里设置sentinel节点的服务IP和端口,sentinel是采用Paxos拜占庭协议,一般sentinel至少3个节点  
    //记住这里不是配置redis节点的服务端口和IP,sentinel会自己把请求转发给后面monitor的redis节点  
    .addSentinelAddress("redis://127.0.0.1:26379")  
    .addSentinelAddress("redis://127.0.0.1:26389")  
    .addSentinelAddress("redis://127.0.0.1:26399");  
  
//创建客户端(发现这一非常耗时,基本在2秒-4秒左右)  
RedissonClient redisson = Redisson.create(config);  
//首先获取redis中的key-value对象,key不存在没关系  
RBucket<String> keyObject = redisson.getBucket("key");  
//如果key存在,就设置key的值为新值value  
//如果key不存在,就设置key的值为value  
keyObject.set("value");  
  
//最后关闭RedissonClient  
redisson.shutdown(); 

spring整合redis和redisson的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans" xmlns:redisson="http://redisson.org/schema/redisson"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://redisson.org/schema/redisson http://redisson.org/schema/redisson/redisson.xsd">

    <!-- 构造JedisConnectionFactory实例,注入池配置poolConfig和哨兵配置sentinelConfig-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!--<constructor-arg index="0" ref="redisSentinelConfiguration"/>-->
        <!--<constructor-arg index="1" ref="jedisPoolConfig"/>-->
        <constructor-arg ref="redisSentinelConfiguration"/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
        <property name="database" value="${redis.database}"/>
    </bean>

    <!-- 配置JedisPoolConfig-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="minIdle" value="${redis.minIdle}"/>
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        <property name="testWhileIdle" value="${redis.testWhileIdle}"/>
        <property name="testOnReturn" value="${redis.testOnReturn}"/>
        <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/>
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
    </bean>

    <!-- 配置RedisSentinelConfiguration-->
    <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <property name="master">
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <property name="name" value="${redis.masterName}"/>
            </bean>
        </property>
        <property name="sentinels">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg index="0" value="${redis.host}"/>
                    <constructor-arg index="1" value="${redis.port}"/>
                </bean>
            </set>
        </property>
    </bean>

    <!-- 用于缓存对象-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <!-- key -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <!-- value -->
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>

    <!-- 用于缓存字符串-->
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
    </bean>

    <redisson:client id="redissonClient">
        <!--master-name为redis的主节点名称-->
        <redisson:sentinel-servers master-name="mymaster">
            <!-- 可以配置多个 -->
            <redisson:sentinel-address value="redis://172.17.46.11:26379" />
        </redisson:sentinel-servers>
    </redisson:client>

</beans>

使用

    @Autowired
    @Lazy
    private RedissonClient redissonClient; 


   /**
     * @param key 具体键值
     * @return 锁对象
     */
    public RReadWriteLock getLock(String key) {
        return redissonClient.getReadWriteLock(key);
    }

集群配置cluster

纯java代码

//创建配置  
Config config = new Config();  
//指定使用集群部署方式  
config.useClusterServers()  
    // 集群状态扫描间隔时间,单位是毫秒  
    .setScanInterval(2000)   
    //cluster方式至少6个节点(3主3从,3主做sharding,3从用来保证主宕机后可以高可用)  
    .addNodeAddress("redis://127.0.0.1:6379" )  
    .addNodeAddress("redis://127.0.0.1:6380")  
    .addNodeAddress("redis://127.0.0.1:6381")  
    .addNodeAddress("redis://127.0.0.1:6382")  
    .addNodeAddress("redis://127.0.0.1:6383")  
    .addNodeAddress("redis://127.0.0.1:6384");  
//创建客户端(发现这一非常耗时,基本在2秒-4秒左右)  
RedissonClient redisson = Redisson.create(config);  
  
//首先获取redis中的key-value对象,key不存在没关系  
RBucket<String> keyObject = redisson.getBucket("key");  
//如果key存在,就设置key的值为新值value  
//如果key不存在,就设置key的值为value  
keyObject.set("value");  
  
//最后关闭RedissonClient  
redisson.shutdown();

spring整合redisson配置方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:redisson="http://redisson.org/schema/redisson"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://redisson.org/schema/redisson http://redisson.org/schema/redisson/redisson.xsd">

    <!--  Redis 连接池配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="minIdle" value="${redis.minIdle}"/>
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        <property name="testWhileIdle" value="${redis.testWhileIdle}"/>
        <property name="testOnReturn" value="${redis.testOnReturn}"/>

        <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/>
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
        <property name="timeBetweenEvictionRunsMillis" value="30000"/>
    </bean>

    <!-- jedisCluster 配置 -->
    <!-- 自定义的工厂类,实现了FactoryBean<JedisCluster>接口 -->
    <bean id="jedisCluster" class="com.xxxxx.JedisClusterFactory">
        <!-- address: 集群配置 -->
        <property name="address" value="${redisColony.host}"/>
        <!-- timeout: 连接超时时间 -->
        <property name="timeout" value="100000"/>
        <!-- maxRedirections: 最大重定向 -->
        <property name="maxRedirections" value="5"/>
        <!-- 连接池 -->
        <property name="genericObjectPoolConfig" ref="poolConfig"/>
    </bean>

    <!--redisson的实例 -->
    <redisson:client id="redissonClient">
        <redisson:cluster-servers>
            <!-- 节点地址同redis集群的节点 -->
            <redisson:node-address value="${redisColony.host1}"/>
            <redisson:node-address value="${redisColony.host2}"/>
            <redisson:node-address value="${redisColony.host3}"/>
            <redisson:node-address value="${redisColony.host4}"/>
            <redisson:node-address value="${redisColony.host5}"/>
            <redisson:node-address value="${redisColony.host6}"/>
        </redisson:cluster-servers>
    </redisson:client>


</beans>

properties文件

redisColony.host = 172.17.46.11:36379,172.17.46.11:36381,172.17.45.12:36379,172.17.45.12:36381,172.17.45.13:36379,172.17.45.13:36381
redis.timeOut = 100000
redis.redirection = 5

redisColony.host1 = redis://172.17.46.11:36379
redisColony.host2 = redis://172.17.46.11:36381
redisColony.host3 = redis://172.17.45.12:36379
redisColony.host4 = redis://172.17.45.12:36381
redisColony.host5 = redis://172.17.45.13:36379
redisColony.host6 = redis://172.17.45.13:36381

使用方式与哨兵配置方式相同,@Autowired注入即可

主从部署方式(master/slave)

纯java代码

    //创建配置  
    Config config = new Config();  
    //指定使用主从部署方式  
    config.useMasterSlaveServers()  
        //设置redis主节点  
        .setMasterAddress("redis://127.0.0.1:6379")  
        //设置redis从节点  
        .addSlaveAddress("redis://127.0.0.1:6380", "redis://127.0.0.1:6381");  
    //创建客户端(发现这一非常耗时,基本在2秒-4秒左右)  
    RedissonClient redisson = Redisson.create(config);  
      
    //首先获取redis中的key-value对象,key不存在没关系  
    RBucket<String> keyObject = redisson.getBucket("key");  
    //如果key存在,就设置key的值为新值value  
    //如果key不存在,就设置key的值为value  
    keyObject.set("value");  
      
    //最后关闭RedissonClient  
    redisson.shutdown();  

spring整合redisson.xml

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:redisson="http://redisson.org/schema/redisson"  
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans.xsd  
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context.xsd  
       http://redisson.org/schema/redisson  
       http://redisson.org/schema/redisson/redisson.xsd">  
    <redisson:client id="masterSlave">  
        <redisson:master-slave-servers master-address="redis://127.0.0.1:6379">  
            <redisson:slave-address value="redis://127.0.0.1:6380" />  
            <redisson:slave-address value="redis://127.0.0.1:6381" />  
        </redisson:master-slave-servers>  
    </redisson:client>  
</beans>

 

GitHub 加速计划 / re / redisson
23.06 K
5.31 K
下载
Redisson - Easy Redis Java client with features of In-Memory Data Grid. Sync/Async/RxJava/Reactive API. Over 50 Redis based Java objects and services: Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Spring Cache, Tomcat, Scheduler, JCache API, Hibernate, RPC, local cache ...
最近提交(Master分支:2 个月前 )
15bd94ed - 3 个月前
d220f2a8 - 3 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐