Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

Spring boot reactive redis connection

$
0
0

I am new to redis reactive implementation. I am trying to setup a backend which utilises redis on localhost:6379 (default location). Now the documentation stated "We do not need to add any code for configuration if want to connect to a Redis server at localhost:6379."

So i started my redis as usual:

enter image description here

made a simple post request

{"uid":"123","message":"hello world"}

to this controller

@PostMapping("/api/v1/space/post/")@ResponseBodypublic 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");    }}

Everything went fine, the log even stated that the hash has been created. But, when i do redis-cli and check for any keys, nothing is there.

enter image description here

I also tried to connect using LettuceConnectionFactory

    @Beanpublic ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {    return new LettuceConnectionFactory("localhost", 6379);}

Yet no luck, I am guessing that the spring boot is somehow connected to an embedded redis which is not what I want, so how do I connect to a redis instance?

EDIT

Here are is the message repo & serviceThe repo returns a MonoIgnorePublisher, in the documentation, Mono either returns an element or error, is this an error? :

Repo:

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;    }

Service:

    @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;           }

Here is the ReactiveRedisOperation config:

@Beanpublic 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);}

Viewing all articles
Browse latest Browse all 2204

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>