Codingfullstack

· 3 minute read
by Avinash Singh

What is CloudFoundry

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:

Language Framework
Java Spring
Ruby on Rails Sinatra
JavaScript Node.js
.NET .NET Framework
Python Python
PHP PHP
Go Go

  In this artile we are going to cover PART 3 of the below series :

 

Let’s get started.

 

Cloud Foundry Configuration

To go through this tutorial, we need to register for a trial here.

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 cloudfoundry.

Let’s install the plugin

spring-tools-plugin

Once plugin is installed, we are ready to push our spring boot app to cloudfoundry, Click on Add Cloudfoundry Target

spring-tools-plugin-2

Provide the details of your pivotal cloudfoundry login,

spring-tools-plugin-3

 

Add manifest.yaml for the cloudfoundry configuration

We will now add manifest.yaml in ````/deployment/cloudfoundry```

applications:
- name: spring-boot-shopping-list-mongodb
  memory: 768M
  random-route: true
  env:
    SPRING_PROFILES_ACTIVE: cloud

Now finally drag and drop the app on the cloudfounry target in Boot Dashboard

spring-tools-plugin-4

A simple microservice should deploy fine at this point, we are however using MongoDB cloud as we develpoed in PART2 of 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-vac4w.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.

cf-login

We can add all these IP addresses in the whitelist for our MongoDB cloud.

Another option is to install a service in CloudFoundry 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.

whitelist-ip

 

Still my application is in github and account is exposed, lets hide the password and pass it as a variable in cloudfoundry

spring:     
  data: 
      mongodb: 
          uri: mongodb+srv://${mongo.username}:${mongo.password}@${mongo.cluster}/shopping-list?retryWrites=true&w=majority

We can set these env variables from the cf-cli installed from here

Run below command to get list of apps,

cf 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.

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,

postman-test

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

comments powered by Disqus