Spring Redis - 从 application.properties 文件中读取配置

2022-09-01 15:09:36

我有Spring Redis使用所有默认配置,如默认等。spring-data-redislocalhostport

现在,我正在尝试通过在文件中配置它来进行相同的配置。但是我无法弄清楚我应该如何创建我的属性值被读取的bean。application.properties

Redis 配置文件

@EnableRedisHttpSession
@Configuration
public class SpringSessionRedisConfiguration {

@Bean
JedisConnectionFactory connectionFactory() {
    return new JedisConnectionFactory();
}

@Autowired
@Bean
RedisCacheManager redisCacheManager(final StringRedisTemplate stringRedisTemplate) {
    return new RedisCacheManager(stringRedisTemplate);
}

@Autowired
@Bean
StringRedisTemplate template(final RedisConnectionFactory connectionFactory) {
    return new StringRedisTemplate(connectionFactory);
}
}

应用程序中的标准参数.属性

spring.redis.sentinel.master=themaster

spring.redis.sentinel.nodes=192.168.188.231:26379

spring.redis.password=12345

我尝试过,

  1. 我可以使用然后注入并获取值。但我不想这样做,因为这些属性不是由我定义的,而是来自Spring。@PropertySource@Value
  2. 在本文档Spring Redis文档中,它只说它可以使用属性进行配置,但没有显示具体示例。
  3. 我还浏览了Spring Data Redis API类,发现这应该对我有所帮助,但仍然无法弄清楚如何确切地告诉Spring从属性文件中读取。RedisProperties

答案 1

您可以使用 从 application.properties 或所需的其他属性文件中读取选项。请查看 PropertySource 用法示例spring-redis-cache 用法工作示例。或者看看这个小样本:@PropertySource

@Configuration
@PropertySource("application.properties")
public class SpringSessionRedisConfiguration {

    @Value("${redis.hostname}")
    private String redisHostName;

    @Value("${redis.port}")
    private int redisPort;

    @Bean
    public static PropertySourcesPlaceholderConfigurer    propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setHostName(redisHostName);
        factory.setPort(redisPort);
        factory.setUsePool(true);
        return factory;
    }

    @Bean
    RedisTemplate<Object, Object> redisTemplate() {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        return redisTemplate;
    }

    @Bean
    RedisCacheManager cacheManager() {
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
        return redisCacheManager;
    }
}

目前(2015 年 12 月),spring.redis.sentinel 选项对以下各项的支持有限:application.propertiesRedisSentinelConfiguration

请注意,目前只有Jedis和生菜生菜支持Redis Sentinel。

您可以在官方文档中阅读有关此内容的更多信息。


答案 2

在深入研究后,我发现了这一点,这可能是你要找的吗?

# REDIS (RedisProperties)
spring.redis.database=0 # Database index used by the connection factory.
spring.redis.host=localhost # Redis server host.
spring.redis.password= # Login password of the redis server.
spring.redis.pool.max-active=8 # Max number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
spring.redis.pool.max-idle=8 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
spring.redis.pool.max-wait=-1 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
spring.redis.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
spring.redis.port=6379 # Redis server port.
spring.redis.sentinel.master= # Name of Redis server.
spring.redis.sentinel.nodes= # Comma-separated list of host:port pairs.
spring.redis.timeout=0 # Connection timeout in milliseconds. 

引用:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html 搜索词 Redis

从我所看到的,这些值已经存在,并被定义为

spring.redis.host=localhost # Redis server host.
spring.redis.port=6379 # Redis server port.

如果你想创建自己的属性,你可以看看我之前的帖子在这个线程。


推荐