User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP...

25
Cloud Application Engine User Guide Issue 01 Date 2016-08-25 HUAWEI TECHNOLOGIES CO., LTD.

Transcript of User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP...

Page 1: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

Cloud Application Engine

User Guide

Issue 01

Date 2016-08-25

HUAWEI TECHNOLOGIES CO., LTD.

Page 2: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

Copyright © Huawei Technologies Co., Ltd. 2016. All rights reserved.No part of this document may be reproduced or transmitted in any form or by any means without prior writtenconsent of Huawei Technologies Co., Ltd. Trademarks and Permissions

and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd.All other trademarks and trade names mentioned in this document are the property of their respectiveholders. NoticeThe purchased products, services and features are stipulated by the contract made between Huawei and thecustomer. All or part of the products, services and features described in this document may not be within thepurchase scope or the usage scope. Unless otherwise specified in the contract, all statements, information,and recommendations in this document are provided "AS IS" without warranties, guarantees orrepresentations of any kind, either express or implied.

The information in this document is subject to change without notice. Every effort has been made in thepreparation of this document to ensure accuracy of the contents, but all statements, information, andrecommendations in this document do not constitute a warranty of any kind, express or implied.

Huawei Technologies Co., Ltd.Address: Huawei Industrial Base

Bantian, LonggangShenzhen 518129People's Republic of China

Website: http://www.huawei.com

Email: [email protected]

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

i

Page 3: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

Contents

1 Quick Start...................................................................................................................................... 11.1 Creating Container Apps................................................................................................................................................ 21.2 Managing Apps...............................................................................................................................................................41.3 Creating a Service...........................................................................................................................................................6

2 App Development Guide.............................................................................................................82.1 Container Apps............................................................................................................................................................... 92.1.1 Tomcat......................................................................................................................................................................... 92.1.2 Jetty..............................................................................................................................................................................92.1.3 PHP............................................................................................................................................................................102.1.4 Go.............................................................................................................................................................................. 122.1.5 Node.js.......................................................................................................................................................................142.2 Environment Variables................................................................................................................................................. 15

3 Service Use Guide....................................................................................................................... 183.1 VCAP_SERVICES....................................................................................................................................................... 193.2 Using Redis Service......................................................................................................................................................20

Cloud Application EngineUser Guide Contents

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

ii

Page 4: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

1 Quick Start

Cloud Application Engine (CAE) provides you with multiple app running environments andservices. You only need to create apps and upload your app codes. Then your apps can run onCloud Application Engine (CAE). With the domain name provided by Cloud ApplicationEngine (CAE), you can access your apps.

1.1 Creating Container Apps

1.2 Managing Apps

1.3 Creating a Service

Cloud Application EngineUser Guide 1 Quick Start

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

1

Page 5: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

1.1 Creating Container AppsThis section describes how to create an app named "sample" on Cloud Application Engine(CAE). For details on how to develop an app that can run on Cloud Application Engine(CAE), see 2 App Development Guide.

Creating an App

Step 1 Access Cloud Application Engine (CAE) and choose Container App Hosting > Create App,as shown in the following figure.

Figure 1-1 Creating an app

Step 2 Enter app information as prompted and create an app named "sample".

1. Configure the app.

NOTICEThe app name can contain a maximum of 32 characters and is case-insensitive.

Cloud Application EngineUser Guide 1 Quick Start

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

2

Page 6: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

2. (Optional) Select services.

NOTE

For details on how to create services, see 1.3 Creating a Service.

3. Check, confirm, and submit app information.After creating the app, you can find it in My Apps.

Step 3 Choose More > Update and upload the app code.

Figure 1-2 Uploading the app code

If the code is successfully uploaded, the message "Success" is displayed.

Step 4 Click Start to start the app.

After starting the app, you can access it through the link.

----End

Cloud Application EngineUser Guide 1 Quick Start

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

3

Page 7: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

1.2 Managing AppsAfter creating apps, you can scale instances, add routes, customize app variables, and bindservices.

Click Manage. The App Management page is displayed.

Elastic Scale

NOTICEYou are advised to use the default setting of automatic scale to avoid extra fees derived fromfrequent elastic scaling caused by misoperation.

For apps in a container, you can choose manual scale or automatic scale for app instances.

Parameters in advanced setting are described as follows:

l Count: time cycle for collecting sampling data (Cloud application engine collectssamples of CPU and memory usage every 15 seconds). The average of data during thatperiod is used to match policies to avoid data fluctuation.

l Duration: the time of continuous automatic scale, for example, CPU usage rate is higherthan 70% for more than 60 seconds. The continuous duration is used to prevent frequentpolicy matching caused by data fluctuation.

Cloud Application EngineUser Guide 1 Quick Start

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

4

Page 8: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

Figure 1-3 Diagram for fluctuation and duration

If you set the Count and Duration to 60 seconds and 300 seconds respectively, thecollected data is matched with the policy every 60 seconds and the duration is 300seconds. If the match is successful, the instance will be scaled.

l Interval: interval of matching. The interval is reserved for execution after matching toprevent continuous elastic scale requests.

l Min. qty of instances: the minimum number of instances.l Max. qty of instances: the maximum number of instances.

Monitoring PerformanceYou can monitor the performance of instances.

Correlated ServicesTo bind services to apps, click Bind More Services on the upper left of the CorrelatedServices tab page.

Cloud Application EngineUser Guide 1 Quick Start

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

5

Page 9: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

Configuration Management

To add domains, click Add Domain on the Configuration Management tab page. Tocustomize variables, click Add Customized Variable.

Files, Events, Alarms, and Logs

View the files, events, alarms, and logs of an application on the related tab page. If theapplication files fail to be queried by using the IE11browser, see Failure of QueryingApplication Files Using IE11.

1.3 Creating a ServiceCAE provides the Redis service for users. Each user can build an Redis instance. Create anRedis instance on the My Services page, as displayed in Figure 1-4.

Figure 1-4 Creating a service

To use CAE services, perform the following operation:

Cloud Application EngineUser Guide 1 Quick Start

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

6

Page 10: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

l When creating an application, select the service to be associated.

l Connect the application code to the service. For details, see 3 Service Use Guide

Cloud Application EngineUser Guide 1 Quick Start

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

7

Page 11: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

2 App Development Guide

Cloud Application Engine (CAE) provides you with the app running environment. You justneed to develop apps locally and then upload the app codes to Cloud Application Engine(CAE) to run the apps.

This guide describes how to develop an app that can run on Cloud Application Engine (CAE)from the perspective of app development.

2.1 Container Apps

2.2 Environment Variables

Cloud Application EngineUser Guide 2 App Development Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

8

Page 12: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

2.1 Container Apps

2.1.1 TomcatThis section describes how to develop Java Tomcat apps on Cloud Application Engine (CAE).

Prerequisite

Cloud Application Engine (CAE) supports only deployment of Java Tomcat apps that areuploaded using the Java War file format.

Code Compilation

If you want to develop a Java app that can run on Cloud Application Engine (CAE), you firstneed to compile Java Tomcat app code locally and ensure that the app canrun on the localenvironment.

Compression

If you have complied and tested the Java app, compress the related files by using any Warcompression tool. Then you can deploy the app on Cloud Application Engine (CAE), Forspecific operation methods, see 1.1 Creating Container Apps.

Example Code

app-sample-javaweb.war.

2.1.2 JettyThis section describes how to develop Java Jetty apps on Cloud Application Engine (CAE).

Prerequisite

Cloud Application Engine (CAE) supports only deployment of Java Jetty apps that areuploaded using the Java War file format.

Code Compilation

If you want to develop a Java Jetty app that can run on Cloud Application Engine (CAE), youfirst need to compile Java Jetty app code locally and ensure that the app can run in the localenvironment.

Compression

If you have complied and tested the Java Jetty app, compress the related files by using anyWar compression tool. Then you can deploy the app on Cloud Application Engine (CAE). Forspecific operation methods, see Step 3.

Cloud Application EngineUser Guide 2 App Development Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

9

Page 13: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

Example Code

app-sample-javaweb.war.

JDK Version

When using a Jetty container, you can select a Java Developer's Kit (JDK) version. Currently,JDK versions 1.6, 1.7, and 1.8 are supported. You can create a system.properties file in theroot directory of an app to specify the JDK version. An example of the file content is asfollows:

java.runtime.version=1.7

NOTE

It is recommended that the specified JDK version be the same as that in the source code compilationenvironment. That is, the JDK version in the app source code compilation environment is the same asthat selected upon PaaS deployment. The app container selected for source code deployment mustsupport the selected JDK version.

2.1.3 PHPThis section describes how to develop Hypertext Preprocessor (PHP) apps on CloudApplication Engine (CAE).

Currently, the platform supports PHP apps that use Composer to manage PHP third-partydependencies.

Prerequisite

You are familiar with basic knowledge of PHP and the installation and use of Apachecontainers and PHP Composer.

PHP Extension

The following table lists names and versions of PHP-related software supported by CloudApplication Engine (CAE).

Software Description

php Supports version 5.4 (in other versions, proper running of apps cannot beensured).

hhvm Supports version 3.2.

apache Supports Apache HTTPD 2.4.

nginx Supports versions 1.5, 1.6, and 1.7.

composer Manages dependencies. Composer allows you to declare libraries thatyour projects depend on and installs these libraries in the projects foryou.

apcu Optimizes PHP caches.

newrelic Monitors apps.

opcache Improves PHP performance.

Cloud Application EngineUser Guide 2 App Development Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

10

Page 14: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

The following modules can be built in Cloud Application Engine (CAE) for PHP to use:

amqp, apc, apcu, bz2, curl, dba, exif, fileinfo, ftp, gd, gettext, gmp, igbinary, imagick, imap,intl, ioncube, ldap, lua, mailparse, mbstring, mcrypt, memcache, memcached, mongo,msgpack, mysql, mysqli, opcache, openssl, pdo, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql,phalcon, phpiredis, pspell, redis, snmp, soap, sockets, suhosin, sundown, twig, xcache,xdebug, xhprof, xsl, yaf, zip, zlib and zookeeper.

composer.json File Writing

Cloud Application Engine (CAE) uses Composer to download, install, and managedependencies of PHP apps. Before using Composer to manage dependencies of an app, youmust create a composer.json file in the root directory of the app. The composer.json file ismainly used to declare package relationships and other elements. In the composer.json file,you can configure third-party dependency packages and versions required by apps. Anexample of the composer.json file content is as follows:

{ "require": { "monolog/monolog": "1.2.*" }}

The require configuration item specifies the dependency package required by a project. Thepreceding example indicates that the current project needs to use the monolog/monologpackage in version 1.2. For details, see official composer website.

Code Compilation

You first need to create a PHP app entry file, which is named index.php in this example. Editthe file and add the following information to it:

<?phpecho "helloWorld";?>

Dependency Installation

To obtain and install dependencies to your local project, you only need to invokecomposer.phar to run the install command.

$php composer.phar install

According to the preceding example, you can find the latest version of monolog/monolog anddownload it to the vendor directory. It is a practice to save third-party codes to the vendordirectory. In case of monolog, the vendor/monolog/monolog directory will be created.

NOTE

If you are using Git to manage your projects, you possibly need to add vendor to your .gitignore file.You may not want to add all codes to your version library.

In addition, after you run the install command, a composer.lock file is created in the rootdirectory of your project.

Cloud Application EngineUser Guide 2 App Development Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

11

Page 15: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

Running PHP Programs Locally

After code compilation, you can access your PHP apps locally. You first need to start theApache server by running the following command:

/etc/.initd/apache2 start

After startup, you can access http://localhost/index.php to view PHP apps.

Compression

If your local PHP app has been compiled and tested, its directory structure is as follows:

app-sample|--index.php //Project entry file|--composer.json //Dependency management file

After compiling and testing app codes, compress all content in the project folder into a ZIPfile. Then you can deploy the app on Cloud Application Engine (CAE).

Example Code

app-sample-php.zip.

2.1.4 GoThis topic describes how to develop Go apps on Cloud Application Engine (CAE). CloudApplication Engine (CAE) supports Go 1.1, 1.1.1, 1.1.2, 1.2, 1.2.1, 1.2.2, 1.3, 1.3.1, 1.3.2,1.3.3, 1.4, and 1.4.1.

Prerequisites

Go language, Godep tool, and Git are correctly installed.

Compiling Code

To develop a Go app that can run on the cloud-inspired PaaS platform, you first need tocompile Go app code locally and ensure that the app can run in the local environment. Thefollowing is an example program web.go of the Go web app demoapp:

package main

import ( "fmt" "net/http" "os")

func main() { http.HandleFunc("/", hello) fmt.Println("listening...") err := http.ListenAndServe(":"+os.Getenv("PORT"), nil) if err != nil { panic(err) }}

func hello(res http.ResponseWriter, req *http.Request) { fmt.Fprintln(res, "hello, world")}

Cloud Application EngineUser Guide 2 App Development Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

12

Page 16: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

Use the go get tool to compile the Go app code as an executable file.

$ go get

Run the which command to check whether the executable file is generated.

$ which demoapp/Users/you/go/bin/demoap

Run the program.

$ PORT=5000 demoapplistening...

Send HTTP requests to check whether the program is running properly.

$ curl -i http://127.0.0.1:5000/HTTP/1.1 200 OKDate: Sat, 22 Sep 2012 15:30:33 GMTTransfer-Encoding: chunkedContent-Type: text/plain; charset=utf-8

hello, world

Create a Git library, which is required for the use of Godep.

$ git init$ git add -A .$ git commit -m code

Compiling Procfile

When deploying a Go app on Cloud Application Engine (CAE), you need to specify thestartup command for entries in the Procfile file.

web: demoapp

demoapp is the app name.

Packing a Go App

After compiling and testing the Go app, save a list of dependencies into Godeps/Godeps.jsonand copy the source code of dependencies to Godeps/_workspace.

$ godep save

The code directories are as follows:

demoapp|--web.go //Project entry file|--Procfile //Platform boot script (mandatory)|--Godeps //Go app dependency management folder

Compress the whole project folder (excluding the demoapp folder) into a zip file. Then youcan deploy the app on the PaaS platform.

Example Code

app-sample-go.zip.

Cloud Application EngineUser Guide 2 App Development Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

13

Page 17: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

2.1.5 Node.jsThis topic describes how to develop Node.js on Cloud Application Engine (CAE). CloudApplication Engine (CAE) supports Node.js v0.8.6-v0.8.27, v0.9.0-v0.9.12, v0.10.0-v0.10.29,and v0.11.0-v0.11.13. By default, v0.10.29 is in use.

Prerequisites

Cloud Application Engine (CAE) supports the deployment and running of Node.js apps.Before deploying apps, ensure that the following prerequisites are met:

l The package.json file exists in the root directory of your project. Cloud ApplicationEngine (CAE) will check this file to determine whether the apps are Node.js apps.

l Cloud Application Engine (CAE) supports Node.js v0.8.6-v0.8.26(stable), v0.9.0-v0.9.12(unstable), v0.10.0-v0.10.28(stable), or v0.11.0-v0.11.13(unstable).

Compiling Code

To develop a Node.js app that can run on Cloud Application Engine (CAE), you first need tocompile Node.js app code locally and ensure that the app can run in the local environment.The following is an example of source code of a Node.js app:

var express = require("express");var logfmt = require("logfmt");var server = express();var port = process.env.VCAP_APP_PORT || 3000;

server.use(logfmt.requestLogger());

server.get('/', function(req, res) { res.send('Hello World!');});server.listen(port);

If the Node.js app will run on Cloud Application Engine (CAE), set the environment variableVCAP_APP_PORT to specify the port on which the app will listen. If the app will run locally,you do not need to specify this environment variable as the app will, by default, listen on port3000.

Editing the package.json File

The package.json file must contain name and version attributes and specify dependencypackages and version information. The dependency package of the specified version will beautomatically installed using the npm install. For example:

{ "description": "A Node.js sample application", "version": "0.0.1", "author": "Mushroom", "dependencies": { "express": "*", "logfmt": "" }, "engines": { "node": "0.10.x" }, "name": "node-sample"}

Cloud Application EngineUser Guide 2 App Development Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

14

Page 18: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

If the specified version information is "*" or no version information is specified, the defaultstable version will be used. For details on compiling the package.json file, see the referencedocuments at the official website of Node.js org.

Running a Node.js App Locally

Install Node.js according to the guidelines at the official website of Node.js org. Then run thefollowing two commands to start the app.

npm installnode server.js

The first command downloads the specified dependency package specified in thepackage.json file to "./node_modules". If the app will run locally, the configuration code ofthe program port is as follows:

port= app.listen(process.env.VCAP_APP_PORT || 3000)

If the app listens on port 3000, the port can be accessed through "http://localhost: 3000".

Running a Node.js App on Cloud Application Engine (CAE)

Specify the startup command in the Procfile file. An example of the file contents is asfollows:

web: node server.js

Packing a Node.js App

After a local Node.js app is compiled and tested, its directory structure is similar to thefollowing:

app-sample|--app |--controllers |--views |--models|--public|--server.js //Project entry file|--package.json|--Procfile //Boot script (mandatory)

After compiling and testing the app code, compress the whole project holder (including theProcfile and package.json files) into a zip file.

Then you can deploy the app on Cloud Application Engine (CAE). For details, see 1.1Creating Container Apps.

Example Code

app-sample-nodejs.zip.

2.2 Environment VariablesAn environment variable refers to communication carriers between Cloud Application Engine(CAE) and deployed apps at runtime. You can view and create environment variables onCloud Application Engine (CAE).

Cloud Application EngineUser Guide 2 App Development Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

15

Page 19: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

MEMORY_LIMIT

Indicates the maximum memory amount available to each app instance. You can select thememory size when creating an app. If the memory used by an instance exceeds the upperlimit, the instance will be automatically restarted. If the instance is restarted frequently, it willbe terminated.

MEMORY_LIMIT=512M

PORT

Indicates the port used for communications between Droplet Execution Agent (DEA) andapps. Upon staging, DEA allocates ports to apps. Therefore, when obtaining a port, an appmust use the keyword PORT.

PORT=61857

PWD

Indicates the current working directory, namely, the location where buildpack executes apps.

PWD=/home/vcap

TMPDIR

Indicates the directory for storing temporary files.

TMPDIR=/home/vcap/tmp

USER

Indicates the user account used to execute apps.

USER=vcap

VCAP_APP_HOST

Indicates the IP address of the DEA host.

VCAP_APP_HOST=0.0.0.0

VCAP_APPLICATION

This variable contains useful information about deployed apps, and results are sent back inJSON format. The following table lists attributes of VCAP_APPLICATION.

Attribute Description

application_users, users User account used to run apps.

instance_id Global Unique Identifier (GUID) of an app instance,which is used to identify the instance.

instance_index Index of an app instance.

Cloud Application EngineUser Guide 2 App Development Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

16

Page 20: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

Attribute Description

application_version, version GUID of the version of the app that is running. When theapp data is uploaded again, the version informationautomatically updates.

application_name, name App name specified upon uploading and deployment ofan app.

application_uris Uniform Resource Identifier (URI) allocated to an appfor access.

started_at, start App startup time.

started_at_timestamp App startup timestamp.

host IP address or host name of an app instance.

port Port allocated to an app instance.

limits Quota limit allocated to an app instance, including limitson the memory, disk, the number of files, and others.

state_timestamp Timestamp when an app instance enters the current state.

VCAP_APPLICATION={"instance_id":"451f045fd16427bb99c895a2649b7b2a","instance_index":0,"host":"0.0.0.0","port":61857,"started_at":"2013-08-12 00:05:29 +0000","started_at_timestamp":1376265929,"start":"2013-08-12 00:05:29 +0000","state_timestamp":1376265929,"limits":{"mem":512,"disk":1024,"fds":16384},"application_version":"c1063c1c-40b9-434e-a797-db240b587d32","application_name":"styx-james","application_uris":["styx-james.a1-app.cf-app.com"],"version":"c1063c1c-40b9-434e-a797-db240b587d32","name":"styx-james","uris":["styx-james.a1-app.cf-app.com"],"users":null}

Cloud Application EngineUser Guide 2 App Development Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

17

Page 21: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

3 Service Use Guide

The difference between cloud apps and local apps regarding using services lies in that theirways of connecting to services. Local apps directly read all parameters of services, whereascloud apps need to read the parameters through the environment variable VCAP_SERVICESin the app running environment.

This section describes the environment variable VCAP_SERVICES in the app runningenvironment and uses a MySQL service as an example to show how to use services in apps.

3.1 VCAP_SERVICES

3.2 Using Redis Service

Cloud Application EngineUser Guide 3 Service Use Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

18

Page 22: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

3.1 VCAP_SERVICESThis section describes how to use the environment variable VCAP_SERVICES.

When a service instance is bound to a source code app, the connection and authenticationinformation of the service instance is injected to the environment variable VCAP_SERVICESin the running environment of the app. Then, you can view VCAP_SERVICES informationon the app.

To display the app management page, click Manage in the app list. On the ConfigurationManagement tab page, you can view the VCAP_SERVICES information of the app.

A VCAP_SERVICES example is as follows:

VCAP_SERVICES={ "mysql-cluster": [ { "name": "mysql-wp", "label": "mysql-cluster", "tags": [ "mysql cluster", "relational" ], "plan": "addplan", "credentials": { "hostname": "9.91.14.23", "port": 3306, "name": "cf_ba64ea5a_95fe_40bd_b2f3_9a9a09ee0fc6", "username": "zdc0u0stz4OPiBPX", "password": "MivJsrjohWRuEsDO", "uri": "mysql://zdc0u0stz4OPiBPX:[email protected]:3306/cf_ba64ea5a_95fe_40bd_b2f3_9a9a09ee0fc6?reconnect=true", "jdbcUrl": "jdbc:mysql://zdc0u0stz4OPiBPX:[email protected]:3306/cf_ba64ea5a_95fe_40bd_b2f3_9a9a09ee0fc6" } } ]}

Cloud Application EngineUser Guide 3 Service Use Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

19

Page 23: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

The following table lists VCAP_SERVICES attributes.

Table 3-1 VCAP_SERVICES attributes

Attribute Description

name Service instance name.

label Service name.

tags Service tag arrays.

plan Service plan name.

credentials A combination of JavaScript Object Notation (JSON) objects ofauthentication information related to access to specific services, for example,the IP address, port, user name, and password.

You can use a service name as the key value to obtain connection authentication information(including the IP address, port, user name, and password) of the service instancecorresponding to the VCAP_SERVICES object. For example, if the service named mysql-cluster is bounded to the app, mysql-cluster can be used as the key value to obtainauthentication information of the service instance.

3.2 Using Redis ServiceTo use the Redis service in the application, edit the application code to connect to the Redisservice.

The difference between how cloud applications and local applications use services lies in theway apps access services. The local applications read parameters of the service directly, whilethe cloud applications read parameters of the service by running the environmental variableVCAP_SERVICES.

The use of the Redis service is similar in different languages. This section describes how toconnect the Redis service to applications by taking Node.js apps as an example.

Cloud Application EngineUser Guide 3 Service Use Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

20

Page 24: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

VCAP_SERVICES

Table 3-2 VCAP_SERVICES attributes

Attribute Description

credentials A combination of JavaScript Object Notation (JSON) objects ofauthentication information related to access to specific services, for example,the IP address and port.

name Name of the service instance

label Service name

tags Service label

plan Name of the service plan

You can use a service name as the key value to obtain connection and authenticationinformation (including the IP address and port) of the service instance corresponding to theVCAP_SERVICES object.

Using Redis Service Through Node.js Applications

Add a dependency package corresponding to Redis service by adding the followinginformation into the package.json file:

"dependencies":{"redis":"*"},

Parse connection and authentication information about the Redis service from theenvironmental variable VCAP_SERVICES. The following is example code:

function getRedisCredentials(){ if (process.env.VCAP_SERVICES) { var env = JSON.parse(process.env.VCAP_SERVICES); var credentials = url.parse(env['redis'][0]['credentials']['url']);

Cloud Application EngineUser Guide 3 Service Use Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

21

Page 25: User Guide - developer-res-cbc-cn.obs.cn-north-1 ... › ... · containers and PHP Composer. PHP Extension The following table lists names and versions of PHP-related software supported

} else { var credentials = {"hostname":"127.0.0.1", "port":6379, "auth":""}; } return credentials}

Create an Redis service connection using the connection and authentication information. Thefollowing is example code:

var credentials = getRedisCredentials();var client = redis.createClient(credentials.port, credentials.hostname);if (credentials.auth != '') { client.auth(credentials.auth.split(':')[1]);}

Perform operation on corresponding data tables. The following is an example of obtaining thedataset from the Redis database:

client.set("message", "Hello, Huawei PaaS!");client.get("message", function(err, reply) { client.quit(); res.end("Fetched Message: " + reply);});

Downloading Example Codeapp-sample-node-redis.zip.

Cloud Application EngineUser Guide 3 Service Use Guide

Issue 01 (2016-08-25) Huawei Proprietary and ConfidentialCopyright © Huawei Technologies Co., Ltd.

22