redis+spring
阿新 • • 發佈:2018-06-05
redisredis.properties
#redis連接池配置參數 redis.pool.maxTotal=200 redis.pool.maxIdle=50 redis.pool.minIdle=10 redis.pool.testOnBorrow=true redis.pool.testOnReturn=true redis.pool.fairness=false redis.pool.maxWaitMillis=20000 redis.pool.blockWhenExhausted=true #格式:redis://:[密碼]@[服務器地址]:[端口]/[db index] redis.uri = redis://:[email protected]:6379/0 redis.host = 172.28.1.239 redis.port = 20000 redis.timeout=30000 redis.password = 12345 redis.database = 0
spring-jedis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 引入jedis的properties配置文件 --> <!--如果你有多個數據源需要通過<context:property-placeholder管理,且不願意放在一個配置文件裏,那麽一定要加上ignore-unresolvable=“true"--> <context:property-placeholder location="classpath:redis/redis.properties" ignore-unresolvable="true" /> <!--shardedJedisPool的相關配置--> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.pool.maxTotal}"/> <property name="maxIdle" value="${redis.pool.maxIdle}"/> <property name="minIdle" value="${redis.pool.minIdle}"/> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/> <property name="testOnReturn" value="${redis.pool.testOnReturn}"/> <property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/> <property name="fairness" value="${redis.pool.fairness}"/> <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/> </bean> <!--集群redis配置--> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1"> <list> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg name="host" value="${redis.uri}" /> </bean> </list> </constructor-arg> </bean> <!--單機redis配置--> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="poolConfig" ref="jedisPoolConfig" /> <constructor-arg name="host" value="${redis.host}" /> <constructor-arg name="port" value="${redis.port}" type="int" /> <constructor-arg name="timeout" value="${redis.timeout}" type="int" /> <!--<constructor-arg name="password" value="${redis.password}" />--> <!--<constructor-arg name="database" value="${redis.database}" type="int" />--> </bean> </beans>
測試
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:redis/spring-jedis.xml"); JedisPool jedisPool = (JedisPool) context.getBean("jedisPool"); Jedis jedis = jedisPool.getResource(); String setVal = jedis.set("key123", "value123"); System.out.println(setVal); jedis.del("key123"); // 事物操作 Transaction multi = jedis.multi(); multi.set("key2","val2"); Map<String, String> hashMap = new HashMap<>(); hashMap.put("name","ttest"); hashMap.put("age","123"); multi.hmset("hash1", hashMap); multi.exec(); }
redis+spring