JAVA连接Redis客户端多种方式实现

Jedis介绍
Redis不仅使用命令来操作,而且可以使用程序客户端操作。现在基本上主流的语言都有客户端支持,比如java、C、C#、C++、php、Node.js、Go等。
在官方网站里列一些Java的客户端,有Jedis、Redisson、Jredis、JDBC-Redis、等其中官方推荐使用Jedis和Redisson。 
Jedis同样也是托管在github上,地址:https://github.com/xetorthio/jedis
 

 
        
            redis.clients
            jedis
            2.9.0
        
        
            org.springframework
            spring-context
            5.0.7.RELEASE
        
        
            org.springframework
            spring-test
            5.0.7.RELEASE
        
        
        
            junit
            junit
            4.12
        
    


    
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.2
                
                    1.8
                    1.8
                    UTF-8
                
            
        
    

单实例连接
 

 @Test
    public void testJedis() {
        //创建一个Jedis的连接
        Jedis jedis = new Jedis("10.28.184.25", 6379);
        //执行redis命令
        jedis.set("mytest", "hello world, this is jedis client!");
        //从redis中取值
        String result = jedis.get("mytest");
        //打印结果
        System.out.println(result);
        //关闭连接
        jedis.close();
    }

连接池连接
 

 @Test
    public void testJedisPool() {
        //创建一连接池对象
        JedisPool jedisPool = new JedisPool("10.28.184.25", 6379);
        //从连接池中获得连接
        Jedis jedis = jedisPool.getResource();
        String result = jedis.get("mytest") ;
        System.out.println(result);
        //关闭连接
        jedis.close();
        //关闭连接池
        jedisPool.close();
    }

连接redis集群
 

@Test
    public void testJedisCluster() throws Exception {
        //创建一连接,JedisCluster对象,在系统中是单例存在
        Set nodes = new HashSet<>();
        nodes.add(new HostAndPort("10.28.184.25", 7001));
        nodes.add(new HostAndPort("10.28.184.25", 7002));
        nodes.add(new HostAndPort("10.28.184.25", 7003));
        nodes.add(new HostAndPort("10.28.184.25", 7004));
        JedisCluster cluster = new JedisCluster(nodes);
        //执行JedisCluster对象中的方法,方法和redis一一对应。
        cluster.set("cluster-test", "my jedis cluster test");
        String result = cluster.get("cluster-test");
        System.out.println(result);
        //程序结束时需要关闭JedisCluster对象
        cluster.close();
    }

Jedis整合spring
配置spring配置文件applicationContext.xml
 



    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
        
        
        
    
    
    
        
            
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
            

        
        
    

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;

/**
 * @author wangbh
 * @Description: test1
 * @date 2021/12/8 10:00
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application.xml")
public class TestJedis2 {
    @Autowired
    private JedisPool jedisPool;
    @Resource
    private JedisCluster cluster;
    @Test
    public void testJedisPool() {
        // 从连接池中获得连接
        Jedis jedis = jedisPool.getResource();
        String result = jedis.get("mytest");
        System.out.println(result);
        // 关闭连接
        jedis.close();
    }
    @Test
    public void testJedisCluster() throws Exception {
        // 执行JedisCluster对象中的方法,方法和redis一一对应。
        cluster.set("cluster-test", "my jedis cluster test");
        String result = cluster.get("cluster-test");
        System.out.println(result);
    }
}
Redisson方式
package com;

import org.redisson.Redisson;
import org.redisson.config.Config;

/**
 * @author wangbh
 * @Description: test
 * @date 2021/8/19 10:44
 */
public class RedissonManager {
    private static Config config = new Config();
    //声明redisso对象
    private static Redisson redisson = null;

    //实例化redisson
    static {
        //单个
        config.useSingleServer().setPassword("!QAZxsw2#EDC(0Ol1)")
                .setAddress("192.168.1.239:6379").setDatabase(2);
        //        config.useClusterServers()
         集群状态扫描间隔时间,单位是毫秒
        //                .setScanInterval(2000)
        cluster方式至少6个节点(3主3从,3主做sharding,3从用来保证主宕机后可以高可用)
        //                .addNodeAddress("192.168.1.239:6379").setPassword("!QAZxsw2#EDC(0Ol1)");
        //得到redisson对象
        redisson = (Redisson) Redisson.create(config);
    }

    //获取redisson对象的方法
    public static Redisson getRedisson() {
        return redisson;
    }
}

你可能感兴趣的:(redis,redis,java)