Redis 客戶端之Redisson 配置使用(基於Spring Boot 2.x)
阿新 • • 發佈:2018-11-15
開發環境:使用Intellij IDEA + Maven + Spring Boot 2.x + JDK 8
1.在專案的pom.xml檔案下,引入Redis和Redisson在Spring Boot 下的相關Jar包依賴。
<properties> <redisson.version>3.8.2</redisson.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>${redisson.version}</version> <optional>true</optional> </dependency> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>${redisson.version}</version> </dependency> </dependencies>
2.在專案的resources目錄下,新增redisson的配置檔案(這裡使用yaml格式的配置檔案redisson-config.yml,檔名可自己定, 檔案的示例配置如下)。
#Redisson配置 singleServerConfig: address: "redis://127.0.0.1:6379" password: null clientName: null database: 7 #選擇使用哪個資料庫0~15 idleConnectionTimeout: 10000 pingTimeout: 1000 connectTimeout: 10000 timeout: 3000 retryAttempts: 3 retryInterval: 1500 reconnectionTimeout: 3000 failedAttempts: 3 subscriptionsPerConnection: 5 subscriptionConnectionMinimumIdleSize: 1 subscriptionConnectionPoolSize: 50 connectionMinimumIdleSize: 32 connectionPoolSize: 64 dnsMonitoringInterval: 5000 #dnsMonitoring: false threads: 0 nettyThreads: 0 codec: class: "org.redisson.codec.JsonJacksonCodec" transportMode: "NIO"
具體想加些什麼配置,可以檢視Redisson提供的Config類裡支援的引數,不支援添加里面沒有的引數配置。除了yaml型別格式的配置,也支援json格式的配置檔案,具體不在這裡展開,詳情可以參考Redisson配置方法。
3.新增Redisson的配置引數讀取類RedissonConfig
package com.dbfor.redis.config; import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.io.IOException; @Configuration public class RedissonConfig { @Bean public RedissonClient redisson() throws IOException { // 本例子使用的是yaml格式的配置檔案,讀取使用Config.fromYAML,如果是Json檔案,則使用Config.fromJSON Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson-config.yml")); return Redisson.create(config); } }
4.構建Spring Boot的啟動類RedisApplication
package com.dbfor.redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RedisApplication {
public static void main(String[] args) {
SpringApplication.run(RedisApplication.class);
}
}
5.編寫測試類RedissonTest
package com.dbfor.redis;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
@Component
public class RedissonTest {
@Autowired
private RedissonClient redissonClient;
@Test
public void set() {
// 設定字串
RBucket<String> keyObj = redissonClient.getBucket("k1");
keyObj.set("v1236");
}
}
6.在Redis上檢視執行結果
從上圖可以看到,Redisson配置操作資料庫成功!
其他Redis客戶端:
Redis 客戶端之Lettuce配置使用(基於Spring Boot 2.x)