Skip to content

Commit

Permalink
redis slave
Browse files Browse the repository at this point in the history
  • Loading branch information
guosean committed Jun 5, 2014
1 parent fb23233 commit bb61f45
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/utils/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ public class BeanUtils {
*/
private static ApplicationContext springCtx = null;

private static String SPRING_FILE = "config/Context-configure.xml";

private static String JOB_FILE = "config/Quartzjob-configure.xml";
/**
* 初始化Spring配置
*/
static {
//默认只加载memcached
springCtx = new ClassPathXmlApplicationContext(
/*CookieGenConstants.SPRING_FILE*/);
SPRING_FILE);
}

/**
Expand All @@ -37,6 +40,6 @@ public static Object getBean(final String name) {
*/
public static void initJob() {
springCtx = new ClassPathXmlApplicationContext(
new String[]{/*CookieGenConstants.SPRING_FILE,CookieGenConstants.JOB_FILE*/});
new String[]{SPRING_FILE,JOB_FILE});
}
}
22 changes: 18 additions & 4 deletions src/main/resources/config/Redis-configure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,30 @@
<ref bean="poolConfig" />
</constructor-arg>
<!-- host -->
<constructor-arg index="1" value="10.1.251.87" />
<constructor-arg index="1" value="192.168.0.106" />
<!-- port -->
<constructor-arg index="2" value="6379" type="int" />
</bean>
<bean id="slave1" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="192.168.0.106" />
<!-- port -->
<constructor-arg index="1" value="6378" type="int" />
</bean>
<bean id="shardedPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0">
<ref bean="poolConfig" />
</constructor-arg>
<constructor-arg>
<list>
<ref local="slave1" />
</list>
</constructor-arg>
</bean>
<!-- redis服务 -->
<bean id="redisService"
class="com.sean.service.imp.RedisService"
<bean id="redisService" class="com.sean.service.imp.RedisService"
lazy-init="true">
<property name="jedisPool">
<ref local="psma_redisPool" />
<ref local="redisPool" />
</property>
</bean>

Expand Down
15 changes: 0 additions & 15 deletions src/test/java/com/sean/study/redis/TestJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;

/**
* <p> Copyright 200 by Asiainfo-Linkage Software corporation
* <p>All rights reserved.
* <p>版权所有:亚信联创科技(中国)有限公司
* <p>未经本公司许可,不得以任何方式复制或使用本程序任何部分,
* <p>侵权者将受到法律追究。
* @PURPOSE:
* @DESCRIPTION:
* @AUTHOR: guozb
* @DATE:2013-12-15
* @VERSION PSMA
* @SINCE
* @HISTORY: 1.0
*/

public class TestJedis {
JedisPool pool = null;
Jedis cli = null;
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/sean/study/redis/TestSharedJedis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.sean.study.redis;

import org.junit.Test;

import com.utils.BeanUtils;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class TestSharedJedis {
@Test
public void testSlave(){
ShardedJedisPool sp = (ShardedJedisPool) BeanUtils.getBean("shardedPool");
ShardedJedis sj = sp.getResource();
System.out.println(sj.get("key"));
//sj.set("test", "dgs"); //slave只读,不能写
sp.returnResource(sj);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Thu May 29 23:32:41 CST 2014
#Thu Jun 05 23:59:45 CST 2014
version=0.0.1-SNAPSHOT
groupId=com.sean
m2e.projectName=sean-study
Expand Down
2 changes: 1 addition & 1 deletion target/classes/META-INF/maven/com.sean/sean-study/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
22 changes: 18 additions & 4 deletions target/classes/Redis-configure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,30 @@
<ref bean="poolConfig" />
</constructor-arg>
<!-- host -->
<constructor-arg index="1" value="10.1.251.87" />
<constructor-arg index="1" value="192.168.0.106" />
<!-- port -->
<constructor-arg index="2" value="6379" type="int" />
</bean>
<bean id="slave1" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="192.168.0.106" />
<!-- port -->
<constructor-arg index="1" value="6378" type="int" />
</bean>
<bean id="shardedPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0">
<ref bean="poolConfig" />
</constructor-arg>
<constructor-arg>
<list>
<ref local="slave1" />
</list>
</constructor-arg>
</bean>
<!-- redis服务 -->
<bean id="redisService"
class="com.sean.service.imp.RedisService"
<bean id="redisService" class="com.sean.service.imp.RedisService"
lazy-init="true">
<property name="jedisPool">
<ref local="psma_redisPool" />
<ref local="redisPool" />
</property>
</bean>

Expand Down
Binary file modified target/classes/com/sean/service/imp/RedisService.class
Binary file not shown.
Binary file modified target/classes/com/utils/BeanUtils.class
Binary file not shown.
22 changes: 18 additions & 4 deletions target/classes/config/Redis-configure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,30 @@
<ref bean="poolConfig" />
</constructor-arg>
<!-- host -->
<constructor-arg index="1" value="10.1.251.87" />
<constructor-arg index="1" value="192.168.0.106" />
<!-- port -->
<constructor-arg index="2" value="6379" type="int" />
</bean>
<bean id="slave1" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="192.168.0.106" />
<!-- port -->
<constructor-arg index="1" value="6378" type="int" />
</bean>
<bean id="shardedPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0">
<ref bean="poolConfig" />
</constructor-arg>
<constructor-arg>
<list>
<ref local="slave1" />
</list>
</constructor-arg>
</bean>
<!-- redis服务 -->
<bean id="redisService"
class="com.sean.service.imp.RedisService"
<bean id="redisService" class="com.sean.service.imp.RedisService"
lazy-init="true">
<property name="jedisPool">
<ref local="psma_redisPool" />
<ref local="redisPool" />
</property>
</bean>

Expand Down
Binary file modified target/test-classes/com/sean/study/redis/TestJedis$1.class
Binary file not shown.
Binary file modified target/test-classes/com/sean/study/redis/TestJedis.class
Binary file not shown.
Binary file not shown.

0 comments on commit bb61f45

Please sign in to comment.