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)文章
SpringMVC運行時出現(xiàn)404錯誤的解決辦法匯總(基本包含所有錯誤可能)
初學(xué)SpringMVC基本都會碰到404問題(確實也困擾了我好長時間),但出現(xiàn)404問題的原因有很多,如果確認路徑,代碼沒問題,并且服務(wù)器可以正常啟動,依然出現(xiàn)404問題的話,就根據(jù)本篇步驟逐一排查,需要的朋友可以參考下2024-04-04
關(guān)于Java JDK安裝、配置環(huán)境變量的問題
這篇文章主要介紹了Java JDK安裝、配置環(huán)境變量,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
mybatis抽取基類BaseMapper增刪改查的實現(xiàn)
目前項目當中使用mapper.xml文件方式對數(shù)據(jù)庫進行操作,但是每個里邊都有增/刪/改/查,為了方便開發(fā),把這些公共的代碼提取出來,不用當做基類,不用每個Mapper文件都寫了,本文就詳細的介紹一下實現(xiàn)方法2021-09-09
Java實現(xiàn) 基于密度的局部離群點檢測------lof算法
這篇文章主要介紹了Java實現(xiàn) 基于密度的局部離群點檢測------lof算法,本文通過算法概述,算法Java源碼,測試結(jié)果等方面一一進行說明,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
Spring?Cloud?Gateway服務(wù)網(wǎng)關(guān)限流問題及解決
這篇文章主要介紹了Spring?Cloud?Gateway服務(wù)網(wǎng)關(guān)限流問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
Spring Cloud Eureka 服務(wù)上下線監(jiān)控的實現(xiàn)
這篇文章主要介紹了Spring Cloud Eureka 服務(wù)上下線監(jiān)控的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09

