Spring統合Jedisの構成と使用の簡単な例
jedisはredisのjavaクライアントで、springはredis接続プールをbeanとして構成します。
redis接続プールは、「
もう一つは「
maven導入関連パッケージ:
ShardedJeds操作:
以上はこの文章の全部の内容です。本文の内容は皆さんの学習や仕事に対して一定の参考学習価値を持ってほしいです。ありがとうございます。もっと知りたいなら、下のリンクを見てください。
redis接続プールは、「
redis.clients.jedis.ShardedJedisPool
」であり、hashアルゴリズムに基づく分散型クラスタredisクライアント接続プールである。もう一つは「
redis.clients.jedis.JedisPool
」で、これはシングルマシン環境に適したRedis接続池です。maven導入関連パッケージ:
<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
ShardedJedPoolはRedisクラスタクライアントの対象プールであり、彼を通じてShardedJeddJedPoolを操作することができます。以下はShardedJedPoolのxml構成であり、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:properties/redis.properties" ignore-unresolvable="true" />
<!--shardedJedisPool -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- maxTotal, maxActive-->
<property name="maxTotal">
<value>${redis.pool.maxActive}</value>
</property>
<property name="maxIdle">
<value>${redis.pool.maxIdle}</value>
</property>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
</bean>
<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>
</beans>
以下はシングルマシン環境でのRedis接続池の構成です。
<?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:properties/redis.properties" ignore-unresolvable="true" />
<!--Jedis -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- maxTotal, maxActive-->
<property name="maxTotal">
<value>${redis.pool.maxActive}</value>
</property>
<property name="maxIdle">
<value>${redis.pool.maxIdle}</value>
</property>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
</bean>
<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>
対応するクラスパス:properties/redis.properties.xmlは以下の通りです。
#
redis.pool.maxActive=200
# idel
redis.pool.maxIdle=50
redis.pool.minIdle=10
redis.pool.maxWaitMillis=20000
# ,
redis.pool.maxWait=300
# :redis://:[ ]@[ ]:[ ]/[db index]
redis.uri = redis://:[email protected]:6379/0
redis.host = 127.0.0.1
redis.port = 6379
redis.timeout=30000
redis.password = 12345
redis.database = 0
両方の動作コードは同様で、まず接続池に注入し、その後、接続池を介してjedisのインスタンスを取得し、インスタンスオブジェクトを介してredisを動作させる。ShardedJeds操作:
@Autowired
private ShardedJedisPool shardedJedisPool;// ShardedJedisPool
@RequestMapping(value = "/demo_set",method = RequestMethod.GET)
@ResponseBody
public String demo_set(){
// ShardedJedis
ShardedJedis shardJedis = shardedJedisPool.getResource();
//
shardJedis.set("key1","hello jedis");
// ShardedJedis
shardJedis.close();
return "set";
}
@RequestMapping(value = "/demo_get",method = RequestMethod.GET)
@ResponseBody
public String demo_get(){
ShardedJedis shardedJedis = shardedJedisPool.getResource();
//
String result = shardedJedis.get("key1");
shardedJedis.close();
return result;
}
Jedis操作:
@Autowired
private JedisPool jedisPool;// JedisPool
@RequestMapping(value = "/demo_set",method = RequestMethod.GET)
@ResponseBody
public String demo_set(){
// ShardedJedis
Jedis jedis = jedisPool.getResource();
//
jedis.set("key2","hello jedis one");
// ShardedJedis
jedis.close();
return "set";
}
@RequestMapping(value = "/demo_get",method = RequestMethod.GET)
@ResponseBody
public String demo_get(){
Jedis jedis = jedisPool.getResource();
//
String result = jedis.get("key2");
jedis.close();
return result;
}
締め括りをつける以上はこの文章の全部の内容です。本文の内容は皆さんの学習や仕事に対して一定の参考学習価値を持ってほしいです。ありがとうございます。もっと知りたいなら、下のリンクを見てください。