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.
We would implement this set up in 3 steps.
release of the repository.
Step 1: Setting up Redis server
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();
}
}
6379 by default or at specified port.
Step 2: Publish payment event on Redis Server
payment-service to publish event on messaging layer.
Add Redis client dependency
We need to add below dependency to out maven project. This is required to connect Redis Server.
dependency>
Add Messaging configuration
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);
}
}
Step 3: Subscribe event from Redis Server
Account Service to subscribe events from our “payments” topic at Redis Server.
Add Redis client dependency
We need to add below dependency to out maven project.
dependency>
Add Message Listener
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()));
}
}
Add Messaging configuration
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.
Now if you create 2 accounts, say 1 and 2. Run below command twice to do that. It will create account number 1 and account number 2 both with the balance of 200.
}'
Lets check the created accounts:
}
}
}
]
}
}
Now if we make payment from account 1 to account 2
}'
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.