欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot整合 lettuce

程序员文章站 2022-04-30 19:45:36
...

导入依赖

		
		<!--spring boot 2.0以后默认使用lettuce-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<!--lettuce 依赖commons-pool-->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
		</dependency>
		

application.yml中配置参数

spring:
  redis:
    port: 6379
    password: root	//Redis 密码
    host: 192.168.255.128	//主机号
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: 1000ms
      shutdown-timeout: 100ms

Lettuce 配置类

@Configuration
public class lettuceConfig  extends CachingConfigurerSupport {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object> template = new RedisTemplate <>();
        template.setConnectionFactory(factory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }
}

模拟service

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Override
    public String getString(String key) {
        if(redisTemplate.hasKey(key)){
            log.info("redis中查找的信息");
            return (String) redisTemplate.opsForValue().get(key);
        }else {
            String val ="RedisTemplate";
            log.info("mysql中查找的信息");
            redisTemplate.opsForValue().set(key,val);
            log.info("mysql中查询的信息存入Redis中");
            return val;
        }

    }
}

测试

@SpringBootTest
class SpringLettuceApplicationTests {
	@Autowired
	private UserService userService;

	@Test
	void contextLoads() {
		String aaaa = userService.getString("aaaa");
		System.out.println(aaaa);
	}

}

成功连接数据库

2020-07-13 19:02:24.708  INFO 10772 --- [           main] c.e.s.service.UserServiceImpl            : redis中查找的信息
RedisTemplate
2020-07-13 19:02:24.746  INFO 10772 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'