SpringBoot整合Redisson的步驟(單機(jī)版)
Redisson、Jedis、Lettuce優(yōu)缺點(diǎn)對比
(1)Redisson
優(yōu)點(diǎn):
實(shí)現(xiàn)了分布式特性和可擴(kuò)展的 Java 數(shù)據(jù)結(jié)構(gòu),適合分布式開發(fā);
API線程安全;
基于Netty框架的事件驅(qū)動(dòng)的通信,可異步調(diào)用。
缺點(diǎn):
API更抽象,學(xué)習(xí)使用成本高。
(2)Jedis
優(yōu)點(diǎn):
提供了比較全面的Redis操作特性的API
API基本與Redis的指令一一對應(yīng),使用簡單易理解。
缺點(diǎn):
同步阻塞IO;
不支持異步;
線程不安全。
(3)Lettuce
優(yōu)點(diǎn):
線程安全;
基于Netty 框架的事件驅(qū)動(dòng)的通信,可異步調(diào)用;
適用于分布式緩存。
缺點(diǎn):
API更抽象,學(xué)習(xí)使用成本高。
其中Jedis是用的最普遍的(確實(shí)非常簡單),特別是很多單體應(yīng)用或者偽分布式應(yīng)用等。
SpringBoot整合Redisson
1.添加Maven依賴
<!-- redisson-springboot -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.11.4</version>
</dependency>
2.配置文件
spring:
redis:
host: 127.0.0.1
port: 6379
database: 0
timeout: 5000
3.添加配置類
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RedissonConfig {
@Autowired
private RedisProperties redisProperties;
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
String redisUrl = String.format("redis://%s:%s", redisProperties.getHost() + "", redisProperties.getPort() + "");
config.useSingleServer().setAddress(redisUrl).setPassword(redisProperties.getPassword());
config.useSingleServer().setDatabase(3);
return Redisson.create(config);
}
}
4.代碼測試(簡單的存取)
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/redisson")
public class RedissonController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/save")
public String save(){
stringRedisTemplate.opsForValue().set("key","redisson");
return "save ok";
}
@GetMapping("/get")
public String get(){
return stringRedisTemplate.opsForValue().get("key");
}
}
以上就是SpringBoot整合Redisson的步驟(單機(jī)版)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Redisson的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring\SpringBoot配置連接數(shù)據(jù)庫的方法
最近在學(xué)習(xí)SpringBoot,第一步就是要配置數(shù)據(jù)庫,本文詳細(xì)的介紹了Spring\SpringBoot配置連接數(shù)據(jù)庫的方法,有需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
解決springboot配置文件組解決自動(dòng)配置屬性無法注入問題
在使用Spring Boot時(shí),可能會遇到配置文件屬性注入失敗的問題,本文描述了一個(gè)案例,其中嘗試使用profile文件組指定不同環(huán)境下的配置文件,但遇到了屬性無法成功注入的情況,提供的解決辦法是將Spring Boot的版本號從2.2.0.RELEASE升級到2.4.02024-09-09
SpringBoot事件發(fā)布與監(jiān)聽超詳細(xì)講解
今天去官網(wǎng)查看spring boot資料時(shí),在特性中看見了系統(tǒng)的事件及監(jiān)聽章節(jié),所以下面這篇文章主要給大家介紹了關(guān)于SpringBoot事件發(fā)布和監(jiān)聽的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
Java Fluent Mybatis 分頁查詢與sql日志輸出詳解流程篇
Java中常用的ORM框架主要是mybatis, hibernate, JPA等框架。國內(nèi)又以Mybatis用的多,基于mybatis上的增強(qiáng)框架,又有mybatis plus和TK mybatis等。今天我們介紹一個(gè)新的mybatis增強(qiáng)框架 fluent mybatis關(guān)于分頁查詢、sql日志輸出流程2021-10-10

