Spring boot App deployment on PCF with Service binding crashes

1.5k views Asked by At

Trying to deploy a simple application to Pivotal Web services via PCF but binding the services like postgresql or mysql service results in application crashes.

Steps to build and deploying the application are as following:

mvn -DskipTests=true clean install

cf push -p target/cf-demo.jar cf102-demo

cf create-service cleardb spark cardb

cf bind-service cf102-demo cardb

cf restage cf102-demo

Application Source --> https://github.com/babarbashir/cf-demo

package com.hrsuite.cfdemo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.stream.Stream;

@SpringBootApplication
public class CfDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(CfDemoApplication.class, args);
    }
}


@RestController
class Greetoings{

    @GetMapping("/hi")
    String hi(){
        return "Hello, Cloud Foundry from Windows";
    }
}


@Component
class SampleDataCLR implements CommandLineRunner{

    private final CarRepository carRepository;

    public SampleDataCLR(CarRepository carRepository) {
        this.carRepository = carRepository;
    }

    @Override
    public void run(String... args) throws Exception {
        Stream.of("Mazda", "Toyota", "Nissan")
        .forEach(name -> carRepository.save(new Car(name)));

        carRepository.findAll().forEach(System.out::println);

    }
}



@Repository
interface CarRepository extends JpaRepository<Car, Long>{

}



@Entity
class Car{

    @Id
    @GeneratedValue
    private Long id;
    private String name;

    public Car() { //for JPA
    }

    public Car(String name) {
        this.name = name;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

The Log is as following

2017-11-22T09:24:52.733+03:00 [APP/PROC/WEB/0] [OUT] :: Spring Boot :: (v2.0.0.M6)
2017-11-22T09:24:52.997+03:00 [APP/PROC/WEB/0] [OUT] 2017-11-22 06:24:52.988 INFO 13 --- [ main] pertySourceApplicationContextInitializer : Adding 'cloud' PropertySource to ApplicationContext
2017-11-22T09:24:56.715+03:00 [APP/PROC/WEB/0] [OUT] 2017-11-22 06:24:56.408 INFO 13 --- [ main] urceCloudServiceBeanFactoryPostProcessor : Auto-reconfiguring beans of type javax.sql.DataSource
2017-11-22T09:24:56.728+03:00 [APP/PROC/WEB/0] [OUT] 2017-11-22 06:24:56.723 INFO 13 --- [ main] urceCloudServiceBeanFactoryPostProcessor : No matching service found. Skipping auto-reconfiguration.
2017-11-22T09:24:57.090+03:00 [APP/PROC/WEB/0] [OUT] 2017-11-22 06:24:57.090 INFO 13 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$8755c8ae] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-11-22T09:24:59.342+03:00 [APP/PROC/WEB/0] [OUT] 2017-11-22 06:24:59.342 WARN 13 --- [ost-startStop-1] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2017-11-22T09:24:59.411+03:00 [APP/PROC/WEB/0] [OUT] 2017-11-22 06:24:59.410 ERROR 13 --- [ost-startStop-1] o.s.b.web.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'webMetricsFilter' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsConfiguration.class]: Unsatisfied dependency expressed through method 'webMetricsFilter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcHandlerMappingIntrospector' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]: Factory method 'requestMappingHandlerMapping' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openEntityManagerInViewInterceptor' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration$JpaWebConfiguration$JpaWebMvcConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "cloud" are currently active).
2017-11-22T09:24:59.465+03:00 [APP/PROC/WEB/0] [OUT] 2017-11-22 06:24:59.465 WARN 13 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
2017-11-22T09:24:59.491+03:00 [APP/PROC/WEB/0] [OUT] 2017-11-22 06:24:59.491 INFO 13 --- [ main] utoConfigurationReportLoggingInitializer :
2017-11-22T09:24:59.491+03:00 [APP/PROC/WEB/0] [OUT] Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-11-22T09:24:59.495+03:00 [APP/PROC/WEB/0] [OUT] 2017-11-22 06:24:59.494 ERROR 13 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
2017-11-22T09:24:59.495+03:00 [APP/PROC/WEB/0] [OUT] ***************************
2017-11-22T09:24:59.495+03:00 [APP/PROC/WEB/0] [OUT] APPLICATION FAILED TO START
2017-11-22T09:24:59.495+03:00 [APP/PROC/WEB/0] [OUT] Description:
2017-11-22T09:24:59.495+03:00 [APP/PROC/WEB/0] [OUT] Cannot determine embedded database driver class for database type NONE
2017-11-22T09:24:59.495+03:00 [APP/PROC/WEB/0] [OUT] Action:
2017-11-22T09:24:59.495+03:00 [APP/PROC/WEB/0] [OUT] If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "cloud" are currently active).
2017-11-22T09:24:59.550+03:00 [APP/PROC/WEB/0] [OUT] Exit status 1
2017-11-22T09:24:59.556+03:00 [CELL/SSHD/0] [OUT] Exit status 0
2017-11-22T09:24:59.583+03:00 [API/22] [OUT] Process has crashed with type: "web"
2017-11-22T09:24:59.594+03:00 [API/22] [OUT] App instance exited with guid 7d2b8a4c-5a2c-46af-a0a7-da376db94893 payload: {"instance"=>"458f5872-9dea-4690-4485-1115", "index"=>0, "reason"=>"CRASHED", "exit_description"=>"APP/PROC/WEB: Exited with status 1", "crash_count"=>1, "crash_timestamp"=>1511331899564724498, "version"=>"1c46af47-5605-4936-9846-61bdf9f50bdb"}
2017-11-22T09:24:59.596+03:00 [CELL/0] [OUT] Stopping instance 458f5872-9dea-4690-4485-1115
2017-11-22T09:24:59.596+03:00 [CELL/0] [OUT] Destroying container
2017-11-22T09:24:59.935+03:00 [CELL/0] [OUT] Successfully destroyed container
1

There are 1 answers

0
Josh Ghiloni On

The logs you posted indicate that it cannot find an appropriate DataSource. After looking at your code, I would suggest adding the following to your POM and trying again:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-cloudfoundry-connector</artifactId>
    <version>2.0.1.RELEASE</version>
</dependency>

These connectors will inspect your VCAP_SERVICES environment variable and create ServiceInfo objects which can be used by the Spring runtime to create DataSource beans.