Redis进阶-Jedis以及Spring Boot操作 Redis 5.x Cluster

在这里插入图片描述

Pre

Redis进阶-5.x 单节点 及Redis Cluster 3主3从集群部署搭建了Redis Cluster 集群,接下来我们看下如何使用Java 代码来操作集群


Jedis操作Redis Cluster

添加依赖

   <dependency>
       <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
 </dependency>

Code


import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;


public class JedisClusterDemo {
    public static void main(String[] args) throws IOException {

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(20);
        config.setMaxIdle(10);
        config.setMinIdle(5);

        Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
        jedisClusterNode.add(new HostAndPort("192.168.18.131", 8001));
        jedisClusterNode.add(new HostAndPort("192.168.18.131", 8004));
        jedisClusterNode.add(new HostAndPort("192.168.18.132", 8002));
        jedisClusterNode.add(new HostAndPort("192.168.18.132", 8005));
        jedisClusterNode.add(new HostAndPort("192.168.18.133", 8003));
        jedisClusterNode.add(new HostAndPort("192.168.18.133", 8006));

        JedisCluster jedisCluster = null;
        try {
            //connectionTimeout:指的是连接一个url的连接等待时间
            //soTimeout:指的是连接上一个url,获取response的返回等待时间
            jedisCluster = new JedisCluster(jedisClusterNode, 6000, 5000, 10, "artisan", config);
            System.out.println(jedisCluster.set("clusterArtisan", "artisanValue"));
            System.out.println(jedisCluster.get("clusterArtisan"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedisCluster != null)
                jedisCluster.close();
        }
    }
}

运行

在这里插入图片描述

去redis中查看下

在这里插入图片描述


Spring Boot 操作Redis Cluster

引入 依赖

 <dependency>
`	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
</dependency>

application.yml

server:
  port: 8080

spring:
  redis:
    database: 0
    timeout: 3000
    password: artisan
      #  sentinel:    #哨兵模式
      #  master: mymaster #主服务器所在集群名称
      #  nodes: 192.168.18.131:26379,192.168.18.132:26379,192.168.18.133:26379
    cluster:
      nodes: 192.168.18.131:8001, 192.168.18.131:8004,  192.168.18.132:8002 ,192.168.18.132:8005, 192.168.18.133:8003 ,192.168.18.133:8006
    lettuce:
      pool:
        max-idle: 50
        min-idle: 10
        max-active: 100
        max-wait: 1000




Code


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 测试节点挂了哨兵重新选举新的master节点,客户端是否能动态感知到
     *
     * @throws InterruptedException
     */
    @RequestMapping("/test_sentinel")
    public void testSentinel() throws InterruptedException {
        int i = 1;
        while (true) {
            try {
                stringRedisTemplate.opsForValue().set("art" + i, i + ""); //jedis.set(key,value);
                System.out.println("设置key:" + "zhuge" + i);
                i++;
                Thread.sleep(1000);
            } catch (Exception e) {
                logger.error("错误:", e);
            }
        }
    }

    @RequestMapping("/test_cluster")
    public void testCluster() throws InterruptedException {
        stringRedisTemplate.opsForValue().set("SmartArtisan" , "ClusterArtisanValue"); //jedis.set(key,value);
        System.out.println("设置key:" + "SmartArtisan" );
        String value = stringRedisTemplate.opsForValue().get("SmartArtisan");
        System.out.println("SmartArtisan 对应的Value:" + value);
    }
}

访问 http://127.0.0.1:8080/test_cluster

在这里插入图片描述
在redis上查看一下
在这里插入图片描述

发布了837 篇原创文章 · 获赞 2088 · 访问量 425万+

猜你喜欢

转载自blog.csdn.net/yangshangwei/article/details/105467574