在
SpringBoot 中配置
Redis 连接池需要以下步骤:
1. 引入
Redis相关依赖,例如
jedis或
let tuce。
2. 在 application.properties 或 application.yml
中配置
Redis 连接池相关属性,如下:
spring.redis.host=127.0.0.1spring.redis.port=6379spring.redis.password=spring.redis.database=0spring.redis.let tuce.pool.max-active=8spring.redis.let tuce.pool.max-idle=8spring.redis.let tuce.pool.min-idle=0spring.redis.let tuce.pool.max-wait=-1ms
3. 在 Java 代码
中通过 @Bean 注解创建
RedisConnectionFactory 和
RedisTemplate 对象,如下:
@Configurationpublic classRedisConfig {@Value("${spring.redis.host}")private String host;@Value("${spring.redis.port}")private int port;@Value("${spring.redis.password}")private String password;@Value("${spring.redis.database}")private int database;@BeanpublicLet tuceConnectionFactoryredisConnectionFactory() {RedisStandaloneConfigurationredisStandaloneConfiguration = newRedisStandaloneConfiguration(host, port);redisStandaloneConfiguration.setDatabase(database);redisStandaloneConfiguration.setPassword(RedisPassword.of(password));return newLet tuceConnectionFactory(redisStandaloneConfiguration);}@BeanpublicRedisTemplate<String, Object>redisTemplate() {RedisTemplate<String, Object> template = newRedisTemplate<>();template.setConnectionFactory(redisConnectionFactory());template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}}
以上代码
中的
Let tuceConnectionFactory 和
RedisTemplate 对象的创建需要使用到
Redis的相关依赖,在 pom.xml 文件
中添加以下依赖即可:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.mushiming.com/mjsbk/4700.html