springboot項目中配置redis詳細的教程
前言
當在 Java 項目中使用 Redis 時,特別是在 Spring Boot 項目中使用 Redis,下面是一個詳細的教程,涵蓋了 Redis 的配置和使用。
Redis是當前比較熱門的NOSQL系統(tǒng)之一,它是一個開源的使用ANSI c語言編寫的key-value存儲系統(tǒng)(區(qū)別于MySQL的二維表格的形式存儲。)。和Memcache類似,但很大程度補償了Memcache的不足。和Memcache一樣,Redis數(shù)據(jù)都是緩存在計算機內(nèi)存中,不同的是,Memcache只能將數(shù)據(jù)緩存到內(nèi)存中,無法自動定期寫入硬盤,這就表示,一斷電或重啟,內(nèi)存清空,數(shù)據(jù)丟失。所以Memcache的應(yīng)用場景適用于緩存無需持久化的數(shù)據(jù)。而Redis不同的是它會周期性的把更新的數(shù)據(jù)寫入磁盤或者把修改操作寫入追加的記錄文件,實現(xiàn)數(shù)據(jù)的持久化。
Redis的特點:
1,Redis讀取的速度是110000次/s,寫的速度是81000次/s;
2,原子 。Redis的所有操作都是原子性的,同時Redis還支持對幾個操作全并后的原子性執(zhí)行。
3,支持多種數(shù)據(jù)結(jié)構(gòu):string(字符串);list(列表);hash(哈希),set(集合);zset(有序集合)
4,持久化,集群部署
5,支持過期時間,支持事務(wù),消息訂閱
在 Spring Boot 項目中配置和使用 Redis
步驟 1:添加 Redis 依賴
在你的 Spring Boot 項目的 pom.xml
文件中,添加 Redis 相關(guān)的依賴項:
<dependencies> <!-- 其他依賴項 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </dependencies>
這將添加 Spring Boot Redis Starter 依賴項,以便在項目中使用 Redis。
步驟 2:配置 Redis 連接信息
在 Spring Boot 項目中,可以通過在 application.properties
或 application.yml
文件中配置 Redis 連接信息。
使用 application.properties 配置文件:
在 application.properties
文件中添加以下配置:
# Redis 連接信息 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=your_password
使用 application.yml 配置文件:
在 application.yml
文件中添加以下配置:
# Redis 連接信息 spring: redis: host: 127.0.0.1 port: 6379 password: your_password
請確保將上述配置中的 your_password
替換為你實際的 Redis 密碼。如果 Redis 服務(wù)器沒有設(shè)置密碼,則可以省略 spring.redis.password
配置。
步驟 3:創(chuàng)建 Redis 配置類
創(chuàng)建一個名為 RedisConfig
的配置類,用于配置 RedisTemplate 和連接工廠。
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { // 從配置文件中讀取Redis主機信息 @Value("${spring.redis.host}") private String redisHost; // 從配置文件中讀取Redis端口信息 @Value("${spring.redis.port}") private int redisPort; // 配置Redis連接工廠 @Bean public RedisConnectionFactory redisConnectionFactory() { // 創(chuàng)建Redis的單機配置 RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort); // 返回Lettuce連接工廠 return new LettuceConnectionFactory(config); } // 配置RedisTemplate @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { // 創(chuàng)建RedisTemplate實例 RedisTemplate<String, Object> template = new RedisTemplate<>(); // 設(shè)置連接工廠 template.setConnectionFactory(connectionFactory); // 設(shè)置默認的序列化器為GenericJackson2JsonRedisSerializer,用于序列化鍵和值為JSON格式 template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer()); // 設(shè)置鍵的序列化器為StringRedisSerializer template.setKeySerializer(new StringRedisSerializer()); // 設(shè)置值的序列化器為GenericJackson2JsonRedisSerializer template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // 返回配置好的RedisTemplate實例 return template; } }
上述配置類使用 Lettuce 作為 Redis 連接工廠,并配置了 RedisTemplate,使用 JSON 序列化器來序列化鍵和值。
步驟 4:使用 RedisTemplate 進行操作
在你的代碼中,你可以使用 RedisTemplate
進行各種操作,如存儲鍵值對、獲取值、刪除鍵等。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class MyService { private final RedisTemplate<String, Object> redisTemplate; @Autowired public MyService(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } public void setValue(String key, Object value) { // 使用RedisTemplate的opsForValue()方法獲取ValueOperations接口實例,然后調(diào)用set()方法存儲鍵值對 redisTemplate.opsForValue().set(key, value); } public Object getValue(String key) { // 使用RedisTemplate的opsForValue()方法獲取ValueOperations接口實例,然后調(diào)用get()方法根據(jù)鍵獲取值 return redisTemplate.opsForValue().get(key); } public void deleteKey(String key) { // 調(diào)用RedisTemplate的delete()方法根據(jù)鍵刪除對應(yīng)的鍵值對 redisTemplate.delete(key); } }
上述示例代碼展示了一個名為 MyService
的服務(wù)類,它使用 RedisTemplate
進行鍵值對的存儲、獲取和刪除操作。
請確保在你的代碼中使用適當?shù)淖⒔猓ㄈ?nbsp;@Service
、@Autowired
等)來注入 RedisTemplate 實例并進行相應(yīng)的操作。
總結(jié)
到此這篇關(guān)于springboot項目中配置redis的文章就介紹到這了,更多相關(guān)springboot項目配置redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用Mybatis&Mybatis-plus文件映射配置方法
這篇文章主要介紹了SpringBoot使用Mybatis&Mybatis-plus文件映射配置方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05Java Swing實現(xiàn)坦克大戰(zhàn)游戲
這篇文章主要介紹了Java Swing實現(xiàn)坦克大戰(zhàn)游戲,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很大的幫助喲,需要的朋友可以參考下2021-05-05SpringBoot+Mybatis+Vue 實現(xiàn)商品模塊的crud操作
這篇文章主要介紹了SpringBoot+Mybatis+Vue 實現(xiàn)商品模塊的crud操作,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10SpringBoot監(jiān)控模塊Actuator的用法詳解
Spring?Boot?Actuator?是?Spring?Boot?自帶的一個功能模塊,提供了一組已經(jīng)開箱即用的生產(chǎn)環(huán)境下常用的特性和服務(wù),比如應(yīng)用程序的健康檢查、信息暴露、度量收集、日志記錄等,本文將給大家詳細SpringBoot監(jiān)控模塊Actuator的用法2023-06-06