我是redisReact式实现的新手。我正试图建立一个后端,利用redis上localhost:6379 (默认位置)。现在,文档中说“如果要连接到一个redis服务器,我们不需要添加任何配置代码localhost:6379."
所以我像往常一样开始了我的redis:
提出了一个简单的请求
{
"uid":"123",
"message":"hello world"
}
到这个控制器
@PostMapping("/api/v1/space/post/")
@ResponseBody
public ResponseEntity<String> postMessage(@RequestBody Message message) throws Exception {
if(messageService.postMessage(message.getUid(), message.getMessage())){
return ResponseEntity.ok("it works!");
} else{
return ResponseEntity.badRequest().body("something's wrong");
}
}
一切都很顺利,日志甚至声明哈希已经创建。但是,当我执行redis cli并检查任何键时,什么都没有。
我也试着用莴苣连接工厂连接
@Bean
public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
return new LettuceConnectionFactory("localhost", 6379);
}
但是运气不好,我猜SpringBoot是以某种方式连接到一个嵌入式redis的,这不是我想要的,那么如何连接到redis示例呢?
编辑
以下是消息repo&service repo返回一个monognorePublisher,在文档中,mono要么返回一个元素,要么返回一个错误,这是一个错误吗
回购:
private final ReactiveRedisOperations<String, Message> redisTemplate;
public MessageRepository(ReactiveRedisOperations<String, Message> redisOperations) {
this.redisTemplate = redisOperations;
}
public Mono<Void> save(String authorId, String msg) {
Message message = new Message(authorId, msg);
Mono<Void> res = Mono.when(redisTemplate.<String, Message>opsForHash().put("space", message.getId(), message),
redisTemplate.opsForZSet().add(message.getUid(), message, message.getTimestamp().toEpochDay())).then();
System.out.println(res.toString());
return res;
}
服务:
@Autowired
private MessageRepository repo;
public Boolean postMessage(String authorId, String msg) throws Exception {
try{
repo.save(authorId, msg);
}
catch (Exception e){
throw new Exception(e);
}
return true;
}
以下是ReactiveRedisoOperation配置:
@Bean
public ReactiveRedisOperations<String, Message> redisOperations(ReactiveRedisConnectionFactory factory) {
Jackson2JsonRedisSerializer<Message> serializer = new Jackson2JsonRedisSerializer<>(Message.class);
RedisSerializationContext.RedisSerializationContextBuilder<String, Message> builder = RedisSerializationContext
.newSerializationContext(new StringRedisSerializer());
RedisSerializationContext<String, Message> context = builder.value(serializer)
.hashKey(new StringRedisSerializer()).hashValue(serializer).build();
return new ReactiveRedisTemplate<>(factory, context);
}
暂无答案!
目前还没有任何答案,快来回答吧!