As discussed in previous posts, GigaSpaces XAP supports near linear scalability of distributed data caches and grid processing algorithms. GigaSpaces can also deploy web application servers in the same highly available, self-healing environment in which it manages data caches, distributed computing algorithms, and remote software services.
This example will show how to build, deploy, and access REST services hosted in Jetty instances running in GigaSpaces XAP. If you haven’t yet, you should read my post on simple caching to learn how to configure GigaSpaces XAP and deploy the GigaSpaces infrastructure. All source code for these examples is available on Github.
Example Scenario
The scenario implemented for this example is a REST API that provides SKU (Stock Keeping Unit) data in JSON format. The URLs supported are:
/v1/items-by-color/{color}
(List items with the specified color)/v1/items-by-size/{size}
(List items with the specified size)/v1/items/{color}/{size}
(List items with the specified color and size)
The space is primed with SKU objects written by a simple feeder. Once the SKUs are loaded, the web servers can access the data via a standard HttpServlet
.
This logic is implemented in three files:
SKU.java
SKUFeeder.java
SKUServlet.java
Deploying the Web Tier
By default, XAP uses Jetty as the web server, but Apache Tomcat is also an option. The unit of deployment for a web application in Gigaspaces XAP is a standard war file, with the addition of a Spring pu.xml
file to configure the distributed space:
rest-api.war
|
+ META-INF
|
+ spring
|
+ pu.xml
|
+ WEB-INF
|
+ classes
|
+ . . .
|
+ lib
|
+ . . .
|
+ web.xml
|
+ images
|
+ . . .
|
+ index.html
The pu.xml
file simply specifies the configuration of the space holding the data that will be used by the web tier:
. . .
<os-sla:sla cluster-schema="partitioned-sync2backup"
number-of-instances="2"
number-of-backups="1"
max-instances-per-vm="1"/>
<os-core:space id="local-proxy" url="/./rest-api-space"/>
<os-core:distributed-tx-manager id="transaction-manager"/>
<os-core:giga-space id="space"
space="local-proxy"
tx-manager="transaction-manager"/>
<os-core:space id="remote-proxy" url="jini:/*/*/rest-api-space"/>
<os-core:distributed-tx-manager id="remote-transaction-manager"/>
<os-core:giga-space id="remoteSpace"
space="remote-proxy"
tx-manager="remote-transaction-manager"/>
. . . .
For this example, the Jetty instances will run in the same processing units as the space partitions, but will access the distributed space as a whole. This is accomplished by accessing the remote space proxy in SKUServlet.java
:
space_ = (GigaSpace)getServletContext().getAttribute("remoteSpace");
Running the Example
To build and run this example:
- Set your
JSHOMEDIR
environment variable to the GigaSpaces installation directory. - Run
ant build
to build the example. - Run
bin/start-infrastructure.sh
to start the GigaSpaces infrastructure - Run
bin/deploy.sh
to deploy the space and Jetty instances - Run
bin/feed.sh
to feed SKUs into the space - Point your browser at
http://localhost:8081/rest-api/v1/items-by-color/Red
to see some data
If you’re intested in seeing the configuration of the infrastructure and the contents of the space, call bin/start-ui.sh
Load Balancing
In this example, with two Jetty instances running, the REST URLs are available on ports 8081 and 8082. If we started four instances, they would range from 8081 to 8084 (the starting port is configurable).
Obviously a web application server needs to provide a single external interface, even if multiple instances are running underneath to ensure reliability. GigaSpaces XAP provides a utility to dynamically update an Apache httpd load balancing configuration to reflect the deployed instances.
Summary
Deploying the web tier in GigaSpaces XAP provides your application with the same high availability and linear scalability that GigaSpaces offers to distributed data caches and common grid computing algorithms. Web server instances can even be spun up and shut down dynamically to handle erratic loads. If you’re looking to learn more about Grid computing with Java, visit our website.
This post implements the simplest possible example of running the web tier in GigaSpaces XAP. As such it has a few limitations, in particular, the constraint that the number of web application server instances must be the same as the number of space primary nodes. That constraint can be eliminated though the use of zones, which will be the topic of my next post.