Frequently when developing Spring based applications, I will use the H2 in memory database during your development process. H2 ships with a web based database console, which you can use while your
application is under development. It is a convenient way to view the
tables created by Hibernate and run queries against the in memory
database. In this post I show what is required to set this up as well as what it means to then deploy your Spring Boot applications to Pivotal Cloud Foundry and rely on a database service and hence your application becomes cloud aware.
1. First ensure you have included the H2 maven dependency as shown below. I also use DevTools BUT thats not needed to enable the H2 web console
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency>2. Then create an specific application.yml file while in development mode only and enable the H2 web console. Using the default name "application.yml"will ensure while your in DEV mode it will use that file. Notice how I give the database a name rather then use the default and also specify a datasource you don't need to go to that effort BUT to me it's good practice to define a datasource because it is what you will do for an application itself when not in DEV mode.
3. Run your spring boot application
2016-09-22 21:30:26.771 INFO 18929 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-09-22 21:30:26.778 INFO 18929 --- [ restartedMain] gBootJpaBootstrapEmployeeDemoApplication : Started SpringBootJpaBootstrapEmployeeDemoApplication in 6.021 seconds (JVM running for 6.553)
When it comes to deployment in Pivotal Cloud Foundry (PCF) you most likely will not want to use H2 and instead bind to a database service like MySQL for example. To do that we would alter our project as follows.
5. Add the following maven dependancies. I add MySQL dependency and you can leave H2 as it will use that if it doesn't finda MySQL service instance to bind to. I also add "spring-boot-starter-cloud-connectors" as it's this which automatically creates and configures a DataSource which injects the service details at Runtime for me.
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cloud-connectors</artifactId> </dependency>
6. Add a specific cloud application YML file named "application-cloud.yml" as follows. I have left out a datasource and Spring Boot will create that for me when bound to the database service, BUT generally I always set the datasource with the correct properties required to meet my application requirements.
7. When creating a manifest.yml file to deploy your application to PCF all you need to do is add a MySQL database service and specify the active profile as CLOUD as shown below which will ensure we use the "application-cloud.yml" file we created above.
- name: springboot-bootstrap-employee
The project in IntelliJ is as follows
GitHub URL as follows: