Spring Boot + Redis with Redisson client

Spring Boot + Redis with Redisson client

2021, Sep 07    

Hướng dẫn tích hợp Redis bằng Redisson client trong Spring Boot application

  • Sử dụng dependency
<dependencies>
  <dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.16.1</version>
  </dependency>
</dependencies>

Lưu ý: chọn version redisson-spring-boot-starter tương ứng version Spring Boot

  • Tạo file redisson.yml chứa cấu hình kết nối redis server trong thư mục resouces
singleServerConfig:
  idleConnectionTimeout: 10000
  connectTimeout: 10000
  timeout: 3000
  retryAttempts: 3
  retryInterval: 1500
  password: null
  subscriptionsPerConnection: 5
  clientName: null
  address: "redis://127.0.0.1:6379"
  subscriptionConnectionMinimumIdleSize: 1
  subscriptionConnectionPoolSize: 50
  connectionMinimumIdleSize: 10
  connectionPoolSize: 64
  database: 0
  dnsMonitoringInterval: 5000
threads: 0
nettyThreads: 0
codec: !<org.redisson.codec.JsonJacksonCodec> {}
  • Thêm cấu hình trong file application.yml
spring:
  redis:
    redisson:
      file: classpath:redisson.yml
  • Thao tác với redis thông qua bean RedissonClient
  @Autowired
  RedissonClient redissonClient;
    
  public void setExample(String key, String value) {
    RBucket<String> bucket = redissonClient.getBucket(key);
    bucket.set(value);
  }

  public String getExample(String key) {
    RBucket<String> bucket = redissonClient.getBucket(key);
    return bucket.get();
  }

Source code ở đây