redis cluster+spring boot

1.在centos上进行redis集群部署。

 参考我前面的一片文章《Redis集群搭建与简单使用

2.springBoot架构整合

①springBoot架构创建 

这里不做过多描述,推荐使用idea一键创建

 ②pom文件增加依赖。


    org.springframework.boot
    spring-boot-starter-data-redis



    redis.clients
    jedis
    2.9.0


③.配置application.yml或application.properties配置文件

spring:
    redis:
       cache.clusterNodes:

192.168.92.100:7000,192.168.92.100:7001,192.168.92.100:7002,192.168.92.101:7003,192.168.92.101:7004,192.168.92.101:7005

       cache.commandTimeout: 5000


④.创建RedisProperties装载配置到对象

    

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


@Component
@ConfigurationProperties(prefix = "spring.redis.cache")
public class RedisProperties {
    private int expireSeconds;
    private String clusterNodes;
    private int commandTimeout;

    public int getExpireSeconds() {
        return expireSeconds;
    }

    public void setExpireSeconds(int expireSeconds) {
        this.expireSeconds = expireSeconds;
    }

    public String getClusterNodes() {
        return clusterNodes;
    }

    public void setClusterNodes(String clusterNodes) {
        this.clusterNodes = clusterNodes;
    }

    public int getCommandTimeout() {
        return commandTimeout;
    }

    public void setCommandTimeout(int commandTimeout) {
        this.commandTimeout = commandTimeout;
    }
}


⑤.创建JedisClusterConfig读取读取配置信息

 

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

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


@Configuration
public class JedisClusterConfig {
    @Autowired
    private RedisProperties redisProperties;

    public JedisCluster getJedisCluster(){
        String [] serverArray=redisProperties.getClusterNodes().split(",");
        Set nodes=new HashSet<>();

        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.getCommandTimeout());
    }

}


⑥.创建RedisClientTemplate接口进行set  get测试  这里简单写了两个方法,自行修改

    

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class RedisClientTemplate {
    private static final Logger log=LoggerFactory.getLogger(RedisClientTemplate.class);

    @Autowired
    private JedisClusterConfig jedisClusterConfig;

    public boolean setToRedis(String key,Object value){
        try {
        String str=jedisClusterConfig.getJedisCluster().set(key, String.valueOf(value));
        if("OK".equals(str))
            return true;
        }catch (Exception ex){
            log.error("setToRedis:{Key:"+key+",value"+value+"}",ex);
        }
        return false;
    }

    public Object getRedis(String key){
        String str=null;
        try {
             str=jedisClusterConfig.getJedisCluster().get(key);
        }catch (Exception ex){
            log.error("getRedis:{Key:"+key+"}",ex);
        }
        return str;
    }
    
}


到这里redis集群搭建好了,代码配置也都弄好了,现在创建一个controller测试!

 

import com.springboot.buying.snapup.utils.redis.RedisClientTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    RedisClientTemplate redisClientTemplate;

    @GetMapping(value = "/testSet")
    public Object testSet(){
        redisClientTemplate.setToRedis("Frank","Frank测试redis");
        System.out.println(redisClientTemplate.getRedis("Frank"));
        return null;
    }

}

 

到这里springBoot整合redisCluster集群模式就完成了

你可能感兴趣的:(2.2Redis)