java 直接操作redis 默认采用的是Jedis,是官方推荐的redis 连接工具。
新建maven 工程,导入pom 文件
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency>
用main 方法测试:
public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.auth("123456"); System.out.println(jedis.toString()); System.out.println(jedis.ping()); jedis.close(); }
现在springboot集成了redis 并且在springboot2.0之后底层用lettuce 替换了jedis
jedis 和lettuce 的区别:
jedis : 采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用 jedis pool 连接 池! 更像 BIO 模式
lettuce : 采用netty,实例可以再多个线程中进行共享,不存在线程不安全的情况!可以减少线程数据 了,更像 NIO 模式
整合测试:
新建maven 项目,导入依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.5.2</version> </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.2.RELEASE</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> <resource> <directory>src/main/webapp</directory> <!--注意此次必须要放在此目录下才能被访问到 --> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> </resources> </build>
在application.yml 中写入以下配置:
spring: redis: host: 192.168.207.130 port: 6379 password: 123456 database: 0 connect-timeout: 10000 jedis: pool: max-active: 100 max-idle: 10 min-idle: 5 max-wait: -1
之后新建测试类:
@RunWith(SpringRunner.class) @SpringBootTest(classes = App.class) public class Redis02SpringbootApplicationTests { @Autowired private RedisTemplate redisTemplate; @Test public void contextLoads(){ redisTemplate.opsForValue().set("key","111",30, TimeUnit.MINUTES); System.out.println(redisTemplate.opsForValue().get("key")); } }
可以成功set 进去值,以及get 获取值。
redistemplate 默认使用的序列化是JdkSerializationRedisSerializer
我们要使用jackson 来序列化:
在config 包下新建配置类:
/** * redis 配置序列化 */ @Configuration public class RedisConfig { @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); // 使用Jackson2JsonRedisSerialize 替换默认序列化 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); // 设置value的序列化规则和 key的序列化规则 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); //String key序列化 redisTemplate.setKeySerializer(new StringRedisSerializer()); //hash key序列化 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //hash value 序列化 redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer); redisTemplate.setEnableDefaultSerializer(true); redisTemplate.afterPropertiesSet(); return redisTemplate; }
结束。