1. Overview
1.概述
Deploying a Spring Boot application to Cloud Foundry is a simple exercise. In this tutorial, we’ll show you how to do it.
将 Spring Boot 应用程序部署到 Cloud Foundry 是一项简单的工作。在本教程中,我们将向您展示如何做到这一点。
2. Spring Cloud Dependencies
2.Spring Cloud的依赖性
Since this project will require new dependencies for Spring Cloud project, we’ll add the Spring Cloud Dependencies BOM:
由于这个项目将需要Spring Cloud项目的新的依赖,我们将添加Spring Cloud Dependencies BOM。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwhich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
We can find the latest version of the spring-cloud-dependencies library on Maven Central.
我们可以在Maven中心上找到最新版本的spring-cloud-dependencies库。
Now, we want to maintain a separate build for the Cloud Foundry, so we’ll create a profile named cloudfoundry in the Maven pom.xml.
现在,我们希望为Cloud Foundry保持单独的构建,因此我们将在Maven的pom.xml中创建一个名为cloudfoundry的配置文件。
We’ll also add compiler exclusions and the Spring Boot plugin to configure the name of the package:
我们还将添加编译器排除项和Spring Boot插件来配置包的名称。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/logback.xml</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.name}-cf</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/cloud/config/*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
We also want to exclude the cloud-specific files from the normal build so we add a global profile exclusion to Maven compiler plugin:
我们还想在正常构建中排除云的特定文件,因此我们在Maven编译器插件中添加了一个全局配置文件排除项。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/cloud/*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Then, we need to add the Spring Cloud Starter and the Spring Cloud Connectors libraries, which provide support for Cloud Foundry:
然后,我们需要添加 Spring Cloud Starter 和 Spring Cloud Connectors 库,它们为 Cloud Foundry 提供支持。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cloud-connectors</artifactId>
</dependency>
3. Cloud Foundry Configuration
3.Cloud Foundry配置
To go through this tutorial, we need to register for a trial here or download the pre-configured development environment for Native Linux or Virtual Box.
要通过本教程,我们需要注册试用这里或下载Native Linux或Virtual Box的预配置开发环境。
Furthermore, the Cloud Foundry CLI needs to be installed. Instructions are here.
此外,还需要安装 Cloud Foundry CLI。说明见此处。
After registration with a Cloud Foundry provider, the API URL will be made available (you can come back to it by following the Tools option on the left side).
在向 Cloud Foundry 提供商注册后,API URL 将被提供(您可以通过左侧的 Tools 选项回到该 URL)。
The application container allows us to bind services to applications. Next, let’s log in to the Cloud Foundry environment:
应用程序容器允许我们将服务与应用程序绑定。接下来,让我们登录到 Cloud Foundry 环境。
cf login -a <url>
The Cloud Foundry Marketplace is a catalog of services like databases, messaging, email, monitoring, logging and a lot more. Most services provide a free or trial plan.
Cloud Foundry Marketplace 是一个服务目录,如数据库、消息传递、电子邮件、监控、日志等。大多数服务提供免费或试用计划。
Let’s search the Marketplace for “MySQL” and create a service for our application:
让我们在市场上搜索 “MySQL “并为我们的应用程序创建一个服务。
cf marketplace | grep MySQL
>
cleardb spark, boost*, amp*, shock* Highly available MySQL for your Apps.
The output lists the services with “MySQL” in the description. On PCF the MySQL service is named cleardb and non-free plans are marked with an asterisk.
输出结果列出了描述中带有 “MySQL “的服务。在PCF上,MySQL服务被命名为cleardb,非免费计划用星号标记。
Next, we list the details of a service using:
接下来,我们列出一个使用服务的细节。
cf marketplace -s cleardb
>
service plan description free or paid
spark Great for getting started and developing your apps free
boost Best for light production or staging your applications paid
amp For apps with moderate data requirements paid
shock Designed for apps where you need real MySQL reliability, power and throughput paid
Now we create a free MySQL service instance named spring-bootstrap-db:
现在我们创建一个名为spring-bootstrap-db的免费MySQL服务实例。
cf create-service cleardb spark spring-bootstrap-db
4. Application Configuration
4.应用配置
Next, we add a @Configuration annotated class that extends AbstractCloudConfig to create a DataSource in the package named org.baeldung.cloud.config:
接下来,我们添加一个@Configuration注解类,它扩展了AbstractCloudConfig,以在名为org.baeldung.cloud.config的包中创建一个DataSource。
@Configuration
@Profile("cloud")
public class CloudDataSourceConfig extends AbstractCloudConfig {
@Bean
public DataSource dataSource() {
return connectionFactory().dataSource();
}
}
Adding @Profile(“cloud”) ensures that the Cloud Connector isn’t active when we do local testing. We also add @ActiveProfiles(profiles = {“local”}) to the Integration tests.
添加@Profile(“cloud”)可以确保在我们进行本地测试时,云连接器没有被激活。我们还在集成测试中添加@ActiveProfiles(profiles = {“local”})。
Then build the application with:
然后用以下方法构建应用程序。
mvn clean install spring-boot:repackage -P cloudfoundry
Also, we need to provide a manifest.yml file, to bind the service to the application.
另外,我们需要提供一个manifest.yml文件,以将服务与应用程序绑定。
We usually place the manifest.yml file in the project folder but in this case, we’ll create a cloudfoundry folder since we’re going to demonstrate deploying to multiple cloud-native providers:
我们通常将 manifest.yml 文件放在项目文件夹中,但在本例中,我们将创建一个 cloudfoundry 文件夹,因为我们要演示部署到多个云原生提供商。
---
applications:
- name: spring-boot-bootstrap
memory: 768M
random-route: true
path: ../target/spring-boot-bootstrap-cf.jar
env:
SPRING_PROFILES_ACTIVE: cloud,mysql
services:
- spring-bootstrap-db
5. Deployment
5.部署
Deploying the application is now as easy as:
部署应用程序现在变得非常简单。
cd cloudfoundry
cf push
Cloud Foundry will use the Java buildpack to deploy the application and create a random route to the application.
Cloud Foundry 将使用 Java buildpack 来部署应用程序,并为该应用程序创建一个随机路由。
We can view the last few entries in the log file using:
我们可以用以下方法查看日志文件中的最后几个条目。
cf logs spring-boot-bootstrap --recent
Or we can tail the log file:
或者我们可以跟踪日志文件。
cf logs spring-boot-bootstrap
Finally, we need the route name to test the application:
最后,我们需要路由名称来测试应用程序。
cf app spring-boot-bootstrap
>
name: spring-boot-bootstrap
requested state: started
routes: spring-boot-bootstrap-delightful-chimpanzee.cfapps.io
last uploaded: Thu 23 Aug 08:57:20 SAST 2018
stack: cflinuxfs2
buildpacks: java-buildpack=v4.15-offline-...
type: web
instances: 1/1
memory usage: 768M
state since cpu memory disk
#0 running 2018-08-23T06:57:57Z 0.5% 290.9M of 768M 164.7M of 1G
Executing the following command will add a new book:
执行以下命令将添加一个新书。
curl -i --request POST \
--header "Content-Type: application/json" \
--data '{"title": "The Player of Games", "author": "Iain M. Banks"}' \
https://<app-route>/api/books
#OR
http POST https://<app-route>/api/books title="The Player of Games" author="Iain M. Banks"
And this command will list all books:
而这个命令将列出所有书籍。
curl -i https://<app-route>/api/books
#OR
http https://<app-route>/api/books
>
HTTP/1.1 200 OK
[
{
"author": "Iain M. Banks",
"id": 1,
"title": "Player of Games"
},
{
"author": "J.R.R. Tolkien",
"id": 2,
"title": "The Hobbit"
}
]
6. Scaling the Application
6.扩展应用程序
Lastly, scaling an application on Cloud Foundry is as simple as using the scale command:
最后,在 Cloud Foundry 上扩展应用程序就像使用 scale 命令一样简单。
cf scale spring-cloud-bootstrap-cloudfoundry <options>
Options:
-i <instances>
-m <memory-allocated> # Like 512M or 1G
-k <disk-space-allocated> # Like 1G or 2G
-f # Force restart without prompt
Remember to delete the application when we don’t need it anymore:
当我们不再需要它时,记得删除该应用程序。
cf delete spring-cloud-bootstrap-cloudfoundry
7. Conclusion
7.结论
In this article, we covered the Spring Cloud libraries that simplify the development of a cloud-native application using Spring Boot. Deployment with the Cloud Foundry CLI is well documented here.
在本文中,我们介绍了 Spring Cloud 库,该库可简化使用 Spring Boot 开发云原生应用程序的过程。使用 Cloud Foundry CLI 进行部署的文档这里。
Extra plugins for the CLI are available in the plugin repository.
CLI的额外插件可在插件库中找到。
The full source code of our examples here is, as always, over on GitHub.
我们这里的例子的完整源代码一如既往地在GitHub上。