Follow this section if you want to deploy your application to Pivotal Web Services (PWS).
The first thing we need to do to create a Cloud Foundry application is to set up an account on PWS. This is documented at http://docs.run.pivotal.io/starting/.
You will be asked to create an organization and each new organization will have a default space (development) created within the organization. As shown in the following screenshot:
On the left-hand side navigation bar, you will see a link to Tools from which you download the CLI. It is also available from the developer console. Select the appropriate package for your operating system:
Our application simply needs to be assembled for deployment.
The good thing with PWS is that you don't have to push your sources to deploy. You can generate the JAR, push it, and everything will be autodetected.
We can package this for deployment with the following command:
./gradlew assemble
This will create a jar file in the build/libs
directory. At this point, you can execute the following command. The following command targets your deployment to your space within PWS (run.pivotal.io
):
$ cf login -a api.run.pivotal.io -u <account email> -p <password> -o <organization> -s development API endpoint: api.run.pivotal.io Authenticating... OK Targeted org <account org> Targeted space development API endpoint: https://api.run.pivotal.io (API version: 2.33.0) User: <account email> Org: <account organization> Space: <account space>
Once you have successfully logged in, you can push your jar with the following command. You will need to come up with an available name:
$ cf push your-app-name -p build/libs/masterSpringMvc-0.0.1-SNAPSHOT.jar Creating app msmvc4 in org Northwest / space development as [email protected]... OK Creating route msmvc4.cfapps.io... OK Binding msmvc4.cfapps.io to msmvc4... OK Uploading msmvc4... Uploading app files from: build/libs/masterSpringMvc-0.0.1-SNAPSHOT.jar Uploading 690.8K, 108 files Done uploading OK Starting app msmvc4 in org <Organization> / space development as <account email> -----> Downloaded app package (15M) -----> Java Buildpack Version: v3.1 | https://github.com/cloudfoundry/java-buildpack.git#7a538fb -----> Downloading Open Jdk JRE 1.8.0_51 from https://download.run.pivotal.io/openjdk/trusty/x86_64/openjdk-1.8.0_51.tar.gz (1.5s) Expanding Open Jdk JRE to .java-buildpack/open_jdk_jre (1.4s) -----> Downloading Open JDK Like Memory Calculator 1.1.1_RELEASE from https://download.run.pivotal.io/memory-calculator/trusty/x86_64/memory-calculator-1.1.1_RELEASE (0.1s) Memory Settings: -Xmx768M -Xms768M -XX:MaxMetaspaceSize=104857K -XX:MetaspaceSize=104857K -Xss1M -----> Downloading Spring Auto Reconfiguration 1.7.0_RELEASE from https://download.run.pivotal.io/auto-reconfiguration/auto-reconfiguration-1.7.0_RELEASE.jar (0.0s) -----> Uploading droplet (59M) 0 of 1 instances running, 1 starting 1 of 1 instances running App started OK App msmvc4 was started using this command `CALCULATED_MEMORY=$($PWD/.java-buildpack/open_jdk_jre/bin/java-buildpack-memory-calculator-1.1.1_RELEASE -memorySizes=metaspace:64m.. -memoryWeights=heap:75,metaspace:10,stack:5,native:10 -totMemory=$MEMORY_LIMIT) && SERVER_PORT=$PORT $PWD/.java-buildpack/open_jdk_jre/bin/java -cp $PWD/.:$PWD/.java-buildpack/spring_auto_reconfiguration/spring_auto_reconfiguration-1.7.0_RELEASE.jar -Djava.io.tmpdir=$TMPDIR -XX:OnOutOfMemoryError=$PWD/.java-buildpack/open_jdk_jre/bin/killjava.sh $CALCULATED_MEMORY org.springframework.boot.loader.JarLauncher` Showing health and status for app msmvc4 in org <Organization> / space development as <Account Email> OK requested state: started instances: 1/1 usage: 1G x 1 instances urls: msmvc4.cfapps.io last uploaded: Tue Jul 28 22:04:08 UTC 2015 stack: cflinuxfs2 buildpack: java-buildpack=v3.1-https://github.com/cloudfoundry/java-buildpack.git#7a538fb java-main open-jdk-like-jre=1.8.0_51 open-jdk-like-memory-calculator=1.1.1_RELEASE spring-auto-reconfiguration=1.7.0_RELEASE state since cpu memory disk details #0 running 2015-07-28 03:05:04 PM 0.0% 450.9M of 1G 137M of 1G
There is a lot that the platform is performing on your behalf. It provisions a container and detects which buildpack is needed, in this case, Java.
It then installs the required JDK and uploads the application we pointed it to. It creates a route to the application, which it reports to us, and then launches the application for us.
Now you can view the application on the developer console:
On selecting the highlighted route, the application will be available for use. Visit http://msmvc4.cfapps.io, then you will see the following screenshot:
Bravo!
The only thing that will not work yet is the file upload. However, we will fix that in a minute.
In your application services, you can choose between many services. One of them is Redis Cloud, which has a free plan with 30 MB of storage. Go ahead and select this plan.
In the form, choose whatever name you fancy and bind the service to your application. By default, Cloud Foundry will inject some properties in relation to the service in your environment:
cloud.services.redis.connection.host
cloud.services.redis.connection.port
cloud.services.redis.connection.password
cloud.services.redis.connection.uri
These properties will always follow the same convention, so it will be easy to keep track of your services as you add more.
By default, Cloud Foundry launches Spring applications and activates the Cloud profile.
We can take advantage of this and create an application-cloud.properties
file in src/main/resources
, which will be used when our application is running on PWS:
spring.profiles.active=prod,redis spring.redis.host=${cloud.services.redis.connection.host} spring.redis.port=${cloud.services.redis.connection.port} spring.redis.password=${cloud.services.redis.connection.password} upload.pictures.uploadPath=file:/tmp
This will bind our Redis instance to our application and activate two additional profiles: prod
and redis
.
We also changed the path where the uploaded pictures will land. Note that using the file system on the cloud obeys different rules. Refer to the following link for more details:
http://docs.run.pivotal.io/devguide/deploy-apps/prepare-to-deploy.html#filesystem
The last thing we need to do is deactivate one Spring Session feature that will not be available on our hosted instance:
@Bean @Profile({"cloud", "heroku"}) public static ConfigureRedisAction configureRedisAction() { return ConfigureRedisAction.NO_OP; }
For more information, visit http://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository-sessiondestroyedevent.
You will see that this configuration will also be applied on Heroku.
That's it. You can reassemble your web application and push it again. Now, your sessions and application cache will be stored on Redis!
You may want to explore the marketplace for other available features such as binding to data or messaging services, scaling the application, and managing the health of the applications that are beyond the scope of this introduction.
Have fun and enjoy the productivity the platform provides!