spring boot集成redis集群

首先创建springboot项目

1.使用spring initializr创建spring boot项目 (具体创建略过...)


2.搭建好spring boot 以后再resources目录下创建redis.properties文件

#redis集群配置
xyy.redis.pool.nodes=192.168.134.2:7000,192.168.134.2:7001,192.168.134.2:7002,192.168.134.2:7003,192.168.134.2:7004,192.168.134.2:7005,192.168.134.2:7006
xyy.redis.pool.timeout=3000
xyy.redis.pool.maxAttempts=5

3.写redis配置文件类

package com.baizhi.szq.util;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * redis集群配置文件
 * @ClassName: RedisProperties
 * @author suziqing
 */
@Component
@ConfigurationProperties(prefix = "xyy.redis.pool")
@PropertySource("classpath:redis.properties")
public class RedisProperties {
    /** redis集群节点 */
    private String nodes;
    /** 连接超时时间 */
    private int timeout;
    /** 重连次数 */
    private int maxAttempts;

    public RedisProperties() {
    }
    public RedisProperties(String nodes, int timeout, int maxAttempts) {
        this.nodes = nodes;
        this.timeout = timeout;
        this.maxAttempts = maxAttempts;
    }

    public String getNodes() {
        return nodes;
    }

    public void setNodes(String nodes) {
        this.nodes = nodes;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public int getMaxAttempts() {
        return maxAttempts;
    }

    public void setMaxAttempts(int maxAttempts) {
        this.maxAttempts = maxAttempts;
    }

    @Override
    public String toString() {
        return "RedisProperties{" +
                "nodes='" + nodes + '\'' +
                ", timeout=" + timeout +
                ", maxAttempts=" + maxAttempts +
                '}';
    }
}

4.生成jediscluster对象,操作redis集群

package com.baizhi.szq.util;

import redis.clients.jedis.HostAndPort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * 生成JedisCluster对象
 * @ClassName: JedisClusterConfig
 * @author suziqing
 * @date 2018年7月10日18:07:04
 */
@Configuration
public class JedisClusterConfig {
    @Autowired
    private RedisProperties redisProperties;

    /**
     * 注意:
     * 这里返回的JedisCluster是单例的,并且可以直接注入到其他类中去使用
     * @return
     */
    @Bean
    public JedisCluster getJedisCluster() {
        String[] serverArray = redisProperties.getNodes().split(",");//获取服务器数组(这里要相信自己的输入,所以没有考虑空指针问题)
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();

        for (String ipPort : serverArray) {
            String[] ipPortPair = ipPort.split(":");
            nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
        }

        return new JedisCluster(nodes, redisProperties.getTimeout(),redisProperties.getMaxAttempts());
    }
}

5.写测试redis集群的使用

package com.baizhi.szq.test;

import com.baizhi.szq.util.JedisClusterConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.JedisCluster;

import java.util.List;

/**
 * @describe 测试redist集群
 * @author  suziqing0810@163.com
 * @date  2018/7/10 20:11
 **/

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RedisTest {

    @Autowired
    private JedisClusterConfig jedisClusterConfig;

    /**
     * 测试redis集群是否正常调用
     */
    @Test
    public void testJedis(){

        System.out.println("进入");

        JedisCluster jedisCluster = jedisClusterConfig.getJedisCluster();
        //String name = jedisCluster.get("name");
        //System.out.println("名字="+name);
        //jedisCluster.set("yanzhenma","87878");
        //String yanzhenma = jedisCluster.get("yanzhenma");
        //System.out.println("验证码="+yanzhenma);
        //jedisCluster.lpush("list","张三","王五","李四","小黑");
        //jedisCluster.rpush("list","小娜");
        List<String> list = jedisCluster.lrange("list", 0, -1);
        for (String s : list) {
            System.out.println(s);
        }
    }

}

6.注意application必须是测试启动类。

package com.baizhi.szq;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
//@SpringBootApplication
public class DemotestApplicationTests {

	@Test
	public void contextLoads() {
	}
	public static void main(String[] args) {
		SpringApplication.run(DemotestApplication.class, args);
	}

}
7.然后启动springboot项目就可以了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值