在 Pivotal Web Services (PWS) 中部署 Grails 3 应用
了解如何在 Pivotal Web Services (PWS) 中部署 Grails 3 应用,该服务由 Cloud Foundry Platform as a Service (PaaS) 提供支持。
作者:Sergio del Amo
Grails 版本 3.3.1
1 Grails 培训
Grails 培训 - 由创建和积极维护 Grails 框架的人员开发和提供!。
2 开始
本指南将介绍如何将 Grails 3 应用部署到 Pivotal Web Services (PWS)。首先我们使用 cf CLI 部署该应用。稍后使用 Gradle 任务部署。
2.1 你需要具备什么
完成本指南需要具备以下条件
-
一些业余时间
-
一个不错的文本编辑器或 IDE
-
已安装 JDK 1.8 或更高版本且适当配置了
JAVA_HOME
如果你想从头开始,创建一个新的 Grails 3 应用
grails create-app grailsguidepws
2.2 如何完成该指南
按照以下步骤开始
-
下载并解压源代码
或
-
克隆 Git 代码库
git clone https://github.com/grails-guides/grails-deploy-pws.git
Grails 指南代码库包含两个文件夹
-
initial
初始项目。通常是一个简单的 Grails 应用,包含一些其他代码,可帮助你快速入门。 -
complete
一个完成的示例。它是在完成指南介绍的步骤并将这些变更应用于initial
文件夹后的结果。
完成指南,请转至 initial
文件夹
-
cd
进入grails-guides/grails-deploy-pws/initial
并按照下一部分中的说明进行操作。
如果你为cd 输入 grails-guides/grails-deploy-pws/complete ,你可以直接转到完成的示例 |
3 设置 PWS 帐户和安装 cf CLI
按照说明 设置你的 PWS 帐户和下载 cf CLI。你可以为你的组织创建免费试用。
4 使用 cf CLI 在 PWS 上部署
安装cf CLI 后,你将能够从终端中登录。
$ cf login
API endpoint: https://api.run.pivotal.io
Email> [email protected]
Password>
Authenticating...
OK
Select an org (or press enter to skip):
1. guides-grails-org
Org> 1
Targeted org guides-grails-org
Targeted space development
API endpoint: https://api.run.pivotal.io (API version: 2.69.0)
User: [email protected]
Org: guides-grails-org
Space: development
4.1 创建 MySQL 服务
你可以使用以下命令查看可用服务列表
$ cf marketplace
我们为 Grails 应用程序创建一个 MySQL 数据库即服务。
cleardb 是 MySQL 支持的应用程序的数据库即服务 |
$ cf create-service cleardb spark grailsguidesmysql
Creating service instance grailsguidesmysql in org guides-grails-org / space development as [email protected]...
OK
在生产环境中,我们将使用我们刚刚创建的 MySQL 服务。
我们来创建一个生产环境配置文件
def uri = new URI(System.env.DATABASE_URL)
dataSources {
dataSource {
driverClassName = "com.mysql.jdbc.Driver"
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
username = uri.userInfo ? uri.userInfo.split(":")[0] : ""
password = uri.userInfo ? uri.userInfo.split(":")[1] : ""
url = "jdbc:mysql://" + uri.host + uri.path
properties {
jmxEnabled = true
initialSize = 5
maxActive = 50
minIdle = 5
maxIdle = 25
maxWait = 10000
maxAge = 600000
timeBetweenEvictionRunsMillis = 5000
minEvictableIdleTimeMillis = 60000
validationQuery = 'SELECT 1'
validationQueryTimeout = 3
validationInterval = 15000
testOnBorrow = true
testWhileIdle = true
testOnReturn = false
jdbcInterceptors = 'ConnectionState'
defaultTransactionIsolation = 2 //# TRANSACTION_READ_COMMITTED
}
}
}
从 application.yml
中删除环境生产数据源块。我们在 application-production.groovy
中定义生产数据源配置
也就是说
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
将 MySQL 添加为 Gradle 依赖项块的运行时依赖项。
compile "org.grails.plugins:scaffolding"
4.2 创建 CF 清单文件
我们创建一个配置文件,用于将应用程序推送到 PWS。配置文件引用我们在上一部分中创建的 MySQL 服务。
---
applications:
- name: grailsguidepws
memory: 1G
instances: 1
path: build/libs/complete-0.1.war
buildpack: java_buildpack
services:
- grailsguidesmysql
你的 war 名称可能不同,请确保配置正确的路径。另外,请记住使用不同的主机名,如果你使用指南中使用的相同主机名,你将收到错误消息,告诉你主机名已经被使用。 |
4.3 部署应用程序
生成一个 war 文件
./grailsw war
运行 cf push
。如果一切顺利,你应该看到类似于以下的输出
$ cf push
Using manifest file /Users/grails/git/grailsguidepws/manifest.yml
Creating app grailsguidepws in org guides-grails-org / space development as [email protected]...
OK
Creating route grailsguidepws.cfapps.io...
OK
Binding grailsguidepws.cfapps.io to grailsguidepws...
OK
Uploading grailsguidepws...
Uploading app files from: /var/folders/j3/_bh_nnt96g3737jvnfcvnwcc0000gn/T/unzipped-app735681719
Uploading 1.8M, 254 files
Done uploading
OK
Binding service grailsguidesmysql to app grailsguidepws in org guides-grails-org / space development as [email protected]...
OK
Starting app grailsguidepws in org guides-grails-org / space development as [email protected]...
Downloading java_buildpack...
Downloaded java_buildpack
Creating container
Successfully created container
Downloading app package...
Downloaded app package (62.1M)
Staging...
-----> Java Buildpack Version: v3.12 (offline) | https://github.com/cloudfoundry/java-buildpack.git#6f25b7e
-----> Downloading Open Jdk JRE 1.8.0_121 from https://java-buildpack.cloudfoundry.org/openjdk/trusty/x86_64/openjdk-1.8.0_121.tar.gz (found in cache)
Expanding Open Jdk JRE to .java-buildpack/open_jdk_jre (1.1s)
-----> Downloading Open JDK Like Memory Calculator 2.0.2_RELEASE from https://java-buildpack.cloudfoundry.org/memory-calculator/trusty/x86_64/memory-calculator-2.0.2_RELEASE.tar.gz (found in cache)
Memory Settings: -Xss349K -Xmx681574K -XX:MaxMetaspaceSize=104857K -Xms681574K -XX:MetaspaceSize=104857K
-----> Downloading Container Certificate Trust Store 1.0.0_RELEASE from https://java-buildpack.cloudfoundry.org/container-certificate-trust-store/container-certificate-trust-store-1.0.0_RELEASE.jar (found in cache)
Adding certificates to .java-buildpack/container_certificate_trust_store/truststore.jks (1.2s)
-----> Downloading Container Customizer 1.1.0_RELEASE from https://java-buildpack.cloudfoundry.org/container-customizer/container-customizer-1.1.0_RELEASE.jar (found in cache)
-----> Downloading Spring Auto Reconfiguration 1.10.0_RELEASE from https://java-buildpack.cloudfoundry.org/auto-reconfiguration/auto-reconfiguration-1.10.0_RELEASE.jar (found in cache)
Exit status 0
Uploading droplet, build artifacts cache...
Staging complete
Uploading build artifacts cache...
Uploading droplet...
Uploaded build artifacts cache (109B)
Uploaded droplet (107.3M)
Uploading complete
Destroying container
Successfully destroyed container
0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
1 of 1 instances running
App started
OK
App grailsguidepws was started using this command `CALCULATED_MEMORY=$($PWD/.java-buildpack/open_jdk_jre/bin/java-buildpack-memory-calculator-2.0.2_RELEASE -memorySizes=metaspace:64m..,stack:228k.. -memoryWeights=heap:65,metaspace:10,native:15,stack:10 -memoryInitials=heap:100%,metaspace:100% -stackThreads=300 -totMemory=$MEMORY_LIMIT) && JAVA_OPTS="-Djava.io.tmpdir=$TMPDIR -XX:OnOutOfMemoryError=$PWD/.java-buildpack/open_jdk_jre/bin/killjava.sh $CALCULATED_MEMORY -Djavax.net.ssl.trustStore=$PWD/.java-buildpack/container_certificate_trust_store/truststore.jks -Djavax.net.ssl.trustStorePassword=java-buildpack-trust-store-password" && SERVER_PORT=$PORT eval exec $PWD/.java-buildpack/open_jdk_jre/bin/java $JAVA_OPTS -cp $PWD/. org.springframework.boot.loader.WarLauncher`
Showing health and status for app grailsguidepws in org guides-grails-org / space development as [email protected]...
OK
requested state: started
instances: 1/1
usage: 1G x 1 instances
urls: grailsguidepws.cfapps.io
last uploaded: Fri Feb 3 09:35:52 UTC 2017
stack: cflinuxfs2
buildpack: java_buildpack
state since cpu memory disk details
#0 running 2017-02-03 10:37:26 AM 260.5% 485.8M of 1G 194.7M of 1G
你可以在 PWS 控制台中检查你的应用程序状态
5 使用 Gradle 在 PWS 上部署
在项目外部创建一个 gradle.properties
文件,其中包含你的 cf 凭证
$ cat ~/.gradle/gradle.properties
[email protected]
cfPassword=secret
添加 Gradle CloudFoundry 插件
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.cloudfoundry:cf-gradle-plugin:1.1.2'
}
}
apply plugin: 'cloudfoundry'
配置你的应用程序服务
bootRun {
jvmArgs('-Dspring.output.ansi.enabled=always')
addResources = true
}
assets {
minifyJs = true
minifyCss = true
}
cloudfoundry {
target = "https://api.run.pivotal.io"
organization = "guides-grails-org"
application = "grailsguidepws"
space = "development"
生成一个 war 文件
./grailsw war
使用 gradle 部署
./gradlew cfPush