基于已搭建好的哨兵模式的redis,创建一个springBoot使用lettuce连接Redis的测试项目。
-
使用idea创建springBoot项目
-
在菜单File–new –project 打开新建项目框
-
打开新建项目框,选择项如图,点击next后 ,设置包名及项目名
-
在新建项目中选择web及redis组件
- ,一路next直到finish即可
-
在菜单File–new –project 打开新建项目框
-
pom.xml文件中的配置:
<?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.2.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.demo</groupId> <artifactId>spring-boot-demo</artifactId> <version>0.0.1</version> <name>spring-boot-demo</name> <description>springboot相关的服务</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
将src/main/resources/application.properties改为application.yml,具体内容如下:
server: port: 10089 spring: redis: database: 11 sentinel: master: mymaster nodes: - 10.10.55.51:26379 - 10.10.55.51:26380 - 10.10.55.51:26381 lettuce: pool: max-active: 1000 max-idle: 10 min-idle: 5 max-wait: -1
-
RedisConfig 的源码
package com.demo.springbootdemo.config; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisNode; import org.springframework.data.redis.connection.RedisSentinelConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.ArrayList; import java.util.List; /** * @program: spring-boot-demo * @description: redis配置 * @date: 2020/6/72:22 **/ @Configuration public class RedisConfig { @Autowired RedisProperties redisProperties; //读取pool配置 @Bean public GenericObjectPoolConfig poolConfig() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle()); config.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle()); config.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive()); config.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMaxWait().toMillis()); return config; } /** * @Description: 将哨兵信息放到配置中 * @date 2020/6/7 14:42 */ @Bean public RedisSentinelConfiguration configuration() { RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration(); redisConfig.setMaster(redisProperties.getSentinel().getMaster()); // redisConfig.setPassword(RedisPassword.of(redisConfigThree.getPassword())); if(redisProperties.getSentinel().getNodes()!=null) { List<RedisNode> sentinelNode=new ArrayList<RedisNode>(); for(String sen : redisProperties.getSentinel().getNodes()) { String[] arr = sen.split(":"); sentinelNode.add(new RedisNode(arr[0],Integer.parseInt(arr[1]))); } redisConfig.setSentinels(sentinelNode); } return redisConfig; } @Bean("lettuceConnectionFactory") public LettuceConnectionFactory lettuceConnectionFactory(@Qualifier("poolConfig") GenericObjectPoolConfig config, @Qualifier("configuration") RedisSentinelConfiguration redisConfig) {//注意传入的对象名和类型RedisSentinelConfiguration LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build(); return new LettuceConnectionFactory(redisConfig, clientConfiguration); } @Bean("stringObjectRedisTemplate") public RedisTemplate<String, Object> stringObjectRedisTemplate(@Qualifier("lettuceConnectionFactory")LettuceConnectionFactory connectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); //设置序列化器 redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } }
-
使用controller测一笔调用
package com.demo.springbootdemo.controller; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; /** * @program: spring-boot-demo * @description: redis操作 * @date: 2020/6/513:13 **/ @RestController public class RedisController { private Logger log = LogManager.getLogger(RedisController.class); @Autowired RedisTemplate<String,String> redisTemplate; @PostMapping("/redis/write") public String writeRedis(@RequestBody String name){ log.info("redis写入操作,入参[{}]",name); redisTemplate.opsForValue().set("name",name); return redisTemplate.opsForValue().get("name"); } }
注意:yml中的配置,因为使用的是哨兵模式,所以配置需要使用 spring.redis.sentinel 属性,一定别写错了,否则连接不上
版权声明:本文为repAgell原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。