Redis Standalone Configuration in Spring Boot: Complete Guide
RedisStandaloneConfiguration is a class within the Spring Data Redis library that provides configuration for a single Redis instance running in standalone mode. It's used to set the Redis server host, port, password, and other properties.
Here's an example of utilizing RedisStandaloneConfiguration to configure a Redis connection in a Spring Boot application:
@Configuration
public class RedisConfig {
@Value('${spring.redis.host}')
private String redisHost;
@Value('${spring.redis.port}')
private int redisPort;
@Value('${spring.redis.password}')
private String redisPassword;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort);
config.setPassword(redisPassword);
return new LettuceConnectionFactory(config);
}
}
In this example, the Redis server host, port, and password are fetched from the application.properties file using the @Value annotation. A RedisStandaloneConfiguration object is then constructed with these values, and a RedisConnectionFactory is created using the Lettuce client implementation.
The RedisConnectionFactory can subsequently be used to create RedisTemplate objects for interacting with the Redis server.
原文地址: https://www.cveoy.top/t/topic/nPgJ 著作权归作者所有。请勿转载和采集!