This?application consist of multiple micro services which interact with each other. We would create event driven construct, where services would connect to messaging layer and would publish and subscribe events to messaging layer.
https://www.metakoder.com/blog/using-redis-with-spring-boot
here?.We would set up messaging between these two services. Whenever a new payment would be created using Payment Service, An event would be published to the messaging layer. Our Account service would receive this event and update the balance in accounts.
release of the repository.
Messaging Layer.
embedded-redis?as dependency in our maven project.
dependency>
stop?redis functions with the messaging-layer service's life-cycle.
Below is the sample code.
redisServer.stop();
}
}
payment-service?to publish event on messaging layer.
We need to add below dependency to out maven project. This is required to connect Redis Server.
dependency>
ChannelTopic?.
ChannelTopic is the topic/queue where our Payment Service would publish the events. We are configuring topic named "payments" here. This is the same topic on which Account Service would subscribe to.
return template;
}
That's all needed to setup in our application to connect and publish event to our Redis server.
Payment?object to string correctly on the topic. Here I am publishing whole payment object as an event but we could create a custom event as well to publish.
Now we can autowire our Redis Template to any service layer to send a event to our payment topic.
RedisMessagePublisher?and used that in service layer PaymentEventHandler.
RedisMessagePublisher. But Redis Template can be used directly from any class. Below is how I am using the Redis Template.
publisher.publish(payment);
}
}
Account Service?to subscribe events from our "payments" topic at Redis Server.
We need to add below dependency to out maven project.
dependency>
Message Listener?and from there we can use the event as we need. We need to implement onMessage method of MessageListener interface. In our case I am using this event to adjust the balance in Accounts.
Message Listener.
new String(message.getBody()));
}
}
RedisMessageListenerContainer?. Below is how I have have configured the relevant components to subscribe the event.
return container;
}
}
We need ChannelTopic, JedisConnectionFactory and we need to configure these in RedisMessageListenerContainer along with the Message Listener.
That's it!!! This is all you need to use Redis Pub/Sub with Spring boot application.
here. And run above mentioned Accounts and Payments services with Messaging layer service.
}'
Lets check the created accounts:
}
}
}
]
}
}
}'
This will create new payment in payment service and an event is published at our Redis payments topic. As Account Service subscribe to this topic , It will receive the event and process it. You will notice below log in Account Service
And now if you fetch the accounts again. You will notice that balance in accounts have been updated
}
}
}
]
}
}
The benefit of using the Messaging Layer is that there is no direct communication between services and this could result in better decoupling and scalability of the application.
以上就是土嘎嘎小编为大家整理的Using Redis Pub/Sub with Spring Boot相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!