SpringBoot整合Redisson的步驟(單機(jī)版)
優(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操作特性的APIAPI基本與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整合Redisson1.添加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: 50003.添加配置類
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;@Configurationpublic 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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. PHP輸入流php://input的使用分析2. asp文件用什么軟件編輯3. PHP基礎(chǔ)之生成器4——比較生成器和迭代器對象4. 利用Java對PDF文件進(jìn)行電子簽章的實(shí)戰(zhàn)過程5. Docker 啟動(dòng)Redis 并設(shè)置密碼的操作6. 詳解PHP laravel中的加密與解密函數(shù)7. ASP新手必備的基礎(chǔ)知識8. PHP與MYSQL數(shù)據(jù)庫連接9. CentOS郵箱服務(wù)器搭建系列——SMTP服務(wù)器的構(gòu)建( Postfix )10. Django項(xiàng)目如何正確配置日志(logging)

網(wǎng)公網(wǎng)安備