【单台redis】spring-data-redis 2.x配置redisTemplate(包括连接池、超时设置等)

单台redis服务器配置

注:假如有多台redis服务器,可以进行redis哨兵配置,直接参考 https://blog.csdn.net/bigtree_3721/article/details/72801414

单机配置原理是利用如下构造器

public JedisConnectionFactory(RedisStandaloneConfiguration standaloneConfig, JedisClientConfiguration clientConfig)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>RedisTest</groupId>
    <artifactId>RedisTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
    
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>
        
		<!-- https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305-->
        <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>jsr305</artifactId>
            <version>3.0.2</version>
        </dependency>


    </dependencies>
</project>

redis.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">

    <!--redis连接工厂-->
    <bean id="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
        <property name="maxIdle" value="50" />
        <property name="maxTotal" value="100" />
        <property name="maxWaitMillis" value="10000" />
    </bean>

    <bean id="jedisClientConfiguration" class="org.springframework.data.redis.connection.jedis.DefaultJedisClientConfiguration">
        <constructor-arg index="0" value="false" />
        <constructor-arg index="1"><null /></constructor-arg>
        <constructor-arg index="2"><null /></constructor-arg>
        <constructor-arg index="3"><null /></constructor-arg>
        <constructor-arg index="4" value="true" />
        <constructor-arg index="5" ref="genericObjectPoolConfig" />
        <constructor-arg index="6"><null /></constructor-arg>
        <constructor-arg index="7" value="#{T(java.time.Duration).ofMillis(10000)}" />
        <constructor-arg index="8" value="#{T(java.time.Duration).ofMillis(20000)}" />
    </bean>

    <bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
        <property name="hostName" value="localhost" />
        <property name="port" value="6379" />
        <property name="database" value="0" />
    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg index="0" ref="redisStandaloneConfiguration" />
        <constructor-arg index="1" ref="jedisClientConfiguration" />
    </bean>

    <!--redis序列化-->
    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="defaultSerializer" ref="stringRedisSerializer" />
<!--        <property name="keySerializer" ref="stringRedisSerializer" />-->
<!--        <property name="valueSerializer" ref="jdkSerializationRedisSerializer" />-->
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>
</beans>

Test验证

public class Test{
    public static void main(String[] args) {
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("redis.xml");
		RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
		redisTemplate.opsForValue().set("goods","100");
		Object goods = redisTemplate.opsForValue().get("goods");
        System.out.println(goods);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值