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

