spring boot中redis使用
spring boot中redis使用
一、簡介
這裡介紹在spring boot中redis的使用,包含常規使用以及基於佇列的使用。
二、示例
2.1 新增maven依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dragon.study</groupId>
<artifactId>spring_boot_data_redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_boot_data_redis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 定義配置檔案application.yaml
application.yaml如下:
spring:
application:
name: spring-boot-data-redis
redis:
host: localhost
port: 6379
database: 0
server:
port: 10018
2.3 定義監聽處理類
監聽處理類ReflectRedisMqListener.java
package com.dragon.study.spring_boot_data_redis.redisMq;
import org.springframework.stereotype.Component;
@Component
public class ReflectRedisMqListener {
//基於direct的訊息處理
public void directMsgDispose(String msg){
System.out.println("ReflectRedisMqListener directMsgDispose msg: "+ msg);
}
//基於topic的訊息處理
public void topicMsgDispose(String msg){
System.out.println("ReflectRedisMqListener topicMsgDispose msg: "+ msg);
}
}
2.4 定義redis配置類
package com.dragon.study.spring_boot_data_redis.redisMq;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import javax.annotation.Resource;
@Configuration
public class RedisMqConfig {
@Resource
private RedisConnectionFactory redisConnectionFactory;
@Resource
private ReflectRedisMqListener reflectRedisMqListener;
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer() {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(redisConnectionFactory);
container.addMessageListener(directMessageListenerAdapter(), new ChannelTopic("direct")); //繫結topicMessageListenerAdapter監控器到指定通道direct
container.addMessageListener(topicMessageListenerAdapter(), new PatternTopic("topic*")); //繫結topicMessageListenerAdapter監控器到模糊匹配通道topic*
return container;
}
@Bean
public MessageListenerAdapter directMessageListenerAdapter(){
return new MessageListenerAdapter(reflectRedisMqListener,"directMsgDispose");//通過反射,由ReflectRedisMqListener類的topicMsgDispose處理訊息
}
@Bean
public MessageListenerAdapter topicMessageListenerAdapter(){
return new MessageListenerAdapter(reflectRedisMqListener,"topicMsgDispose");//通過反射,由ReflectRedisMqListener類的topicMsgDispose處理訊息
}
}
2.5 定義配置類
package com.dragon.study.spring_boot_data_redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
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;
@SpringBootApplication
public class SpringBootDataRedisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDataRedisApplication.class, args);
}
}
2.6 測試
測試前先啟動SpringBootDataRedisApplication,開啟redis監聽,接著定義測試類如下:
package com.dragon.study.spring_boot_data_redis;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
import java.util.Set;
@SpringBootTest
class SpringBootDataRedisApplicationTests {
@Resource
private StringRedisTemplate stringRedisTemplate;
//訊息測試
@Test
void redisListenerTest() {
stringRedisTemplate.convertAndSend("topic", "topic_msg1");
stringRedisTemplate.convertAndSend("topicOne", "topic_msg2");
stringRedisTemplate.convertAndSend("topicTwo", "topic_msg3");
stringRedisTemplate.convertAndSend("direct", "direct_msg1");
}
//常規測試
@Test
void redisTest() {
//對string操作
stringRedisTemplate.opsForValue().set("k6", "v6");
Object vaObject = stringRedisTemplate.opsForValue().get("k6");
//對list操作
stringRedisTemplate.opsForList().rightPush("q", "one");
Object o = stringRedisTemplate.opsForList().leftPop("q");
//對hash操作
stringRedisTemplate.opsForHash().put("h", "k1", "v1");
Object hashObject = stringRedisTemplate.opsForHash().get("h", "k1");
//對set操作
stringRedisTemplate.opsForSet().add("s", "v1", "v2");
Object setObject = stringRedisTemplate.opsForSet().pop("s");
//對zset有序集合操作,最後一個引數為元素分數
stringRedisTemplate.opsForZSet().add("zs", "v1", 99);
stringRedisTemplate.opsForZSet().add("zs", "v2", 89);
Set scoreZset = stringRedisTemplate.opsForZSet().rangeByScore("zs", 90, 100);
Set zset = stringRedisTemplate.opsForZSet().range("zs", 0, 0);
}
}
相關文章
- Spring Boot(三):Spring Boot 中 Redis 的使用Spring BootRedis
- springboot(三):Spring boot中Redis的使用Spring BootRedis
- 在 Spring Boot 中使用 RedisSpring BootRedis
- Spring Boot(十一):Spring Boot 中 MongoDB 的使用Spring BootMongoDB
- Spring Boot:使用Redis儲存技術Spring BootRedis
- Spring Boot 中使用 Jedis 來操作 RedisSpring BootRedis
- spring boot中zookeeper使用Spring Boot
- Spring Boot中Dockerfile使用Spring BootDocker
- Spring Boot整合RedisSpring BootRedis
- Spring Boot 整合redisSpring BootRedis
- spring boot(三)整合 redisSpring BootRedis
- redis-10-spring-bootRedisSpringboot
- Spring-Boot整合RedisSpringbootRedis
- Spring Boot-Redis教程Spring BootRedis
- 使用Spring Boot實現Redis事務 | VinsguruSpring BootRedis
- spring boot 使用redis進行釋出訂閱Spring BootRedis
- 嵌入式Redis伺服器在Spring Boot測試中的使用Redis伺服器Spring Boot
- JMS 在 Spring Boot 中的使用Spring Boot
- Spring Boot 專案整合RedisSpring BootRedis
- spring-boot-note6---redisSpringbootRedis
- Spring Boot Cache Redis快取Spring BootRedis快取
- spring-boot-starter-redis 整合SpringbootRedis
- spring boot redis做叢集Spring BootRedis
- spring boot使用Jedis整合Redis實現快取(AOP)Spring BootRedis快取
- Spring Boot中@Import三種使用方式!Spring BootImport
- Spring Boot中攔截器的使用Spring Boot
- 【Azure Redis 快取】示例使用 redisson-spring-boot-starter 連線/使用 Azure Redis 服務Redis快取Springboot
- Spring Boot(十八):使用 Spring Boot 整合 FastDFSSpring BootAST
- Spring Boot(五):Spring Boot Jpa 的使用Spring Boot
- Spring Boot整合Redis實戰操作Spring BootRedis
- Spring Boot系列筆記--整合RedisSpring Boot筆記Redis
- Spring Boot 如何快速整合 Redis 哨兵?Spring BootRedis
- 玩轉spring boot——結合redisSpring BootRedis
- Spring Boot 2.0(四):使用 Docker 部署 Spring BootSpring BootDocker
- Spring Boot(十六):使用 Jenkins 部署 Spring BootSpring BootJenkins
- 使用Spring Boot + Redis 進行實時流處理 - vinsguruSpring BootRedis
- Spring Boot 2 + Redis 處理 Session 共享Spring BootRedisSession
- Spring Boot整合Redis叢集(Cluster模式)Spring BootRedis模式