Spring Data Redis 连接Redis 集群

从Resources 下 创建一个 cluster.xml 文件

配置

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

    <!-- JedisPoolConfig
        配置连接池  连接数最大30
    -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="30"/>
    </bean>
    <!-- 配置6个 RedisNode
        一个 node 就是一个主机
        构造器的参数 分别为 host主机 和 post端口
    -->
    <bean id="node8001" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg name="host" value="192.168.132.139"/>
        <constructor-arg name="port" value="8001"/>
    </bean>
    <bean id="node8002" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg name="host" value="192.168.132.139"/>
        <constructor-arg name="port" value="8002"/>
    </bean>
    <bean id="node8003" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg name="host" value="192.168.132.139"/>
        <constructor-arg name="port" value="8003"/>
    </bean>
    <bean id="node8004" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg name="host" value="192.168.132.139"/>
        <constructor-arg name="port" value="8004"/>
    </bean>
    <bean id="node8005" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg name="host" value="192.168.132.139"/>
        <constructor-arg name="port" value="8005"/>
    </bean>
    <bean id="node8006" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg name="host" value="192.168.132.139"/>
        <constructor-arg name="port" value="8006"/>
    </bean>
    <!-- 配置 RedisClusterConfigguration
        配置集群 引用构造出来的主机
    -->
    <bean id="clusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <property name="clusterNodes">
            <set>
                <ref bean="node8001"/>
                <ref bean="node8002"/>
                <ref bean="node8003"/>
                <ref bean="node8004"/>
                <ref bean="node8005"/>
                <ref bean="node8006"/>
            </set>
        </property>
    </bean>
    <!-- 配置JedisConnctionFactory
        创建jedis 的链接工厂
        构造器的参数分别引用 集群 和 连接池
    -->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg name="clusterConfig" ref="clusterConfiguration"/>
        <constructor-arg name="poolConfig" ref="poolConfig"/>
    </bean>
    <!-- 配置StringRedisTemplate
        创建模板引用 链接工厂
     -->
    <bean  class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>
</beans>

package com.etoak;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;

public class ClusterTest {
    public static void main(String[] args) {
        /*
        * ApplicationContext spring 容器 为应用程序提供的中央接口,引用 cluster.xml中的bean配置
        * */
        ApplicationContext ioc = new ClassPathXmlApplicationContext("cluster.xml");
        /* 得到spring 容器中的 Redis 模板 */
        StringRedisTemplate template = ioc.getBean(StringRedisTemplate.class);

        //zset
        template.opsForZSet().add("set3","数学",100);
        template.opsForZSet().add("set3","语文",110);
        template.opsForZSet().add("set3","英语",90);

        //zrange
        template.opsForZSet().range("set3",0,-1).forEach(x -> System.out.println(x));
        System.out.println("=======================");
        template.opsForZSet().reverseRangeWithScores("set3",0,-1).forEach(tuple ->{
            System.out.println(tuple.getValue() + ":"+ tuple.getScore());
        });
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值