Deploy applications using the Eclipse plug-in to CloudFoundry

First, what is Cloud Foundry

Cloud Foundry is an open source platform as a service product that provides developers with the freedom to choose cloud platform development frameworks and application services. Cloud Foundry was started by VMware, it has been widely supported by the industry, which allows developers to quickly and easily develop, test, deploy and scale applications. Cloud Foundry is an open source project, users can use a variety of private cloud distributions, you can use public cloud services, the official website http://www.cloudfoundry.com

Second, the registered developer account

To deploy applications to Cloud Foundry, developers must first apply for an account, https: //my.cloudfoundry.com/signup,

within a few minutes after the registration is complete, log on to your mailbox, you will receive a theme of "Welcome to Cloud Foundry -! Your signup is approved " , there are landing Cloud Foundry platform temporary username and password (later will explain how to change the password)

Third, install plug-ins Eclipse CloudFoundry

1) Start Eclipse, then select the menu Help--> Eclipse Marketplace, the pop-up box, enter Cloud Foundry, then search, click on the installation, after installation restart Eclipse, on behalf of the installation is complete.


2) the installation is complete need to bring up the plug-in uses, methods are: Window -> Show View, select Server in the pop-up box, click OK, this is our common will in the usual bar appears on the Server console this column. If we had used Tomcat, then, will be here to see the Tomcat service options.

Fourth, the deployment of applications to Cloud Foundry

(1)新建Cloud Foundry Server,在servers选项卡里单击右键-》new-》server-》选择Cloud Foundry,然后单击next

这时需要填入申请获得的账号和密码

然后单击next

这时可以将左边你的应用add到右边,然后单击finish,这时会弹出一个对话框,让你输入你部署在Cloud Foundry上web app名称

next,然后让你输入url地址(可修改)

然后单击finish后,又会弹出一个对话框问你绑定什么服务到你的应用,一般这里要设置数据库服务


单击加号选择mysql database

然后给你的这个服务起个名称,笔者这里命名为mysqlservice,然后单击finish

(2)Cloud Foundry参数设置
①修改密码
在Servers选项卡下双击VMware Cloud Foundry,弹出窗口

在overview里可以修改密码,修改后选择ChangePassword即可!
②启动mysql服务
单击Application选项卡,右击mysqlservice,然后选择open Tunnel

然后再右击mysqlservice选择show Tunnel information..

然后右击MySQLservice,选择copy all(这里主要用来复制cloudFoundry分配的数据名称,以及连接数据库的用户名和密码,以及端口)
比如笔者获得的信息如下:

UserName: u5SO4zfK53CcF    
Password: pM3G4xIbq1peR     
DatabaseName: df9bf5aaaa0fe4decbe74b96743761656     
Port: 10100     
URL: jdbc:mysql://127.0.0.1:10100/df9bf5aaaa0fe4decbe74b96743761656
This information is important because when we connect mysql database to use

③ remote operation of the database and the new method

In this method, by carrying Eclipse Data Source Explorer, select the menu Window- "show-" view- "Data Source Explorer

then right Database Connections-" select mysql- "next-" select mysql5.1 drive, if the drive prompted invalid, click the jar list tab, then select mysql-connector-java-5.1.20- bin.jar from your hard drive, and then fill in the above information we have obtained the database



and click test Connection, the test can No connection to the mysql service cloudFoundry offer, you can click finish successfully completed, after the completion of the following information can be seen in the data source Explorer tab

database Connectivity build good, how do you create a database, how to remotely execute sql it? Right-click the database MySQL_CF select open SQL Scrapbook, where we can write sql, then right-click to select execution!


④ Remote Method two new databases and operating
method for a somewhat inconvenient, we can use Navicat client to connect remotely mysql data, the new connection after you start Navicat following information:

user name, password, port fill in the information we get on top of that is can, then you can directly manipulate the database

5, how to get the data connection program

(1)Cloud Foundry提供了 VCAP_SERVICES 变量,该变量已在应用程序的 Cloud Foundry 环境中被设置。此变量的内容是一个 JSON 文件,其中包含一个与应用程序捆绑的所有配置服务的列表。以下为一个作为示例 JSON 文件(出于可读性考虑而重设了格式),该文件来自带有两个已配置 MySQL 服务的 Cloud Foundry 应用程序的环境。
{"mysql-5.1":[
    {
        "name":"mysql-4f700",
        "label":"mysql-5.1",
        "plan":"free",
        "tags":["mysql","mysql-5.1","relational"],
        "credentials":{
            "name":"d6d665aa69817406d8901cd145e05e3c6",
            "hostname":"mysql-node01.us-east-1.aws.af.cm",
            "host":"mysql-node01.us-east-1.aws.af.cm",
            "port":3306,
            "user":"uB7CoL4Hxv9Ny",
            "username":"uB7CoL4Hxv9Ny",
            "password":"pzAx0iaOp2yKB"
        }
    },
    {
        "name":"mysql-f1a13",
        "label":"mysql-5.1",
        "plan":"free",
        "tags":["mysql","mysql-5.1","relational"],
        "credentials":{
            "name":"db777ab9da32047d99dd6cdae3aafebda",
            "hostname":"mysql-node01.us-east-1.aws.af.cm",
            "host":"mysql-node01.us-east-1.aws.af.cm",
            "port":3306,
            "user":"uJHApvZF6JBqT",
            "username":"uJHApvZF6JBqT",
            "password":"p146KmfkqGYmi"
        }
    }
]}

使用访问操作系统环境变量的编程语言工具检索 VCAP_SERVICES 的值。例如,在 Java 中为 java.lang.System.getenv("VCAP_SERVICES"),在 Ruby 中为 ENV['VCAP_SERVICES']。在 Node.js (JavaScript) 中使用 process.env.VCAP_SERVICES,在 Python 中使用 os.getenv("VCAP_SERVICES")。
(2)数据库连接示例代码,以下代码采用fastjson去解析 VCAP_SERVICES 的值,采用jfinal框架的c3p0的配置
String db_json = java.lang.System.getenv("VCAP_SERVICES");
String dbname,username,password,host,port;
if(StringKit.isBlank(db_json)){
//本机数据库设置
	dbname = "blogdb";
	username = "root";
	password = "test";
	host = "localhost";
	port = "3306";
}else{
//cloundFoundry数据库信息解析
	JSONObject credentials = JSONObject.parseObject(db_json).getJSONArray("mysql-5.1").getJSONObject(0).getJSONObject("credentials");
	dbname =  credentials.getString("name");
        username = credentials.getString("username");
	password = credentials.getString("password");
	host      = credentials.getString("host");
	port      = credentials.getString("port");
}
C3p0Plugin cp = new C3p0Plugin("jdbc:mysql://" + host + ":" + port + "/" + dbname, username, password);

六、如何查看应用部署错误日志

若部署的应用出错,如何查看错误日志呢?CloudFoundry插件给我们提供了Remote Systems View,在Applications选项卡下双击你部署的应用,就会在右边下面有个Remote Systems View链接,双击即可打开远程视图








发布了41 篇原创文章 · 获赞 114 · 访问量 68万+

Guess you like

Origin blog.csdn.net/hil2000/article/details/8867303
Recommended