springboot2 用lettuce客户端配置多个redisTemplate

MAVEN依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</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.yml配置文件

spring:

  redis:
    activity: 
      hostName: 127.0.0.1
      port: 1000
      password: dddsiedXdf
    lettuce: 
      pool: 
        maxIdle: 10
        minIdle: 0
        maxTotal: 20
        maxWaitMillis: 5000

配置类:

@Configuration
public class RedisConfig {
    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Spring Boot中配置Lettuce,可以按照以下步骤进行: 1. 添加Lettuce依赖 首先需要在pom.xml文件中添加Lettuce依赖: ``` <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>5.3.1.RELEASE</version> </dependency> ``` 2. 配置Redis连接 在application.properties或application.yml文件中添加以下配置: ``` spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.database=0 spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.max-wait=-1ms spring.redis.lettuce.pool.min-idle=0 ``` 3. 配置Lettuce连接 在Spring Boot中,Lettuce的连接可以通过@Configuration和@Bean注解进行配置。例如: ``` @Configuration public class RedisConfig { @Autowired private RedisProperties redisProperties; @Bean public RedisConnectionFactory lettuceConnectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); config.setHostName(redisProperties.getHost()); config.setPort(redisProperties.getPort()); config.setPassword(RedisPassword.of(redisProperties.getPassword())); return new LettuceConnectionFactory(config); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(lettuceConnectionFactory()); return template; } } ``` 在上面的示例中,使用了@Configuration和@Bean注解来定义了一个RedisConfig类,并且定义了一个lettuceConnectionFactory()方法和一个redisTemplate()方法。 4. 使用Lettuce连接Redis 在代码中使用Lettuce连接Redis时,可以直接注入RedisTemplate并使用它来操作Redis。例如: ``` @Autowired private RedisTemplate<String, Object> redisTemplate; public void setValue(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object getValue(String key) { return redisTemplate.opsForValue().get(key); } ``` 在上面的示例中,使用了@Autowired注解来注入RedisTemplate,并使用它来设置和获取值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值