就像一个从来没有杀过猪的人在教别人杀猪

一、介绍

在上一篇文章里,在公共模块中引入了redis依赖,从而造成没有配置redis信息的模块,在启动时,会默认去连接localhost:6379

这是我们不希望看到的,所以便有了上一篇文章

当SpringBoot引用Redis依赖但没有配置Redis信息时 | 半月无霜 (banmoon.top)

好巧不巧,目前又出现了一个情况。

就是reactiveselevt两种不同的web去引用了这个模块。

selevt是好了,但我的gateway出现了问题,它连不上redis了,连对应的bean都没有创建出来。

所以没办法,我只能再继续改造这个RedisConfig.java

二、代码

1)配置类

原本的RedisConfig.java,用来给selevt使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.banmoon.business.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
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.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* @author banmoon
* @date 2024/03/07 12:08:25
*/
@SuppressWarnings("all")
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@EnableAutoConfiguration(exclude = {RedisAutoConfiguration.class})
public class RedisConfig {

@Bean
@ConditionalOnBean(RedisConnectionFactory.class)
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
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;
}

}

新开一个ReactiveRedisConfig.java,给reactive使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.banmoon.business.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* @author banmoon
* @date 2024/03/07 12:08:25
*/
@SuppressWarnings("all")
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore({RedisAutoConfiguration.class, RedisReactiveAutoConfiguration.class})
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public class ReactiveRedisConfig {

@Bean
@ConditionalOnBean(ReactiveRedisConnectionFactory.class)
public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory 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();
RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext
.<String, Object>newSerializationContext(stringRedisSerializer)
.value(jackson2JsonRedisSerializer)
.hashKey(stringRedisSerializer)
.hashValue(jackson2JsonRedisSerializer)
.build();
ReactiveRedisTemplate<String, Object> template = new ReactiveRedisTemplate<>(factory, serializationContext);
return template;
}

}

注意此处,需要指定在其之前加载,那样才能保证使用的是我们自己的reactiveRedisTemplate

@AutoConfigureBefore({RedisAutoConfiguration.class, RedisReactiveAutoConfiguration.class})

2)测试

写一个controller进行测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.banmoon.controller;

import com.banmoon.business.obj.dto.ResultData;
import com.banmoon.obj.request.RedisSetValueRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import javax.validation.Valid;

@Slf4j
@Api(tags = "redis测试模块")
@Validated
@RestController
@RequestMapping("/redis/test")
public class RedisTestController {

@Resource
private ReactiveRedisTemplate<String, Object> reactiveRedisTemplate;

@ApiOperation("获取redis缓存")
@GetMapping("/get")
public Mono<ResultData<String>> get(String key) {
return reactiveRedisTemplate.opsForValue()
.get(key)
.map(value -> ResultData.success(value.toString()));
}

@ApiOperation("设置redis缓存")
@PostMapping("/set")
public Mono<ResultData<?>> set(@RequestBody @Valid RedisSetValueRequest request) {
return reactiveRedisTemplate.opsForValue()
.set(request.getKey(), request.getValue())
.map(result -> result ? ResultData.success() : ResultData.fail());
}

}

执行测试

image-20240517230158000

image-20240517230227446

三、最后

那些我记录的技能点,就算是哪天我在某处看到的一篇文章,它就像一个引子,我想把它分享出去,这个问题可以试试用这个去解决。

就当我满心欢喜的分享我所知道的技术的时候,冷不防的一句话将我真是让我哑口无言

就像一个从来没有杀过猪的人在教别人杀猪!

行吧,半响之后,我从牙缝里面挤出这个词。

以后,再无共勉,工作上的事情,还是不要报太大的兴趣吧。