My microservice is required to direct requests between 2 different servers via TCP connection. Using the current TCPClient, we are required to provide the host & port numbers which means that I can connect to one server
TcpClient.create().host(host).port(port)
I did see that netflix ribbon can support tcp but most examples are related to http & eureka
I tried using netflix ribbon where i define the ip address and port number under listOfServers in configuration, and annotate tcpClient bean as load balanced. This does not work. Is there any other solution for client side load balancing or should we create multiple tcp clients and rotate the requests between the different tcp clients?
@Bean
@LoadBalanced
public TcpClient customTcpClient(){
return TcpClient.create();
}
SERVICE:
ribbon:
listOfServers: 123456:1500, 223456:1500
Error: Connection refused. No further information
Note: in the original implementation, it is created with host & port, and then autowired into another class to send request and receive response
EDIT: Based on this discussion, is it right to say that only RestTemplate has support for @LoadBalanced and as such my implementation will not work? https://stackoverflow.com/questions/39587317/difference-between-ribbonclient-and-loadbalanced#:~:text=TL%3BDR%3A%20%40LoadBalanced%20is,is%20used%20for%20configuration%20purposes.
Q.) Is it right to say that only RestTemplate has support for @LoadBalanced and as such my implementation will not work?
Yes.
@LoadBalancedannotation is primarily designed to work with RestTemplate for HTTP-based communication and doesn't directly support non-HTTP protocols like TCP. In your case you are using aTcpClientfor TCP communication, you won't be able to use the @LoadBalanced annotation because it's not designed to work with TCP.In this scenario, you'll need to manage the load balancing yourself. One approach could be to manually create multiple instances of your TcpClient with different host and port combinations, and then manage the distribution of requests among these instances in your code. This approach would involve rotating the requests between the different instances of your TcpClient to achieve the load balancing.
Approach you can follow:
@Configuration public class TcpClientConfig {
}
Note: I use
AtomicIntegerin my TcpLoadBalance. The primary use ofAtomicIntegeris when you are in a multithreaded context and you need to perform thread safe operations on an integer without using synchronized. More info checkout this https://stackoverflow.com/a/4818916/12866947