import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * Created by YeCongZhi on 2017/6/21. */ @Configuration public class JedisConfiguation { Logger logger = LoggerFactory.getLogger(JedisConfiguation.class); @Value("${redisA.host:}")//IP private String hostA; @Value("${redisA.port:}")//端口 private int portA; @Value("${redisA.password:}")//密碼 private String passwordA; @Value("${redisA.database:}")//數據庫 private int databaseA; @Value("${redisB.host:}")//IP private String hostB; @Value("${redisB.port:}")//端口 private int portB; @Value("${redisB.password:}")//密碼 private String passwordB; @Value("${redisB.database:}")//數據庫 private int databaseB; @Value("${redis.maxTotal:}")//最大連接數 private int maxTotal; @Value("${redis.maxIdle:}")//最大空閒連接數 private int maxIdle; @Value("${redis.minIdle:}")//最小空閒連接數 private int minIdle; @Value("${redis.maxWaitMillis:}")//獲取連接時的最大等待毫秒數 private Long maxWaitMillis; @Value("${redis.testOnBorrow:false}")//在獲取連接的時候檢查有效性 private boolean testOnBorrow; @Value("${redis.MinEvictableIdleTimeMillis:}")//多長時間後回收空閒連接 private Long MinEvictableIdleTimeMillis; @Bean("JedisPoolA") public JedisPool redisPoolFactoryA() { // logger.info("[redisA服務地址:]" + hostA); // logger.info("[redisA服務端口:]" + portA); // logger.info("[redisA密碼:]" + passwordA); JedisPoolConfig poolCofig = new JedisPoolConfig(); poolCofig.setMaxTotal(maxTotal); poolCofig.setMaxIdle(maxIdle); poolCofig.setMinIdle(minIdle); poolCofig.setMaxWaitMillis(maxWaitMillis); poolCofig.setMinEvictableIdleTimeMillis(MinEvictableIdleTimeMillis); poolCofig.setTestOnBorrow(testOnBorrow); JedisPool jedisPool = new JedisPool(poolCofig, hostA, portA, 0, passwordA); return jedisPool; } @Bean("JedisPoolB") public JedisPool redisPoolFactoryB() { // logger.info("[redisB服務地址:]" + hostB); // logger.info("[redisB服務端口:]" + portB); // logger.info("[redisB密碼:]" + passwordB); JedisPoolConfig poolCofig = new JedisPoolConfig(); poolCofig.setMaxTotal(maxTotal); poolCofig.setMaxIdle(maxIdle); poolCofig.setMinIdle(minIdle); poolCofig.setMaxWaitMillis(maxWaitMillis); poolCofig.setMinEvictableIdleTimeMillis(MinEvictableIdleTimeMillis); poolCofig.setTestOnBorrow(testOnBorrow); JedisPool jedisPool = new JedisPool(poolCofig, hostB, portB, 0, passwordB); return jedisPool; }
}
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; /** * Created by YeCongZhi on 2017/6/21. * 使用示例 @Autowired private JedisClientService jedisClientService; jedisClientService.setValueToA("AAAAA","TFDSFSDF333"); System.out.println(jedisClientService.getValueFromA("AAAAA")); jedisClientService.setValueToB("BBBBB","AAREGRTE33"); System.out.println(jedisClientService.getValueFromB("BBBBB")); */ @Service public class JedisClientService { @Autowired @Qualifier("JedisPoolA") private JedisPool jedisPoolA; @Autowired @Qualifier("JedisPoolB") private JedisPool jedisPoolB; /**獲取key的value值**/ public synchronized String getValueFromA(String key) { /**獲取jedis實例*/ Jedis jedis = jedisPoolA.getResource(); String str = ""; try { str = jedis.get(key); } finally { try { /**close方法 包含兩個方法 * returnBrokenResource * returnResource*/ jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**保存key - velue */ public synchronized String setValueToA(String key, String value) { /**獲取jedis實例*/ Jedis jedis = jedisPoolA.getResource(); String str = ""; try { str = jedis.set(key, value); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**保存key - velue */ public synchronized String setexValueToA(String key, int seconds, String value) { /**獲取jedis實例*/ Jedis jedis = jedisPoolA.getResource(); String str = ""; try { str = jedis.setex(key, seconds, value); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**刪除key**/ public synchronized Long delFromA(String key) { /**獲取jedis實例*/ Jedis jedis = jedisPoolA.getResource(); Long cnt; try { cnt = jedis.del(key); } finally { try { /**close方法 包含兩個方法 * returnBrokenResource * returnResource*/ jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return cnt; } /******************************************************************/ /**獲取key的value值**/ public synchronized String getValueFromB(String key) { /**獲取jedis實例*/ Jedis jedis = jedisPoolB.getResource(); String str = ""; try { str = jedis.get(key); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**保存key - velue */ public synchronized String setValueToB(String key, String value) { /**獲取jedis實例*/ Jedis jedis = jedisPoolB.getResource(); String str = ""; try { str = jedis.set(key, value); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**保存key - velue */ public synchronized String setexValueToB(String key, int seconds, String value) { /**獲取jedis實例*/ Jedis jedis = jedisPoolB.getResource(); String str = ""; try { str = jedis.setex(key, seconds, value); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**刪除key**/ public synchronized Long delFromB(String key) { /**獲取jedis實例*/ Jedis jedis = jedisPoolB.getResource(); Long cnt; try { cnt = jedis.del(key); } finally { try { /**close方法 包含兩個方法 * returnBrokenResource * returnResource*/ jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return cnt; } }