-
Notifications
You must be signed in to change notification settings - Fork 182
Spring
There are several Spring components that make application development on Cloud Foundry simple.
- STS - Eclipse tooling support
- Cloud namespace - configure cloud based services
- Profiles - group together sets of related bean definitions (local/cloud/dev/prod) in XML or Java
- Spring Data - Spring programming model support for writing RDBMS/JPA, Redisand MongoDBservices.
Please refer to the Getting Started Guide for STS users
To reference cloud based services within Spring use the new cloud namespace. The cloud namespace provides support for defining beans backed by services.
<cloud:data-source/>
<cloud:redis-connection-factory/>
<cloud:mongo/>
This style works as long as you have only one service of each type bound to the application.
If you want to bind to specific services (perhaps because you have more than one of a kind of service, for example two MySql services), you need to specify the service-name attribute as follows:
<cloud:data-source service-name="inventory-db"/>
<cloud:data-source service-name="pricing-db"/>
In your Java code, simply add @Autowired dependencies for each bounded service:
@Autowired DataSource dataSource;
@Autowired RedisConnectionFactory redisConnectionFactory;
@Autowired Mongo mongo;
You have access to services without breaking a sweat.
There is also a properties factory bean:
<cloud-service-properties id="serviceProperties"/>
and the properties exposed are in the form <service-name>.<option>
.
The options are from the credentials
section of the service info (so
username
, password
are common, and you might see things like
virtualhost
for rabbit). The properties that are exposed can be
eyeballed in the simple JS application listed in Node.
The service name will be used as the bean id by default, but if you would prefer to use a separate value for that, you can specify the 'id' attribute. Regardless of how the id is determined, you can specify the @Qualifier annotation to pick the right bean or even use the XML based configuration. Please check the Spring Framework documentation for more details.
To access the namespace, add the following to your maven pom.xml
<dependency>
<groupId>org.cloudfabric</groupId>
<artifactId>runtime</artifactId>
<version>0.5.0-LD</version>
</dependency>
and the following maven repository location
<repositories>
<repository>
<id>spring-milestone</id>
<url>http://s3.amazonaws.com/private.maven.springsource.com/milestone/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
If that does not work you can download and install the locally.
Please refer to the Getting Started Guide for Command Line (VMC) users
The initial set of services offered on Cloud Foundry are
Spring provides strong integration with all of these services. The features that Spring offers for each service is discussed in the following sections.
Spring has always provided great support for relational databases. Spring's JDBC and ORM support can be used within Cloud Foundry.
A new Spring project, Spring JPA, provides great infrastructure support to further kickstart your development. It will automatically provide implementations of of Repository interfaces including support for custom finder methods. There is also integration wit QueryDSL so that you can write type-safe queries.
The bootstrapping of a datasource is the only thing you need to be concerned with.
If you are using Spring's cloud namespace, then in your Spring XML configuration file declare the following element
<cloud:data-source/>
The service name will be used as the bean id by default, but if you would prefer to use a separate value for that, you can specify the 'id' attribute. You can then reference within other Spring XML definitions or within your classes using @Autowired or @Inject annotations.
Part of the umbrella Spring Data project, the Spring Data Key Value provides integration with Redis.
To quote the Redis project home page: "Redis is an advanced key-value store. It is similar to memcached but the dataset is not volatile, and values can be strings, exactly like in memcached, but also lists, sets, and ordered sets. All this data types can be manipulated with atomic operations to push/pop elements, add/remove elements, perform server side union, intersection, difference between sets, and so forth. Redis supports different kind of sorting abilities."
Spring Redis integration provides easy configuration and access to Redis from Spring application. Offers both low-level and high-level abstraction for interacting with the store, freeing the user from infrastructural concerns.
The sample application RetwisJ is running on Cloud Foundry and showcases various Spring Redis features. Please read the RetwisJ documentation and look at the source code.
The WGRUS sample uses MongoDB for the Spring Integration MessageStore
(claim check pattern). Look at the source code. Spring Mongo at present does not enforce authentication so the <cloud:mongo/>
feature gives you an unautheticated instance of Mongo
. If you are using Spring Mongo, then the MongoTemplate
has settings for username and password (see Spring MongoDB docs).
TBD: link to Spring Data Mongo docs
TBD