Search This Blog

Thursday, 22 September 2016

Using H2 Console in development with Spring Boot then NOT when deployed to Pivotal Cloud Foundry

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.

application.yml

server:
  error:
    whitelabel:
      enabled: false

spring:
  h2:
    console:
      enabled: true
  jpa:
    hibernate:
      ddl-auto: create
  datasource:
    url: jdbc:h2:mem:apples;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    username: sa
    password:
    driver-class-name: org.h2.Driver
    platform: h2

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)

4. Connect to the H2 we console as follows

http://localhost:8080/h2-console/

The JDBC Url now becomes what you set in the dialog above, inshort the DB name I set was "apples"



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.

application-cloud.yml

spring:
  jpa:
    hibernate:
      ddl-auto: create

server:
  error:
    whitelabel:
      enabled: false

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.

manifest.yml

---
applications:
- name: springboot-bootstrap-employee
  memory: 512M
  instances: 1
  random-route: true
  timeout: 180
  path: ./target/springbootjpabootstrapemployeedemo-0.0.1-SNAPSHOT.jar
  services:
    - pas-mysql
  env:
    JAVA_OPTS: -Djava.security.egd=file:///dev/urando
    SPRING_PROFILES_ACTIVE: cloud

The project in IntelliJ is as follows


GitHub URL as follows:

https://github.com/papicella/SpringBootJPABootstrapEmployeeDemo

No comments: