【问题标题】:Spring boot App deployment on PCF with Service binding crashes带有服务绑定崩溃的 PCF 上的 Spring Boot App 部署
【发布时间】:2017-11-22 22:35:41
【问题描述】:

尝试通过 PCF 将简单的应用程序部署到 Pivotal Web 服务,但绑定 postgresql 或 mysql 服务等服务会导致应用程序崩溃。

构建和部署应用程序的步骤如下:

mvn -DskipTests=true 全新安装

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

cf create-service cleardb spark cardb

cf 绑定服务 cf102-demo cardb

cf restage cf102-demo

应用源 --> 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 + '\'' +
                '}';
    }
}

日志如下

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

【问题讨论】:

  • cf logs cf102-demo —recent 的输出是什么?
  • 感谢 Josh,添加日志供您参考。

标签: java spring postgresql maven cloud-foundry


【解决方案1】:

您发布的日志表明它找不到合适的数据源。查看您的代码后,我建议将以下内容添加到您的 POM 并重试:

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

这些连接器将检查您的 VCAP_SERVICES 环境变量并创建 ServiceInfo 对象,Spring 运行时可以使用这些对象来创建 DataSource bean。

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2021-12-23
    • 2016-11-26
    • 2018-06-17
    相关资源
    最近更新 更多