Spring Redis - Read configuration from application.properties file

后端 未结 9 933
渐次进展
渐次进展 2020-12-25 13:46

I have Spring Redis working using spring-data-redis with all default configuration likes localhost default port and so on.

Now

相关标签:
9条回答
  • 2020-12-25 14:24

    Upon looking deeper I found this, could it be what you are looking for?

    # 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. 
    

    Refernce:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html Searchterm Redis

    From what I can see the values already exist and are defined as

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

    if you want to create your own properties you can look at my previous post in this thread.

    0 讨论(0)
  • 2020-12-25 14:24

    This works for me :

    @Configuration
    @EnableRedisRepositories
    public class RedisConfig {
    
        @Bean
        public JedisConnectionFactory jedisConnectionFactory() {
            RedisProperties properties = redisProperties();
            RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
            configuration.setHostName(properties.getHost());
            configuration.setPort(properties.getPort());
    
            return new JedisConnectionFactory(configuration);
        }
    
        @Bean
        public RedisTemplate<String, Object> redisTemplate() {
            final RedisTemplate<String, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(jedisConnectionFactory());
            template.setValueSerializer(new GenericToStringSerializer<>(Object.class));
            return template;
        }
    
        @Bean
        @Primary
        public RedisProperties redisProperties() {
            return new RedisProperties();
        }
    
    }
    

    and properties file :

    spring.redis.host=localhost
    spring.redis.port=6379
    
    0 讨论(0)
  • 2020-12-25 14:31
    @Autowired
    private JedisConnectionFactory connectionFactory;
    
    @Bean
    JedisConnectionFactory connectionFactory() {
        return connectionFactory
    }
    
    0 讨论(0)
提交回复
热议问题