Cloud Foundry:我们的博客APP - 如何绑定服务

在上一篇文章< Cloud Foundry 快速入门 (cf工具) >,我们简单快速的上传了一个小应用,了解了CF部署APP的流程。但是如果我们想要APP有更强大的功能,它就必须有那些最常见的服务,例如数据库(mysql, postgres)键值存储(redis, rabbitmq),log分析等等。幸运的是,很多最流行服务的供应商已经和CF合作集成,可以即时的给我们的APP提供服务。
今天我会为大家介绍如何创建CF的服务,并绑定该服务到我们的APP。我们所push的APP是根据这个rails教程做成的blogger。(http://tutorials.jumpstartlab.com/projects/blogger.html)你可以克隆我的修改: git clone http://github.com/jimmida/blogger.git。这是一个和数据库结合的博客应用程序,所以我们会在CF里创建一个mysql数据库服务。

整个流程的大概顺序是:
1. 创建应用 (暂时不启动)
2. 创建服务 (可以和1互换)
3. 绑定应用和服务
4. 启动已经绑定服务的应用

- 服务的市场 (cf marketplace)

  'cf marketplace'会列出当前CF所集成的所有服务(service)以及它们的套餐(plans)。目前免费账户可以使用最初级的套餐。

  ~/workspace/blogger $ cf marketplace 

Getting services from marketplace in org my-pivotal-org / space development as jda@gopivotal.com... OK

service          plans                                                                 description
blazemeter       free-tier, basic1kmr, pro5kmr, pp10kmr, hv40kmr                       The JMeter Load Testing Cloud
cleardb          spark, boost, amp, shock                                              Highly available MySQL for your Apps.
cloudamqp        lemur, tiger, bunny, rabbit, panda                                    Managed HA RabbitMQ servers in the cloud
cloudforge       free, standard, pro                                                   Development Tools In The Cloud
elephantsql      turtle, panda, hippo, elephant                                        PostgreSQL as a Service
ironmq           pro_platinum, pro_gold, large, medium, small, pro_silver              Powerful Durable Message Queueing Service
ironworker       large, pro_gold, pro_platinum, pro_silver, small, medium              Scalable Background and Async Processing
loadimpact       lifree, li100, li500, li1000                                          Cloud-based, on-demand website load testing
memcachedcloud   25mb, 100mb, 250mb, 500mb, 1gb, 2-5gb, 5gb                            Enterprise-Class Memcached for Developers
mongolab         sandbox                                                               Fully-managed MongoDB-as-a-Service
newrelic         standard                                                              Manage and monitor your apps
rediscloud       25mb, 100mb, 250mb, 500mb, 1gb, 2-5gb, 5gb, 10gb, 50gb                Enterprise-Class Redis for Developers
searchify        small, plus, pro                                                      Custom search you control
searchly         small, micro, professional, advanced, starter, business, enterprise   Search Made Simple. Powered-by ElasticSearch
sendgrid         free, bronze, silver, gold, platinum                                  Email Delivery. Simplified.

- 服务的创建 (cf create-service)

  比如我们需要一个数据库服务,我们可以调用下面的命令来创建这个服务,而服务供应商则会根据我们的要求,在他们的'云'里建造一个新的数据库。
  ~/workspace/blogger $ cf create-service cleardb spark mydb
Creating service mydb in org my-pivotal-org / space development as jda@gopivotal.com...
OK
  现在我们就可以在自己的空间里看到新建的数据库服务。

  ~/workspace/blogger $ cf services
Getting services in org my-pivotal-org / space development as jda@gopivotal.com...
OK

name   service   plan    bound apps
mydb   cleardb   spark

- 服务的绑定 (cf bind-service)

  服务的绑定是指应用程序知道服务的存在,并且获取其验证信息的步骤。我们现在已经有了服务,那我们就来上传blogger吧!

  我对教程里代码的主要改动(1 of 3)是config/database.yml。数据库验证信息都改成了动态的获取,是从一个'VCAP_SERVICES'的环境变量读出的。这个环境变量则是在绑定服务之后而生成的。
<%
  mydb = JSON.parse(ENV['VCAP_SERVICES'])["cleardb"]
  credentials = mydb.first["credentials"]
%>
...
production:
  adapter: pg
  encoding: utf8
  reconnect: false
  pool: 5
  host: <%= credentials["host"] %>
  username: <%= credentials["username"] %>
  password: <%= credentials["password"] %>
  database: <%= credentials["database"] %>
  port: <%= credentials["port"] %>
  第二个改动是在Gemfile里加了一行"gem 'mysql2'"。这样我们的blogger就可以通过这个依赖和数据库联系。

  第三个改动则是在config/environments/production.rb,将config.assets.compile = false改成了config.assets.compile = true。这个改动主要是用于启用assets(javascript, scss)的动态编译。

  我们可以push了!需要注意的是我们上传APP后还不能start,因为我们还未绑定APP所需的数据库。
  ~/workspace/blogger $ cf push iblogger --no-start
Creating app iblogger in org my-pivotal-org / space development as jda@gopivotal.com...
OK

Using route iblogger.cfapps.io
Binding iblogger.cfapps.io to iblogger...
OK

Uploading iblogger...
Uploading from: /Users/jda/workspace/blogger
1.8M, 480 files
OK
  上传完毕!MySQL - ready! APP - ready! 开始绑定MySQL - APP绑定!
  ~/workspace/blogger $ cf bind-service iblogger mydb
Binding service mydb to app iblogger in org my-pivotal-org / space development as jda@gopivotal.com...
OK
TIP: Use 'cf push' to ensure your env variable changes take effect

- APP的运行 (cf push)


  根据上一个命令返回的提示,这时我们可以重新启动(push)我们的APP。在下面的命令里<-c 'bundle exec rake db:migrate && bundle exec rails s -p $PORT'>注明的是APP编译好要运行的命令:'bundle exec rake db:migrate'会将数据库初始化;'bundle exec rails s -p $PORT'会启动rails的webserver。

  ~/workspace/blogger $ cf push iblogger -c 'bundle exec rake db:migrate && bundle exec rails s -p $PORT'
 Updating app iblogger in org my-pivotal-org / space development as jda@gopivotal.com...
  OK

  Uploading iblogger...
  Uploading from: /Users/jda/workspace/blogger
  1.8M, 480 files
  OK

  Starting app iblogger in org my-pivotal-org / space development as jda@gopivotal.com...
  OK
  -----> Downloaded app package (8.5M)
  -----> Using Ruby version: ruby-1.9.3
  -----> Installing dependencies using Bundler version 1.3.2
         Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
         Installing rake (10.1.0)
         Installing i18n (0.6.5)
         ...[output omitted]
  -----> Writing config/database.yml to read from DATABASE_URL
  -----> Preparing app for Rails asset pipeline
         Detected manifest file, assuming assets were compiled locally
  -----> WARNINGS:
         ...
  -----> Uploading droplet (44M)

  0 of 1 instances running, 1 starting
  0 of 1 instances running, 1 starting
  1 of 1 instances running

  App started

  Showing health and status for app iblogger in org my-pivotal-org / space development as jda@gopivotal.com...
  OK

  requested state: started
  instances: 1/1
  usage: 1G x 1 instances
  urls: iblogger.cfapps.io

       state     since                    cpu    memory         disk
  #0   running   2014-04-30 06:18:25 PM   0.0%   171.1M of 1G   121.3M of 1G
  如果我们想亲自连接到数据库里查看我们的数据信息,我们可以通过'cf files'来获取数据库的验证信息。(请放心,我把下面数据库的密码改掉了:D)

  ~/workspace/blogger $ cf files iblogger logs/env.log

Getting files for app iblogger in org my-pivotal-org / space development as jda@gopivotal.com...
OK

TMPDIR=/home/vcap/tmp
VCAP_APP_PORT=63752
USER=vcap
VCAP_APPLICATION={"limits":{"mem":1024,"disk":1024,"fds":16384},"application_version":"152d7bc5-d2a2-4351-af07-552a62a75f1a","application_name":"iblogger","application_uris":["iblogger.cfapps.io"],"version":"152d7bc5-d2a2-4351-af07-552a62a75f1a","name":"iblogger","space_name":"development","space_id":"ce1a7115-ba9a-493e-8f74-145e7571ab7f","uris":["iblogger.cfapps.io"],"users":null,"instance_id":"d7209457daa5463c9c1840b94dbac877","instance_index":0,"host":"0.0.0.0","port":63752,"started_at":"2014-04-30 10:18:12 +0000","started_at_timestamp":1398853092,"start":"2014-04-30 10:18:12 +0000","state_timestamp":1398853092}
RACK_ENV=production
PATH=/home/vcap/app/bin:/home/vcap/app/vendor/bundle/ruby/1.9.1/bin:/bin:/usr/bin:/bin:/usr/bin
PWD=/home/vcap
LANG=en_US.UTF-8
VCAP_SERVICES={"cleardb":[{"name":"mydb","label":"cleardb","tags":["relational","Data Store","mysql"],"plan":"spark","credentials":{"jdbcUrl":"jdbc:mysql://username:password@mysql.host.url:3306/db_name","uri":"mysql://username:password@mysql.host.url:3306/db_name?reconnect=true","name":"db_name","hostname":"mysql.host.url","port":"3306","username":"username","password":"password"}}]}
SHLVL=1
HOME=/home/vcap/app
RAILS_ENV=production
GEM_PATH=/home/vcap/app/vendor/bundle/ruby/1.9.1:
PORT=63752
VCAP_APP_HOST=0.0.0.0
DATABASE_URL=mysql2://username:password@mysql.host.url:3306/db_name?reconnect=true
MEMORY_LIMIT=1024m
_=/usr/bin/env

iblogger.cfapps.io留言吧!!


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值