写一个Spring程序推送到Cloud Foundry,并尝试从外部访问

尝试内容:

自己编写一个后台程序,尝试推送到Cloud Foundry上,并开放从MindSphere以外访问的权限。

程序实现以下功能:

1、使用MindSphere提供的API获得指定时序的Token

2、启动定时任务,从时序数据中拉取数据,并打印在日志中。

3、提供一个REST接口,用来获取数据。

4、程序开启log日志,打印到Cloud Foundry的容器中。

前提条件:

1、电脑已经安装Power Shell。

2、提前编写好要测试的程序。

3、拥有MindSphere的Developer账号。

操作随记:

(一)编写程序

程序地址:https://github.com/quchunhui/demo-macket/tree/master/mindsphere

(二)登陆Cloud Foundry

Cloud Foundry的登陆,可以参考上一篇博客。

博客地址:https://www.cnblogs.com/quchunhui/p/12015167.html

(三)创建CF空间并赋予权限

1、切换到已有组织(org),相关命令如下:

切换组织:cf target -o rexelcn0

2、创建测试程序的运行空间(space),相关命令如下:

创建空间:cf create-space rexel-mf-api-test 

查看所有空间:cf spaces

3、更新空间的Role(角色)及Permission(权限),相关命令如下:

查看Space中的使用者及其权限:cf space-users rexelcn0 rexel-mf-api-test

附加:

如果希望使用其他账户完成后续的操作,可以使用一下命令来添加权限。

为指定用户添加SpaceManager权限:cf set-space-role [email protected] rexelcn0 rexel-mf-api-test SpaceManager

为指定用户添加SpaceDeveloper权限:cf set-space-role [email protected] rexelcn0 rexel-mf-api-test SpaceDeveloper

(四)将应用程序部署到Cloud Foundry

1、在程序根目录中创建manifest.yml文件

文件内容如下:

---
applications:
- name: rexel-mindsphere-api-test
  instances: 1
  random-route: true
  path: target/rexel-mindspere-api-0.0.1.jar
  memory: 512M  

  

2、将程序推送到CF的空间,相关命令如下:

跳转入Spaces:cf target -s rexel-mf-api-test

跳转到程序根目录:cd D:\MyWork\22_Github\rexel-cn\rexel-jarvis\rexel-mindsphere-api

推送程序:cf push

列出所有应用及运行状态:cf apps

查看应用的详细状态:cf app rexel-mf-api-test

【小插曲1】

执行cf push的时候报错一个错误

在程序根目录中使用mvn package重新编译打包,然后再次执行cf push之后问题解决。

【小插曲2】

程序上传之后,cf提示启动失败

按照提示的命令查看了日志。命令:cf.exe logs rexel-mindsphere-api-test --recent

可以看到提示内存不够。

尝试将manifest.yml中配置的内存大小调高,重新推送之后解决。修改之后的manifest.yml文件内容

---
applications:
- name: rexel-mindsphere-api-test
  instances: 1
  random-route: true
  path: target/rexel-mindspere-api-0.0.1.jar
  memory: 1G 

  

(五)SSH进入到空间查看运行

官网操作手册:https://developer.mindsphere.io/zh/paas/paas-cloudfoundry-ssh.html

1、检查空间SSH是否开启

命令:cf space-ssh-allowed rexel-mf-api-test

2、开始指定空间SSH权限:

默认是开启的,如果没有开启,可以使用以下命令打开权限。

命令:cf allow-space-ssh rexel-mf-api-test

3、为应用开启SSh权限:

命令:cf enable-ssh rexel-mindsphere-api-test

注意不要弄错了空间名(rexel-mf-api-test)和应用名(rexel-mindsphere-api-test)

4、重新启动应用:

命令:cf restart rexel-mindsphere-api-test

5、SSH进入应用空间:

命令:cf ssh rexel-mindsphere-api-test

6、进入日志所在路径:

在spring程序的logback中,配置的日志路径为:/home/vcap/logs

进入路径:cd /home/vcap/logs/

 7、查看日志是否正常:

命令:less rexel-md-test2019-12-11.log

Cloud Foundry

猜你喜欢

转载自www.cnblogs.com/quchunhui/p/12024493.html
今日推荐