曲靖高端网站制作,数据需求 网站建设,北京网站开发公司飞沐,域名数和网站数目录 一、前言二、基础集成配置#xff08;redis单节点#xff09;2.1、POM2.2、添加配置文件application.yml2.3、编写配置文件2.4、编写启动类2.5、编写测试类测试是否连接成功 一、前言
spring-boot-starter-data-redis有两种实现 lettuce 和 jedis#xff0c;spring bo… 目录 一、前言二、基础集成配置redis单节点2.1、POM2.2、添加配置文件application.yml2.3、编写配置文件2.4、编写启动类2.5、编写测试类测试是否连接成功 一、前言
spring-boot-starter-data-redis有两种实现 lettuce 和 jedisspring boot 2的spring-boot-starter-data-redis中默认使用的是lettuce作为redis客户端也推荐使用lettuce它与jedis的主要区别如下
Jedis Jedis是同步的不支持异步Jedis客户端实例不是线程安全的需要每个线程一个Jedis实例所以一般通过连接池来使用Jedis.优点 提供了比较全面的 Redis 操作特性的 APIAPI 基本与 Redis 的指令一一对应使用简单易理解 缺点 同步阻塞 IO不支持异步线程不安全 Lettuce Lettuce是基于Netty框架的事件驱动的Redis客户端其方法调用是异步的Lettuce的API也是线程安全的所以多个线程可以操作单个Lettuce连接来完成各种操作同时Lettuce也支持连接池.优点 线程安全基于 Netty 框架的事件驱动的通信支持异步和响应式编程适用于分布式缓存支持集群Sentinel管道和编码器等等功能 缺点 API 更抽象学习使用成本高不过我们基本都使用的RedisTemplate来操作Redis它抽象了Jedis或者Lettuce客户端底层实现我们可以不用关心
二、基础集成配置redis单节点
工程结构
2.1、POM parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.12.RELEASE/version/parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependency!--springboot中的redis依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency!-- lettuce pool 缓存连接池--dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactId/dependency!-- 使用jackson作为redis数据序列化 --dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.11.4/version/dependency!-- SpringBoot测试包 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies2.2、添加配置文件application.yml
因为我们用的spring-boot-starter-data-redis包会自动配置redis连接在配置文件中添加对应配置即可
spring:#redis配置信息redis:## Redis数据库索引默认为0database: 0## Redis服务器地址host: 172.16.8.169## Redis服务器连接端口port: 6379## Redis服务器连接密码默认为空password: 123456## 连接超时时间毫秒timeout: 1200lettuce:pool:## 连接池最大连接数使用负值表示没有限制max-active: 8## 连接池最大阻塞等待时间使用负值表示没有限制max-wait: -1## 连接池中的最大空闲连接max-idle: 8## 连接池中的最小空闲连接min-idle: 12.3、编写配置文件
这里需要添加一个RedisTemplate的bean设置这个RedisTemplate序列化方式为jackson
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;Configuration
public class RedisConfig{/*** retemplate相关配置*/Beanpublic RedisTemplateString, Object redisTemplate(RedisConnectionFactory factory) {RedisTemplateString, Object template new RedisTemplate();// 配置连接工厂template.setConnectionFactory(factory);//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值默认使用JDK的序列化方式Jackson2JsonRedisSerializer jacksonSeial new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om new ObjectMapper();// 指定要序列化的域field,get和set,以及修饰符范围ANY是都有包括private和publicom.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);// 指定序列化输入的类型类必须是非final修饰的final修饰的类比如String,Integer等会跑出异常om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jacksonSeial.setObjectMapper(om);// 值采用json序列化template.setValueSerializer(jacksonSeial);//使用StringRedisSerializer来序列化和反序列化redis的key值template.setKeySerializer(new StringRedisSerializer());// 设置hash key 和value序列化模式template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(jacksonSeial);template.afterPropertiesSet();return template;}
}2.4、编写启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class LettuceApplication {public static void main(String[] args) {SpringApplication.run(LettuceApplication.class);}
}2.5、编写测试类测试是否连接成功
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class)
SpringBootTest(classes LettuceApplication.class)
public class LettuceTest {Autowiredprivate RedisTemplateString,Object redisTemplate;Testpublic void t1(){String key key1;System.out.println(插入数据到redis);redisTemplate.opsForValue().set(key,value1);Object value redisTemplate.opsForValue().get(key);System.out.println(从redis中获取到值为 value);Boolean delete redisTemplate.delete(key);System.out.println(删除redis中值 delete);}
}