spring集成redis客户端jedis以及redisson

2023-11-18 09:48:27 浏览数 (1)

集成jedis实例,xml方式

集成前引用的jar

<!--kl add redis client--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-pool/commons-pool --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.2</version> </dependency> spring bean配置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"        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">     <!-- POOL配置 -->     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">         <property name="maxTotal" value="300"/>         <property name="maxIdle" value="10"/>         <property name="maxWaitMillis" value="1000"/>         <property name="testOnBorrow" value="true"/>     </bean>     <!-- jedis shard信息配置 -->     <bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo">         <constructor-arg index="0" value="${redis.host}"/>         <constructor-arg index="1" value="${redis.port}" type="int"/>     </bean>     <!-- jedis shard pool配置 -->     <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">         <constructor-arg index="0" ref="jedisPoolConfig"/>         <constructor-arg index="1">             <list>                 <ref bean="jedisShardInfo"/>             </list>         </constructor-arg>     </bean>     <bean id="shardedJedis" factory-bean="shardedJedisPool" factory-method="getResource" /> </beans> 集成redisson实例,java bean的方式

集成前引入的jar

<!--kl add redis client--> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>2.5.0</version> </dependency>

javabean配置如下

代码语言:javascript复制
/**
 * Created by kl on 2016/10/21.
 */
@Configuration
@ComponentScan
public class RedsissonConfig {
    @Bean(destroyMethod="shutdown")
    RedissonClient redisson(@Value("classpath:/conf/redisson.yaml") Resource configFile) throws IOException {
        Config config = Config.fromYAML(configFile.getInputStream());
        return Redisson.create(config);
    }
}

0 人点赞