Thursday, November 18, 2010

Apache load balancing for Tomcat Example (with Wamp server apache module (Apache2.2.xx))

This is continued from the previous post in here.

Scenario: You have application running on two tomcat servers. Example is for localhost. Replcae it with relevant IP addresses which the tomcat servers are runing.

1. http://localhost:8080/app/
2. http://localhost:8090/app/

You need to load balance between two tomcat servers through apache server as well as to hide the actualURLs from outside.

Here is the Step by Step instructions.

1. First follow the previous post and configure apache proxy.
2. Let's write a balancer configuration named "mybalancer" with two members who are two tomcat servers. The load should be balanced between those two members (1:1). Add the following segment to httpd.conf file.

<PROXY balancer://mybalancer>
BalancerMember http://localhost:8080
BalancerMember http://localhost:8090
</PROXY>

3. Now change the ProxyPass statement to refer the balancing cluster. For that replace line

ProxyPass /proxydemo/ http://localhost:8080/app/

with

ProxyPass /proxydemo/ balancer://mybalancer


4. Then replace the ProxyPassReverse statement line

ProxyPassReverse /proxydemo/http://localhost:8080/app
with

ProxyPassReverse /proxydemo/ balancer://mybalancer

What you did was to place the load balancer behind the apache server. So the requests come to http://localhost:9090/proxydemo (assuming Apache runs on port 9090,replace with your own port)will be redirected to either of the balancing members. But the actual URL is hidden to outside.

If you want to access one of the resource hosted on tomcat servers such as

http://localhost:8080/app/service.jsp

You can access it through apache load balancer through

http://localhost:9090/proxydemo/app/service.jsp

when someone access the above URL requests will be balanced to one of the members, in this case 1:1. You can shut down one tomcat server and  try the above URL. Then it will work as another server is still working. But if you have shut down both servers it will display as server temporaly not available.

Ths example was assuming there are two tomcat servers running on the same machine (port 8080 and port 8090) and apache server is running on port 9090. You can try this on distributed servers and replace localhost with IP addresses.

Apache proxy configuration for Tomcat Example (with Wamp server apache module (Apache2.2.xx))

Apache Proxy Configuration - To place Tomcat server behind apache server

Scenario: You have tomcat server applications (http://localhost:8080/app/, etc)which need to be placed behind a apache server. In this example apache port is 9090. You can have your own ports.

First download WampServer2.0i file and install in your machine.
Start Wamp and start apache service (Click on wamp icon in toolbar and go to apache-Service-Start/Resume Service )
In you wamp installation directory go to it's apache module and open httpd.conf file. (WAMP_HOME\bin\apache\Apache2.2.11\conf\httpd.conf)

1. Here you have to enable following modules.
                                  LoadModule proxy_module modules/mod_proxy.so
                                  LoadModule proxy_http_module modules/mod_proxy_http.so 
                                  LoadModule rewrite_module modules/mod_rewrite.so

Search them in httpd.conf file and uncomment them. (Remove # mark infront of each module entry)
Now restart Apache (Click on wamp icon in toolbar and go to apache-Service-Restart Service )

Veryfy whether modules are enabled
Click on Wamp icon and go to Apache-Apache Modules.
Check whether above modules are marked as enabled (If not by clicking on each module enable them and restart wamp)

Now you have enabled the required modules.

2. Add following line to the httpd.conf file

          ProxyRequests Off

3. Now add theproxy stanza as follows to httpd.conf


    Order deny,allow
    Allow from all


4. Then you have to enable URL rewriting as follows

      RewriteEngine on

5. For each server which need to be placed behind apahe server add a ProxyPass statement as follows. In here  proxydemo is the namespace for the application access.

ProxyPass /proxydemo/ http://localhost:8080/app/ 


6. For each ProxyPass statement add ProxyPassReverse statement to rewrite Location headers information before passing redirect response back to the client.
ProxyPassReverse /proxydemo/ http://localhost:8080/app/ 

7. To avoid 404 errors after URL without ending slash add the following line to httpd.conf
    
     RewriteRule ^/proxydemo$ /proxydemo/ [R]


Now You have done proxypassing.
Here after when you access apachserver proxypass proxydemo by http://localhost:9090/proxydemo , it will redirect to http://localhost:8080/app/ but actual URL is hidden from outside.

Imagine now you have service.jsp file in http://localhost:8080/app/service.jsp . To access this through Proxy the URL for outside will be http://localhost:9090/proxydemo/service.jsp.

Please ask if you got any problems.

In my next post apache load balancing for tomcat server will be discussed with an example.

Thursday, November 11, 2010

Java-Simple way to check command line input to enter a number-Give error message if not a number and ask again to input

If you want to take a commandline input through BufferedReader or Scanner and you want user to enter a number. So in this solution user is asked to enter a number and if it is not a number (throws NumberFormatException) error message is displayed and again asks for user to enter. Until user enters a number this continues. This is simple.

Here is the code with comments explaining the code.

import java.io.*;
class InputNumber{
 public static void main(String args[]){
  boolean success=false;
  String value=null;
  int number_value=0;//number value is stored
  System.out.print("Enter Number:");
              
              //loop continues until user entered a number,success 
              //will assigned true when user entered a number and while
              //loop will terminate
              while (!success){
   try{
      BufferedReader bf=new BufferedReader
         (new InputStreamReader(System.in));
      value=bf.readLine();
      number_value=Integer.parseInt(value);


           success=true;//Come to this point only if the 
                                    // exception is not thrown
     System.out.println("\nSuccess! You Entered a Number: "+number_value);

   }catch(IOException e){


   }catch(NumberFormatException e){//When string did 
                                      //not contained a numer it goes here
    System.out.print("\nWrong value, please enter a number!\nEnter Number :");
    success=false;

   }
  }
 }


}