Spring Boot Microservice on Cloud — Deploy to Cloud Foundry (Part 3)
What is Cloud Foundry
Cloud Foundry is an open source, multi-cloud application platform as a service (PaaS) governed by the Cloud Foundry Foundation.
Cloud Foundry’s container-based architecture runs apps in any programming language over a variety of cloud service providers.
This multi-cloud environment allows developers to use the cloud platform that suits specific application workloads and move those workloads as necessary within minutes with no changes to the application.
Supported Languages
Languages and frameworks that can be deployed using the buildpack lifecycle include:
In this article we are going to cover PART 3 of the below series :
- Introduction and creating our Microservices (Part 1)
- Mongo DB cloud integration (Part 2)
- Deployment to Cloud Foundry (Part 3)
- Deployment to AWS (Part 4)
Let’s get started.
Cloud Foundry Configuration
To go through this tutorial, we need to register for a free trial at pivotal Cloud Foundry.
Furthermore, the Cloud Foundry CLI needs to be installed.
As we are working with Java and Spring Boot App, we will use the eclipse plugin for Spring Boot to interact with Cloud Foundry.
Let’s install the plugin,
Once plugin is installed, we are ready to push our spring boot app to cloudfoundry, Click on Add Cloudfoundry Target
Provide the details of your pivotal cloudfoundry login,
Next steps are to,
Add manifest.yaml
for the cloudfoundry configuration
We will now add manifest.yaml
in /deployment/cloudfoundry
Now finally drag and drop the app on the cloudfounry target in Boot Dashboard
A simple microservice should deploy fine at this point, we are however using MongoDB cloud as we developed in PART2 of the series.
We are seeing following error in logs,
org.mongodb.driver.cluster : Exception in monitor thread while connecting to server shopping-list-cluster-shard-00-02-xxx.mongodb.net:27017 com.mongodb.MongoSocketReadException: Prematurely reached end of stream
MongoDB cloud requires us to whitelist the IP addresses that can access the database.
If we look at official documentation, Pivotal provides us with below command to get range of IP addresses it uses at AWS.
http://engineering.pivotal.io/post/public-ips-for-diego-cells/
curl https://ip-ranges.amazonaws.com/ip-ranges.json | ruby -rjson -e 'puts JSON.pretty_generate( JSON.parse(STDIN.read)["prefixes"].select {|v| v["region"] == "us-east-1" && v["service"] == "EC2"}.map{|v| v["ip_prefix"]} )'
If we run this , we get range of IP addresses. We can add all these IP addresses in the whitelist for our MongoDB cloud.
Another option is to install a service in Cloud Foundry like QuotaGuard that allows us to get load balanced static IP’s in front of our apps.
These options will work for a production ready multi cloud strategy.
For purpose of this tutorial, lets whitelist all IP addresses, this will allow any IP address with password to connect to MongoDB Atlas.
This is temporary access that will be deleted in 6 hrs.
Hide password and cluster name in spring boot
Adding whitelist to all IP exposes our application in github as our account details are in plain text, lets hide the password and pass it as a variable in cloudfoundry
Run below command to get list of apps,
To get environment of the app run,
cf env spring-boot-shopping-list-mongodb
Set the application-cloud.yml
properties,
cf set-env spring-boot-shopping-list-mongodb mongo.username testuser
cf set-env spring-boot-shopping-list-mongodb mongo.password testpassword
cf set-env spring-boot-shopping-list-mongodb mongo.cluster shopping-list-cluster-xyz.mongodb.net
We can now deploy the app again from eclipse IDE, ignore the manifest file while deploying as it will overwrite changes in the cloudfoundry env we just created.
Your application should start fine at this point.
Test the application
Let’s test the PUT call , you can get the URL of the app by right click the app in Boot Dashboard and select open browser
Test your app in POSTMAN,
Our microservice is deployed in multi cloud.
We have reached the end of this PART in the series , we commit our changes and create a tag.
🙌🥂🎉🎉🍺 The GitHub repository for the demo application: https://github.com/avinash10584/shoppinglist-springboot-microservice/
For PART3
https://github.com/avinash10584/shoppinglist-springboot-microservice/tree/PART3
Originally published at https://codingfullstack.com on May 20, 2020.