如何将 Spring Boot 部署到 Cloud Foundry?

Posted

技术标签:

【中文标题】如何将 Spring Boot 部署到 Cloud Foundry?【英文标题】:How to deploy Spring Boot to Cloud Foundry? 【发布时间】:2015-05-06 02:42:20 【问题描述】:

我尝试使用以下pom.xml 将我的 Spring Boot 应用程序部署到 CF

       <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-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cloud-connectors</artifactId>
        </dependency>
    </dependencies>

更新

我的申请

@SpringBootApplication
public class EdollarApplication 

    @Autowired
    private DataSource dataSource;

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

DataSourceConfiguration.java

@Configuration
@Profile("cloud")
public class DataSourceConfiguration 

  @Bean
  public Cloud cloud() 
    return new CloudFactory().getCloud();
  

  @Bean
  @ConfigurationProperties(DataSourceProperties.PREFIX)
  public DataSource dataSource() 
    return cloud().getSingletonServiceConnector(DataSource.class, null);
  


日志中没有错误,但是我无法访问我在应用程序中定义的 URL。示例日志如下所示

Updated app with guid 8b167ac9-11bb-483e-be0a-3dd8c8991a84 ("state"=>"STARTED")
-----> Downloaded app package (20K)

-----> Downloading Open Jdk JRE 1.8.0_45 from https://download.run.pivotal.io/openjdk/trusty/x86_64/openjdk-1.8.0_45.tar.gz (2.5s)
       Expanding Open Jdk JRE to .java-buildpack/open_jdk_jre (1.3s)
-----> Downloading Tomcat Instance 8.0.21 from https://download.run.pivotal.io/tomcat/tomcat-8.0.21.tar.gz (0.5s)
       Expanding Tomcat to .java-buildpack/tomcat (0.1s)
-----> Downloading Tomcat Lifecycle Support 2.4.0_RELEASE from https://download.run.pivotal.io/tomcat-lifecycle-support/tomcat-lifecycle-support-2.4.0_RELEASE.jar (0.0s)
-----> Downloading Tomcat Logging Support 2.4.0_RELEASE from https://download.run.pivotal.io/tomcat-logging-support/tomcat-logging-support-2.4.0_RELEASE.jar (0.0s)
-----> Downloading Tomcat Access Logging Support 2.4.0_RELEASE from https://download.run.pivotal.io/tomcat-access-logging-support/tomcat-access-logging-support-2.4.0_RELEASE.jar (0.0s)
-----> Uploading droplet (51M)
[CONTAINER] org.apache.catalina.core.StandardService           INFO    Starting service Catalina
[CONTAINER] org.apache.catalina.startup.Catalina               INFO    Initialization processed in 514 ms
[CONTAINER] org.apache.coyote.http11.Http11NioProtocol         INFO    Initializing ProtocolHandler ["http-nio-61187"]
[CONTAINER] org.apache.catalina.core.StandardEngine            INFO    Starting Servlet Engine: Apache Tomcat/8.0.21
[CONTAINER] org.apache.catalina.startup.HostConfig             INFO    Deploying web application directory /home/vcap/app/.java-buildpack/tomcat/webapps/ROOT
[CONTAINER] org.apache.jasper.servlet.TldScanner               INFO    At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[CONTAINER] org.apache.coyote.http11.Http11NioProtocol         INFO    Starting ProtocolHandler ["http-nio-61187"]
[CONTAINER] org.apache.tomcat.util.net.NioselectorPool         INFO    Using a shared selector for servlet write/read
[CONTAINER] org.apache.catalina.startup.Catalina               INFO    Server startup in 507 ms

我在这里错过了什么?

【问题讨论】:

可能值得确认...EdollarApplication 在名为demo 的根包中吗?它是否在命令行上运行?即java -jar &lt;app&gt;-&lt;version&gt;.jar? 可能不是问题,但您的主课程中不需要@EnableAutoConfiguration Spring Boot 是否有开箱即用的示例或 Cloud Foundry 中的最低配置? 它已经开箱即用。虽然我现在想知道 - 鉴于您没有在上面显示,您的 manifest.yml 是什么样的? 我没有 manifest.yml。我需要一个吗? 【参考方案1】:

这是一个配置为在 Cloud Foundry 中运行的最小 Spring Boot 项目的示例:https://github.com/gratiartis/super-mini

我通过将以下 manifest.yml 放入项目的根目录中使其在 Cloud Foundry 中运行。

---
applications:
- name: super-mini
  # Fork of https://github.com/cloudfoundry/java-buildpack
  # For stability, keep your own fork.
  buildpack: https://github.com/gratiartis/java-buildpack
  memory: 512M
  instances: 1
  # Because the URL will be http://super-mini.cfapps.io/
  host: super-mini
  domain: cfapps.io
  path: target/super-mini-1.0.0-SNAPSHOT.jar

请注意,可以添加以下内容来激活配置文件(即“云”):

  env:
    SPRING_PROFILES_ACTIVE: cloud

path 基于我从项目的根目录运行部署。我相信你已经猜到了,它是一个 Maven 构建,所以在 target 目录中创建了一个 .jar。

要部署,cd 到项目的根目录并运行:

cf push

以上将创建一个运行在http://super-mini.cfapps.io/hello的应用程序

看看 - 它现在正在运行。当然,我现在已经创建了那个应用程序名称,所以你不能使用它!

【讨论】:

我应该像你一样使用 mysql 作为配置文件名称吗?或者我需要将其更改为Cloud,如上面的DataSourceConfiguration 我完全按照您的建议进行了操作。该应用程序已推送到云端,但我无法访问任何网址。事实上,在日志中我没有看到任何请求映射已被映射。 哦不...我只是在应用程序资源文件夹中有一个application.yml,其配置文件名为mysql,它定义了连接详细信息。这是一种让它工作的简单方法,但 Spring Cloud 的东西可能会更好。 它是否在日志中显示应用程序启动?他们应该提供一些关于应用程序是否已部署、它被称为什么、启动过程中是否有任何问题等信息。 另外,您的应用程序日志记录配置到哪里?

以上是关于如何将 Spring Boot 部署到 Cloud Foundry?的主要内容,如果未能解决你的问题,请参考以下文章

Intellij IDEA 部署 Spring Boot / Spring Cloud 应用到阿里云

如何在谷歌云中部署 Spring Boot 应用程序?

Google Cloud Spring Boot InvocationTargetException

Spring Boot 应用在部署到 Google App Engine 时无法连接到 Google Cloud SQL (PostgreSQL)

将 Spring Boot 应用程序连接到 Google Cloud 中的 MySQL 数据库

Spring Cloud 注册中心在tomcat中部署