Showing posts with label Jboss. Show all posts
Showing posts with label Jboss. Show all posts

Friday, 6 September 2013

JBoss 7.1.1.Final clustering with mod_proxy and Apache 2.2

This article shows how to configure a cluster made up of two JBoss AS 7.1.1.Final instances and how to config Apache Web Server to implement the so-called load balanced mechanism.
The version of Apache used to implement this example is Apache 2.2
Firstly, I would like to show how to create two clustered nodes with JBoss standalone version.
Finally, I will outline the steps needed to configure the Apache server to behave just like a load balancer element.
JBoss Cluster configuration
It is extremely easy to create a JBoss cluster based on the Standalone modality.
Imagine that the Jboss server is installed in the following filesystem location.
\JBOSS_HOME\
For dealing with a simple and non-infrastructure complex example, this example deploy two clustered nodes in the same machine but listening different ports.
Thus, we are going to create two nodes called: node1 and node 2
For creating each node, the following steps must be followed:
1) Copy the folder \JBOSS_HOME\standalone to \JBOSS_HOME\ with the name, for instance, “standalone_node1”
2) Copy the folder \JBOSS_HOME\standalone again to \JBOSS_HOME\ with the name, form instance, “standalone_node2”
3) In the \JBOSS_HOME\bin create a “bat” file called, for instance, “cluster_node1.bat” and include the following sentence that run and deploy the instance of the node1.
./standalone.bat -c standalone-ha.xml -b 127.0.0.1 -u 230.0.0.4 -Djboss.server.base.dir=../standalone_node1 -Djboss.node.name=node1 -Djboss.socket.binding.port-offset=100
 4) In the \JBOSS_HOME\bin create a “bat” file called, for instance, “cluster_node2.bat” and include the following sentence that run and deploy the instance of the node1.
./standalone.bat -c standalone-ha.xml -b 127.0.0.1 -u 230.0.0.4 -Djboss.server.base.dir=../standalone_node2 -Djboss.node.name=node2 -Djboss.socket.binding.port-offset=200
5) Finally, execute in different cmd console each of the “bat” files in order to lauch and make available the JBoss cluster.
APACHE LOAD BALANCER CONFIGURATION
For activate the load balancer feature of the Apache, I assume in this example to do so by using the mod_proxy module that is included in this version of Apache Web Server. It is not the best option to configure a load balancer node, because the best protocol called ajp is not used. Instead, all the communication is based on the common http protocol.
In the file \APACHE_HOME\conf\httpd.conf several modifications must be done:
1) Activate the following modules by adding these sentences
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_scgi_module modules/mod_proxy_scgi.so

Be aware that all this *.so files are included in the distributable 2.2.* version of Apache
2) Create a virtual host as follow:
<VirtualHost localhost:90>
    ServerName localhost:90
    DocumentRoot "d:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
    # Enable forward proxy
    ProxyRequests On
    ProxyVia On
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    # Enable reverse proxy
    ProxyPass / balancer://mycluster/ stickysession=JSESSIONID
    ProxyPassReverse / http://localhost:8180/
    ProxyPassReverse / http://localhost:8280/
    <Proxy balancer://mycluster>
        BalancerMember http://localhost:8180/ route=node1
        BalancerMember http://localhost:8280/ route=node2
        # Set counting algorithm to more evenly distribute work:
        ProxySet lbmethod=byrequests
    </Proxy>
    # Enable load balancer management
    <Location /balancer-manager>
        SetHandler balancer-manager
    </Location>
    <Directory "htdocs">
        AllowOverride AuthConfig
    </Directory>
</VirtualHost>

As you can see, this load balancer configuration points to the two JBoss instances deployed by way of the http ports (8180 for node 1 and 8280 for node2).
 The Jboss node1 listen the request in the port 8180 because when the instance is launched , the port offset is indicated properly in the command showed previously (8080 + 100 offset=8180). The same for the node 2 (8080 + 200=8280).
./standalone.bat -c standalone-ha.xml -b 127.0.0.1 -u 230.0.0.4 -Djboss.server.base.dir=../standalone_node1 -Djboss.node.name=node1 -Djboss.socket.binding.port-offset=100
./standalone.bat -c standalone-ha.xml -b 127.0.0.1 -u 230.0.0.4 -Djboss.server.base.dir=../standalone_node2 -Djboss.node.name=node2 -Djboss.socket.binding.port-offset=200

With these steps, you have in your own machine a Jboss cluster comprised of two nodes and in front of them an Apache Web service balancing the petitions.





Sunday, 2 December 2012

JAX-WS under JBOSS based on Maven (I)

This entry shows how to create a simple Web service based on JAX-WS. The eclipse project is a Maven project and therefore the following elements are necessary to implement this:

1) Maven 3.0.4.

2) JUnit 4.0

3) JDK 1.5

4) JAX-WS 2.2.6

5) SOAP UI testing tool.

6) Jboss Application Server 6.1

This first view of a web service implementation conveys only a simple operation but It is a good first movement to show how the JAX-WS is and the way that a simple web service can be implemented.

From an architecture point of view, this very simple example highlights the lightweight implementation without bearing in mind the contract and contract coding decoupled approach.

This article outlines the following stages.

1)   Eclipse Maven project creation.

2)  JAX-WS –based web service implementation.

3) Web Service package generation.

4) Jboss web service deployment.

5) Verifying the web service deployment

6) Testing the Web Services by using SOAP UI TestSuite.            

Description of every step

First stage. Eclipse Maven project creation.

Before creating a new maven project, eclipse maven must be installed thoroughly. I assume that you have already installed the plugin with no problems.

In order to create the eclipse project, click the new project and in the Maven folder select the Maven Project option and click “next” button.

 

In this case, I have decided to create a non-archetype project; therefore I have checked the “create a simple project” option.

 

The result is a new eclipse project respecting the maven directory structure as following

Second stage. JAX-WS –based web service implementation.

Firstly, I have added several maven dependencies to the pom.xml maven file located in the root of the project.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>greeting.services.com</groupId>

  <artifactId>GreetingWS</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>war</packaging>

  <name>GreetingWS</name>

  <dependencies>

       <dependency>

      <groupId>com.sun.xml.ws</groupId>

      <artifactId>jaxws-rt</artifactId>

      <version>2.2.6</version>

    </dependency>

    <dependency>

       <groupId>junit</groupId>

       <artifactId>junit</artifactId>

       <version>4.10</version>

</dependency>

  </dependencies>

</project>

JAX-WS and JUnit dependencies are added. Please, do be aware that in this case JUnit are not to be used but it is important for future JAX-WS examples based on this first approach.

Once the file is saved, immediately the maven engine search the dependencies within the repositories configured and download them into the local maven repository.

Now, it is time to create the Web service class. For doing this, you can create a file source like this.

package com.greeting.ws;

 

import javax.jws.WebService;

import javax.jws.soap.SOAPBinding;

import javax.jws.soap.SOAPBinding.Style;

 

 

@WebService

@SOAPBinding(style=Style.RPC)

public class GreetingWS {

       public String formalGreeting(String name){

      

             return "Hello boy :" + name;

       }

 

}

As you can see, several issues are worth being commented.

Firstly, the “import” include in the source file the dependencies needed to use @WebService attribute and @SOABinding.

If you want your class to behave such as a Web service class, you must use both attributes.

@WebService attribute establishes the class as a web service in itself.

@SOAPBinding determinates the SOAP style, in this case RPC style (Document style is permitted as well).

As you can see, you do not need to draw on extra attributes to establish the methods of this class as web service methods.

Finally, two files are need and you must add to the project under the WEB-INF folder.

web.xml is the common web specification file all web-based applications must comprise.

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"

  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

  id="WebApp_ID" version="2.5">

 

  <display-name>greetingExample</display-name>

 

  <listener>

    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>

  </listener>

  <servlet>

    <servlet-name>greetingWs</servlet-name>

    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>

    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>greetingWs</servlet-name>

    <url-pattern>/GreetingWS</url-pattern>

  </servlet-mapping>

  <session-config>

    <session-timeout>120</session-timeout>

  </session-config>

</web-app>

sun-jaxws.xml is the specific file that defines the endpoint.

<?xml version="1.0" encoding="UTF-8"?>

<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">

  <endpoint name="GreetingWS" implementation="com.greeting.ws.GreetingWS" url-pattern="/GreetingWS" />

</endpoints>

 

 

To access to the web service definition, these files define the following url.

http://localhost:8080/GreetingWS-0.0.1-SNAPSHOT/GreetingWS?wsld

 

Third stage. Web Service package generation

Under maven environment, two maven goals must be executed. Firstly, the compile goal compiles the class and generates the target files.

Next, you must follow the same operation by establishing the “package” goal. This will generates the WAR file.

GreetingWS-0.0.1-SNAPSHOT.war

Fourth stage. Jboss web service deployment.

This stage is quite simple. You have just to copy the war file in the JBOSS_HOME\ server\default\deploy

Then, execute run.bat file located in JBOSS_HOME\bin folder.

Fifth stage. Verifying the web service deployment

 By typing the url http://localhost:8080/GreetingWS-0.0.1-SNAPSHOT/GreetingWS?wsld you can see the web service details.

Sixth stage. Testing the Web Services by using SOAP UI TestSuite.

The SOAP UI suite provides two modes of use. In this case, it is embedded in the Eclipse IDE.

The first step is to create a new SOAP UI project.

To create the binding follow the two next steps.

 

As you can see, the request is created and the web service can be tested properly.

 

Submitting the request you obtain the Web service response.

 

As a conclusion, you can create a simple web service by using JAX-WS and testing it with SOAP UI test suite.