Reputation: 2120
I have 2 webservers in load balanced environment and both webservers have 2 webapps deployed in their tomcat servers. Two different domain names are used to access these two web applications.
I am struck up in a situation where I have to reload something in both webservers for both web apps separately. So I have to a send http request to each webserver and each webapp to reload (Basically I want to bypass the load-balancer).
Is there a way to add a HTTP header so that I can send my request to specific webserver at runtime for specif domain names? Or is there any other way to achieve this?
Note: This HTTP request can be sent from java code deployed in any one of the webserver. So basically we can assume it as the communication between the webservers within the local network.
Upvotes: 2
Views: 12107
Reputation: 2120
Here is the solution I am going to try. It seemes worked for a small proof of concept and i am going to try in actual application.
Solution:
The URL should be the IP address or server host name of the webserver.
Ex: http:///
For the HTTP request set the "Host" header as the actual domain name that u want to access. In my case, it is the second webapp (test.myapp2.com) that i have deployed in my tomcat.
Send the HTTP request.
First it worked when i tried with the "Poster" addon in firefox but it didnt work in my Java code. It did not show any error either. It simply ignored the "Host" header that I set. The java library restricts overriding some of the Request header values and "Host" is one of them. So I had to set it explicitly to accept the restricted headers.
The initial code I had is:
URL url = new URL("http://<IP-ADDRESS>/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Host", "test.myapp2.com");
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
New Fix:
String allowRestrictedHeader = System.getProperty("sun.net.http.allowRestrictedHeaders");
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
try {
URL url = new URL("http://<IP-ADDRESS>/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Host", "test.myapp2.com");
// use connection
.
.
.
}
finally {
if (allowRestrictedHeader == null) {
System.clearProperty("sun.net.http.allowRestrictedHeaders");
} else {
System.setProperty("sun.net.http.allowRestrictedHeaders",
allowRestrictedHeader);
}
}
Upvotes: 0
Reputation: 54084
IMO this question is weird.
If you have a load balancer that means that you distribute the load between 2 backend servers i.e. who deploy the same applications and which must be consistent, meaning that both parts must have exactly the same database (since the same client could be served from one server in one connection or the other in a next request depending also on the configuration of the load balancer).
So in this case and since both parts should be in sync and the database essentially mirrored if you send a specific request that has a certain interpretation it does not matter which server receives it. The other server should also understand that this request came since the data are mirrored.
I.e. just make a specific update in the database and let the servers reload as soon as the database update is visible in the other server as well.
Unless you are not using a load balancer or you are not using it properly...
And to answer your question how to bypass the load balancer. When you use a load balancer you use either a virtual IP or a specific domain name that "points" to the load balancer and the load balancer redirects to an available server. So you could bypass the loadbalancer if you use directly the IP of the servers instead of the IP of the load balancer.
Upvotes: 0