溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

spring boot redis

發(fā)布時(shí)間:2020-07-09 17:56:32 來源:網(wǎng)絡(luò) 閱讀:1434 作者:乾坤刀 欄目:軟件技術(shù)

dependency

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

application-redis.properties

#########基礎(chǔ)配置#########
spring.redis.database=0
#spring.redis.url=redis://user:password@example.com:6379
spring.redis.host=
spring.redis.port=
spring.redis.password=
spring.redis.timeout=30000
spring.redis.ssl=false

#########redis哨兵設(shè)置#########
#spring.redis.sentinel.master=
#spring.redis.sentinel.nodes=
#spring.redis.cluster.max-redirects=
#spring.redis.cluster.nodes=

#########jedis client 線程池配置#########
#spring.redis.jedis.pool.max-active=
#spring.redis.jedis.pool.max-idle=
#spring.redis.jedis.pool.min-idle=
#spring.redis.jedis.pool.max-wait=

#########lettuce client 線程池配置(默認(rèn))#########
spring.redis.lettuce.pool.max-active=10
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=1
spring.redis.lettuce.pool.max-wait=20000
spring.redis.lettuce.shutdown-timeout=10

Test

@RunWith(SpringRunner.class)
@SpringBootTest
public class AnnotationAppContextTest {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void setTest(){
        redisTemplate.opsForValue().set("key1","val1", 100);
        String key1 = redisTemplate.opsForValue().get("key1");
        System.err.println("=============="+key1);
    }
}

源碼-RedisProperties

@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {

    /**
     * Database index used by the connection factory.
     */
    private int database = 0;

    /**
     * Connection URL. Overrides host, port, and password. User is ignored. Example:
     * redis://user:password@example.com:6379
     */
    private String url;

    /**
     * Redis server host.
     */
    private String host = "localhost";

    /**
     * Login password of the redis server.
     */
    private String password;

    /**
     * Redis server port.
     */
    private int port = 6379;

    /**
     * Whether to enable SSL support.
     */
    private boolean ssl;

    /**
     * Connection timeout.
     */
    private Duration timeout;

    private Sentinel sentinel;

    private Cluster cluster;

    private final Jedis jedis = new Jedis();

    private final Lettuce lettuce = new Lettuce();
}   

源碼-RedisAutoConfiguration

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

}

參考:https://blog.csdn.net/abombhz/article/details/78123253?locationNum=6&fps=1

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI