Redis在開發中的使用Jedis:單機版和集羣版

0、添加依賴

        <!-- Redis客戶端 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

一、配置文件:applicationContext-redis.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" 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-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<!-- 連接redis單機版 -->
	<!-- 
	<bean id="jedisClientPool" class="com.e3mall.common.jedis.JedisClientPool">
		<property name="jedisPool" ref="jedisPool"></property>
	</bean>
	<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="host" value="192.168.25.130" />
		<constructor-arg name="port" value="6379" />
	</bean>
	 -->

	<!-- 連接redis集羣版 -->
	<bean id="jedisClientCluster" class="com.e3mall.common.jedis.JedisClientCluster">
		<property name="jedisCluster" ref="jedisCluster"></property>
	</bean>
	<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
		<constructor-arg name="nodes">
			<set>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.130" />
					<constructor-arg name="port" value="7001" />
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.130" />
					<constructor-arg name="port" value="7002" />
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.130" />
					<constructor-arg name="port" value="7003" />
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.130" />
					<constructor-arg name="port" value="7004" />
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.130" />
					<constructor-arg name="port" value="7005" />
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.130" />
					<constructor-arg name="port" value="7006" />
				</bean>
			</set>
		</constructor-arg>
	</bean>

</beans>

二、使用步驟:

package com.e3mall.jedis;

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

import org.junit.Test;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

public class TestJedis {
    // 單機版Redis
    @Test
    public void testJedis() {
        // 創建一個連接jedis對象 參數 host,port
        Jedis jedis = new Jedis("192.168.25.130", 6379);
        // 直接使用jedis操作redis。所有jedis命令都對應一個方法
        jedis.set("testJedis", "Hello my first Jedis");
        String string = jedis.get("testJedis");
        System.out.println(string);
        // 關閉jedis
        jedis.close();
    }

    // 單機版Redis 使用連接池單機版的Redis
    @Test
    public void testJedisPool() {
        // 創建連接池對象JedisPool;參數 host,port
        JedisPool jedisPool = new JedisPool("192.168.25.130", 6379);
        // 從連接池中獲取一個連接,就是一個jedis對象
        Jedis jedis = jedisPool.getResource();
        // 使用jedis操作redis
        String string = jedis.get("testJedis");
        System.out.println(string);
        // 關閉連接,每次使用完畢後關閉連接。連接池回收資源
        jedis.close();
        // 關閉連接池jedisPool
        jedisPool.close();
    }

    // 集羣版Redis
    @Test
    public void testJedisCluster() throws Exception {
        // 創建一個JedisCluster對象,有一個參數nodes是一個Set類型。Set中包含若干個HostAndPort對象
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();
        nodes.add(new HostAndPort("192.168.25.130", 7001));
        nodes.add(new HostAndPort("192.168.25.130", 7002));
        nodes.add(new HostAndPort("192.168.25.130", 7003));
        nodes.add(new HostAndPort("192.168.25.130", 7004));
        nodes.add(new HostAndPort("192.168.25.130", 7005));
        nodes.add(new HostAndPort("192.168.25.130", 7006));
        JedisCluster jedisCluster = new JedisCluster(nodes);
        // 直接使用JedisCluster對象操作redis
        jedisCluster.set("testJedisCluster", "Hello My Frist JedisCluster");
        String string = jedisCluster.get("testJedisCluster");
        System.out.println(string);
        // 關閉JedisCluster對象
        jedisCluster.close();
    }
}



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章