

Table of Contents
- 1. Overview
- 2 Cloud Foundry CLI set up and Pivotal cloud foundry trail account creation
- 3.MySQL database service creation in the pivotal cloud:
- 4.Project Structure
- 5. Creating and using manifest.yml file.
- 6.Deploying spring boot application in cloud foundry
- 7. How spring-boot is bind to MySQL service and configured with our zero lines of code?
- 8.Conclusion
- 9.Source Code
- 10.References
1. Overview
In this article, we are going to learn a little about cloud foundry and we will also provide an example to deploy spring boot application in cloud foundry. We will see how spring boot will do auto-configuration for the MySQL database and the installation of the required tools for cloud foundry deployments.
Cloud Foundry is an open-source platform as service (Paas) which is backed by Google, IBM, Microsoft, pivotal, SAP, and other companies. Cloud Foundry makes it faster and easier to build, test, deploy and scale applications and it is providing the choice of clouds, developer frameworks, and application services.
In the cloud foundry account, Organizations can be created. Each Organization may again have spaces like dev, staging, production. Each space has services like database and application services. In cloud foundry, the marketplace is available for provisioning new services.
Prerequisites to deploy any spring boot application in cloud foundry are,
1.CF command-line interface (CLI) in your system
2.Cloud foundry account (Example: Pivotal or SAP cloud foundry…)
In this article, we will use the Pivotal cloud foundry trail account to deploy and test the Spring boot application with MySQL database connection.
2 Cloud Foundry CLI set up and Pivotal cloud foundry trail account creation
2.1 Cloud Foundry CLI installation steps
1. Download the CF CLI zip file.
Link for download: https://pivotal.io/platform/pcf-tutorials/getting-started-with-pivotal-cloud-foundry/install-the-cf-cli
2. Unzip the downloaded file and click on the cf_installer file and it will guide our next steps to finish the installation.
3. To test, CF CLI installed or not. just open the command prompt or terminal and run cf help
command.it will list down all cloud foundry related commands.
2.2 Pivotal cloud account creation steps
1. Go to the pivotal cloud foundry signup page.
2. After completing a successful pivotal account creation, just sign in, it will show different services.
3. Enter the organization name and finish. By default, it will create one space with name development.
3.MySQL database service creation in the pivotal cloud:
Cloud Foundry is providing different types of services and we can buy any service in the marketplace.
Below are steps to create a MySQL database service free plan
1. In in your Organization, go to the marketplace and search for MySQL
2. select Clear DB MySQL Database
3. choose the spark DB plan which is free.it looks as below image
4.Project Structure
We have created a sample spring boot application to deploy in cloud foundry. Have look at the following image
application.properties file:
These properties are configured to test rest API in the local system, not for a cloud system.
#Data source spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root #Hibernate properties spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.properties.hibernate.formate_sql=true
pom.xml:
The important dependencies in pom.xml
are,
spring-boot-starter-web: Responsible for web application creation
spring-boot-starter-data-jpa: for database configuration
mysql-connector-java: for MySQL connection
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.javadevloperzone</groupId> <artifactId>SpringBootMysqlCF</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootMysqlCF</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <finalName>SpringBootMysqlApp</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
User.java
package com.javadevloperzone.services; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; private String name; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
UserRepository.java
package com.javadevloperzone.services; import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepository<User, Integer> { }
UserController.java
package com.javadevloperzone.services; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/users") public List<User> getAllUsers() { return (List<User>) userRepository.findAll(); } @PostMapping("/users") public User createUser(@RequestBody User user) { return userRepository.save(user); } }
5. Creating and using manifest.yml file.
Cloud Foundry uses manifest.yml
file to identify application properties like memory limit, application name, service bindings, and routes and more
Create manifest.yml
file in the root directory of your app.
applications: - name: SpringBootMysqlApp memory: 1GB path: target/SpringBootMysqlApp.jar buildpacks: - java_buildpack services: - MySqlDB
6.Deploying spring boot application in cloud foundry
1.open command prompt/terminal and change directory to project root directory,
Example: D:\Pivotel\SpringBootMysqlCF
Command: cd D:\Pivotel\SpringBootMysqlCF
2. Every cloud foundry will have an API endpoint. for pivotal cloud foundry
API endpoint: https://api.run.pivotal.io
Connect to API endpoint by entering following command
cf login -a https://api.run.pivotal.io
3. By providing credentials, it will be logged into your pivotal cloud foundry
4. Enter command cf push
. it will read the manifest file and pick jar file from the path and deploy it to cloud foundry. By using the build pack, it will download the required dependencies for running an application.
5. Login into cloud foundry account and open development space and copy route URL.
By route URL, we can test our rest API
Example:
Route URL: https://springbootmysqlapp.cfapps.io
REST API endpoint: /users
Final URL: https://springbootmysqlapp.cfapps.io/users
we did not write code for MySQL database configuration. But still, it is working. In the next phase, we are going to discuss how spring boot did auto reconfiguration for the MySQL database.
7. How spring-boot is bind to MySQL service and configured with our zero lines of code?
Properties in application.properties are responsible for connecting MySQL local database, not cloud MySQL database.
Cloud foundry will do auto-reconfiguration for the MySQL database. Only when following are true for your application
1. Only one service instance of a given service type is bound to the application.
Example: if we bind MySQL and PostgreSQL to the application, then Auto-Reconfiguration does not work due to two databases are bind to application
2. Only one matching type bean is in the Spring application context. For example, you can have only one bean type of javax.sql.Datasource
If you are eager to know about manual configuration, how to connect to different databases and how pivotal cloud foundry will do auto-reconfiguration. just go through following URL
Pivotal cloud foundry doc.
8.Conclusion
We have explained the deployment process of spring boot with MySQL database in cloud foundry clearly with images.
Hope 😊 you like it. if you want more information you can through the following links,
About Buildpacks: https://docs.cloudfoundry.org/buildpacks/
manifest.yml file: https://docs.cloudfoundry.org/devguide/deploy-apps/manifest.html
Auto reconfiguration: https://docs.run.pivotal.io/buildpacks/java/configuring-service-connections/spring-service-bindings.html
Cloud Foundry CLI: https://docs.cloudfoundry.org/cf-cli/
9.Source Code
A complete example code is available on Spring Boot Examples Git repository.
10.References
https://docs.run.pivotal.io/buildpacks/java/getting-started-deploying-apps/gsg-spring.html
2 comments. Leave new
very nicely explained.
Thank you, Amit.