Spring Cloud Connectors and Heroku – Spring Cloud Connectors and Heroku

最后修改: 2017年 12月 10日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we’re going to cover setting up a Spring Boot application on Heroku using Spring Cloud Connectors.

在这篇文章中,我们将介绍使用Spring Cloud Connectors在Heroku上设置一个Spring Boot应用程序。

Heroku is a service that provides hosting for web services. Also, they provide a large selection of third-party services, called add-ons, that provide everything from system monitoring to database storage.

Heroku是一个为网络服务提供托管的服务。同时,他们提供大量的第三方服务,称为附加组件,提供从系统监控到数据库存储的所有服务。

In addition to all of this, they have a custom CI/CD pipeline that integrates seamlessly into Git that expedites development into production.

除此以外,他们还有一个定制的CI/CD管道,与Git无缝集成,加快了开发到生产的速度。

Spring supports Heroku through it’s Spring Cloud Connectors library. We’ll be using this to configure a PostgreSQL data source in our application automatically.

Spring通过其Spring Cloud Connectors库支持Heroku。我们将使用它在我们的应用程序中自动配置一个PostgreSQL数据源。

Let’s jump into writing the app.

让我们跳入编写应用程序。

2. Spring Boot Book Service

2.Spring Boot图书服务

First, let’s create a new simple Spring Boot service.

首先,让我们创建一个新的简单Spring Boot服务

3. Heroku Sign Up

3.Heroku注册

Now, we need to sign up for a Heroku account. Let’s go to heroku.com and click on the sign-up button in the top right corner of the page.

现在,我们需要注册一个Heroku账户。让我们进入heroku.com,点击页面右上角的注册按钮。

Now that we’ve got an account we need to get the CLI tool. We need to navigate to the heroku-cli installation page and install this software. This will give us the tools we need to complete this tutorial.

现在我们已经有了一个账户,我们需要获得CLI工具。我们需要导航到heroku-cli安装页面并安装这个软件。这将为我们提供完成本教程所需的工具。

4. Create Heroku Application

4.创建Heroku应用程序

Now that we have the Heroku CLI let’s go back to our app.

现在我们有了Heroku CLI,让我们回到我们的应用程序。

4.1. Initialize Git Repository

4.1.初始化 Git 仓库

Heroku works best when using git as our source control.

当使用git作为我们的源代码控制时,Heroku工作得最好。

Let’s begin by going to the root of our application, the same directory as our pom.xml file, and running the command git init to create a git repository. Then run git add . and git commit -m “first commit”.

让我们首先进入我们的应用程序的根目录,与我们的pom.xml文件相同,并运行命令git init来创建一个git仓库。然后运行git add .git commit -m “first commit”

Now we’ve got our application saved to our local git repository.

现在我们已经把我们的应用程序保存在本地的git仓库里。

4.2. Provision Heroku Web App

4.2. 提供Heroku网络应用程序

Next, let’s use the Heroku CLI to provision a web server on our account.

接下来,让我们使用Heroku CLI在我们的账户上配置一个网络服务器。

First, we need to authenticate our Heroku account. From the command line run heroku login and follow the instructions for logging in and creating an SSH key.

首先,我们需要验证我们的Heroku账户。在命令行中运行heroku login,并按照说明登录和创建SSH密钥。

Next, run heroku create. This will provision the web server and add a remote repository that we can push our code to for deployments. We’ll also see a domain printed in the console, copy this domain so that we can access it later.

接下来,运行heroku create。这将提供网络服务器,并添加一个远程仓库,我们可以将我们的代码推送到该仓库进行部署。我们还将看到控制台中打印出一个域,复制这个域,以便我们以后可以访问它。

4.3. Push Code to Heroku

4.3.推送代码到Heroku

Now we’ll use git to push our code to the new Heroku repository.

现在我们将使用git将我们的代码推送到新的Heroku仓库。

Run the command git push heroku master to send our code to Heroku.

运行git push heroku master命令,将我们的代码发送到Heroku。

In the console output we should see logs indicating the upload was successful then their system will, download any dependencies, build our application, run tests (if present), and deploy the app if everything goes smoothly.

在控制台输出中,我们应该看到日志显示上传成功,然后他们的系统将下载任何依赖性,构建我们的应用程序,运行测试(如果存在),如果一切顺利,则部署应用程序。

That is it -we now have our application publicly deployed to a web server.

就是这样–我们现在有了公开部署到网络服务器的应用程序。

5. Test In-Memory on Heroku

5.在Heroku上进行内存测试

Let’s make sure our app is working. Using the domain from our create step, let’s test our live application.

让我们确保我们的应用程序正在工作。使用创建步骤中的域,让我们测试一下我们的实时应用程序。

Let’s issue some HTTP requests:

让我们发出一些HTTP请求。

POST https://{heroku-domain}/books HTTP
{"author":"baeldung","title":"Spring Boot on Heroku"}

We should get back:

我们应该回去了。

{
    "title": "Spring Boot on Heroku",
    "author": "baeldung"
}

Now let’s try to read the object we just created:

现在让我们试着读取我们刚刚创建的对象。

GET https://{heroku-domain}/books/1 HTTP

This should return:

这应该返回。

{
    "id": 1,
    "title": "Spring Boot on Heroku",
    "author": "baeldung"
}

That all looks good, but in production, we should be using a permanent data store.

这一切看起来很好,但在生产中,我们应该使用一个永久的数据存储。

Let’s walk through provisioning a PostgreSQL database and configuring our Spring app to use it.

让我们通过配置PostgreSQL数据库和配置我们的Spring应用来使用它。

6. Adding PostgreSQL

6.添加PostgreSQL

To add the PostgreSQL database, run this command heroku addons:create heroku-postgresql:hobby-dev

要添加PostgreSQL数据库,运行这个命令heroku addons:create heroku-postgresql:hobby-dev

This will provision a database for our web server and add an environment variable that provides the connection information.

这将为我们的网络服务器提供一个数据库,并添加一个环境变量来提供连接信息。

Spring Cloud Connector is configured to detect this variable and set up the data source automatically given that Spring can detect that we want to use PostgreSQL.

Spring Cloud Connector被配置为检测这个变量并自动设置数据源,因为Spring可以检测到我们要使用PostgreSQL。

To let Spring Boot know that we’re using PostgreSQL, we need to make two changes.

为了让Spring Boot知道我们在使用PostgreSQL,我们需要做两个改动。

First, we need to add a dependency to include the PostgreSQL drivers:

首先,我们需要添加一个依赖关系,以包括PostgreSQL驱动。

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.10</version>
</dependency>

Next, let’s add properties so that Spring Data Connectors can configure the database according to its available resources.

接下来,让我们添加属性,以便Spring Data Connectors能够根据其可用资源来配置数据库。

In src/main/resources create an application.properties file and add the following properties:

src/main/resources中创建一个application.properties文件并添加以下属性。

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.maxActive=10
spring.datasource.maxIdle=5
spring.datasource.minIdle=2
spring.datasource.initialSize=5
spring.datasource.removeAbandoned=true
spring.jpa.hibernate.ddl-auto=create

This will pool our database connections and limit our application’s connections. Heroku limits the number of active connections in a development tier database to 10 and so we set our max to 10. Additionally, we set the hibernate.ddl property to create so that our book table will be created automatically.

这将汇集我们的数据库连接并限制我们应用程序的连接。Heroku限制开发层数据库的活动连接数为10,因此我们将最大连接数设为10。此外,我们将hibernate.ddl属性设置为create,这样我们的书表将被自动创建。

Finally, commit these changes and run git push heroku master. This will push these changes up to our Heroku app. After our app starts, try running tests from the previous section.

最后,提交这些修改,并运行git push heroku master。这将把这些变化推送到我们的Heroku应用程序。在我们的应用程序启动后,尝试运行前一部分的测试。

The last thing we need to do is change the ddl setting. Let’s update that value as well:

我们需要做的最后一件事是改变ddl的设置。让我们也来更新这个值。

spring.jpa.hibernate.ddl-auto=update

This will instruct the application to update the schema when changes are made to the entity when the app is restarted. Commit and push this change like before to have the changes pushed to our Heroku app.

这将指示应用程序在应用程序重启时对实体进行更改时更新模式。像以前一样提交并推送这一变化,以便将变化推送到我们的Heroku应用程序。

We didn’t need to write a custom data source integration for any of this. That’s because Spring Cloud Connectors detects that we’re running with Heroku and using PostgreSQL – and automatically wires up the Heroku data source.

我们不需要为这一切写一个自定义的数据源集成。这是因为Spring Cloud Connectors检测到我们正在使用Heroku并使用PostgreSQL,并自动连接到Heroku的数据源。

5. Conclusion

5.结论

We now have a running Spring Boot app in Heroku.

现在我们有一个在Heroku中运行的Spring Boot应用。

Most of all, the simplicity of going from a single idea to a running application makes Heroku a solid way to deploy.

最重要的是,从一个想法到一个正在运行的应用程序的简单性使Heroku成为一种可靠的部署方式。

To find out more about Heroku and all the tools, it offers we can read more on heroku.com.

要了解更多关于Heroku及其提供的所有工具,我们可以在heroku.com上阅读更多信息。

As always, code snippets can be found over on GitHub.

像往常一样,代码片段可以在GitHub上找到over