Example project showing how to interact with Redis using Spring Boot
Inspired by https://spring.io/guides/gs/spring-data-reactive-redis/
Go to https://start.spring.io/ and choose Lombok
, Reactive Web
, Reactive Redis
.
The Testcase CoffeeControllerTest.java uses testcontainers framework to leverage redis as a broker. Just run it, testcontainers will take care of firing up redis with Docker (just be sure to have Docker installed).
If you want to run the SpringredisApplication.java be sure to fire up redis with:
docker-compose up
See https://docs.spring.io/spring-data/data-redis/docs/2.1.1.RELEASE/reference/html/#redis:reactive
First we´ll need to configure Spring to connect reactively to Redis with the ReactiveRedisConnectionFactory with Lettuce behind the scenes:
@Bean
public ReactiveRedisConnectionFactory connectionFactory() {
return new LettuceConnectionFactory("localhost", 6379);
}
Interaction with Redis in reactive use cases is abstracted in Spring Data by ReactiveRedisTemplate. So we need to initialize a Bean:
@Bean
public ReactiveRedisTemplate<String, String> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
return new ReactiveRedisTemplate<>(factory, RedisSerializationContext.string());
}
Only if one uses channels and subscribes to it on Redis, these channels are present (see Understanding channels
in https://redisgreen.net/blog/pubsub-howto/).
To see the current channels in the Redis Docker container, check:
docker exec -it redisContainerName sh
redis-cli
pubsub channels
Spring Reactive WebClient: https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#webtestclient-tests
We need to use a curl / Postman / httpie to open up a HTTP connection to retrieve Server Side Events (SSE) from our channel subscriber:
curl -v http://localhost:8080/message/coffees
Now messages could be retrieved from the Redis Pub/Sub channel coffees:queue
.
On a second terminal windows we can now publish messages to Redis :
http POST http://localhost:8080/message/coffee/fooBarCoffee
See https://developer.okta.com/blog/2018/09/24/reactive-apis-with-spring-webflux#the-web-the-final-frontier for more details on how to implement Spring Webflux style REST endpoints.
https://spring.io/guides/gs/messaging-redis/
https://www.baeldung.com/spring-data-redis-pub-sub
https://spring.io/guides/gs/spring-data-reactive-redis/
https://docs.spring.io/spring-data/data-redis/docs/2.1.1.RELEASE/reference/html/#redis:reactive
https://www.baeldung.com/java-redis-lettuce
https://www.baeldung.com/spring-amqp-reactive
https://spring.io/guides/gs/reactive-rest-service/
https://www.baeldung.com/spring-5-webclient
https://dzone.com/articles/reactive-programming-with-spring-webflux