Technical Articles
通过Standalone Approuter 消费OnPremise数据源
写在开头
已经有很多开发人员在SAP业务技术平台(以下简称为BTP)上发布了自己的应用以及完整的解决方案。很多客户在SAP BTP业务技术平台上进行开发时,经常很困惑关于Approuter的问题,关键是还分SAP Managed Approuter和Standalone Approuter两种,使得客户和合作伙伴在开发前端应用时面临选型的疑惑,甚至想选用Standalone Approuter,但是不知道如何使用。在这篇文章里我会详细介绍如何使用Standalone Approuter配合Connectivity服务以及Destinaiton服务来消费企业内网里的数据资源。
本文用一个简单的页面index.html通过Connectivity 服务和Destination服务配合使用,从而连接到企业内网OnPremise的OData数据源。在这里不会花太多篇幅解释SAP Managed Approuter与Standalone Approuter的区别,想了解这两种approuter的区别,请移步这里了解更多。
后续还会有一篇文章讲解如何将自己的APP部署到HTML5 repository中去,并通过standalone approuter去消费后端数据源(这里数据源可以是Cloud的OData数据源也可以是OnPremise的数据源)
HTML5应用通过standalone approuter消费OData数据源
架构图
项目配置
前期准备
-
安装并配置好SAP云连接器(SAP Cloud Connector),您可以参考这里 install SAP Cloud Connector with Docker, 或者在这里下载可安装版本。
-
在本项目中,我们使用 ES5 作为我们后端基于ABAP的OData数据源. Configure OP system in SAP Cloud Connector, 在SAP Cloud Connector里您需要配置自己的Location ID(任何利于区分的字符串即可).
项目开发
整个文件目录结构主要包含这些内容:
-
在MTA.yaml中配置Destination属性.
定义Destination名称为 es5, 可以从您配置的SAP Cloud Connector中获取到URL。并输入相应的ES5系统的用户名和密码(注意这里并不是SAP i-用户的域密码)。另外,需要添加一个参数sap-client,其值为002,注意这里的002必须用双引号括起来,否则会被转义成数字。这里的URL是SAP云连接器里配置好的Cloud To OnPremise配置里获取到。
浏览到destination_approuter模块,将YOUR.CUSTOM.DOMAIN.COM更新为您自己的真实域名。ID: destination-approuter _schema-version: "2.1" version: 1.0.0 modules: - name: destination_approuter type: approuter.nodejs path: approuter parameters: disk-quota: 256M memory: 256M routes: - route: opes5.YOUR.CUSTOM.DOMAIN.COM requires: - name: es5-xsuaa - name: es5-destination - name: es5-connectivity resources: - name: es5-connectivity type: org.cloudfoundry.managed-service parameters: service: connectivity service-plan: lite - name: es5-destination type: org.cloudfoundry.managed-service parameters: config: HTML5Runtime_enabled: true init_data: instance: destinations: - Authentication: BasicAuthentication Name: es5 ProxyType: OnPremise Type: HTTP URL: http://<Your.OnPremise.OData.Service> User: i000000 Password: xxxxxxxxxx sap-client: "002" existing_destinations_policy: update version: 1.0.0 service: destination service-name: es5-destination service-plan: lite - name: es5-xsuaa parameters: path: ./xs-security.json service-plan: application service: xsuaa type: org.cloudfoundry.managed-service
-
配置自定义域名
在BTP@AliCloud上,根据法规要求合法的域名是必须项目。您可以通过命令行cf domains
来确认自己是否已经具有合法的域名。如果拥有合法域名将会显示成如下图示:
另外,打开文件xs-security.json,按以下格式添加自己的域名到oauth2-configuration下:{ "xsappname": "desttest", "tenant-mode": "dedicated", "description": "Security profile of called application", "scopes": [ { "name": "uaa.user", "description": "UAA" } ], "role-templates": [ { "name": "Token_Exchange", "description": "UAA", "scope-references": [ "uaa.user" ] } ], "oauth2-configuration": { "redirect-uris": [ "https://*.YOUR.CUSTOM.DOMAIN.COM/**" ] } }
-
检查文件xs-app.json中关于路由的配置。Destination es5应该在列,如果没有,需要按以下格式添加进去。所有URL中包含es5的请求都会路由到Destination es5。Connectivity服务作为代理功能与SAP云连接器配合打通一个从内网到云环境的通道,从而实现从云端访问内网数据源的功能。
{ "welcomeFile": "index.html", "authenticationMethod": "route", "routes": [ { "source": "^/user-api(.*)", "target": "$1", "service": "sap-approuter-userapi" }, { "source": "^/es5(.*)", "target": "$1", "destination": "es5" }, { "source": "^(.*)$", "target": "$1", "authenticationType": "xsuaa", "localDir": "./" } ] }
部署应用
- 编译项目:
mbt build -t ./
- 部署应用到Cloud Foundry:
cf deploy destination-approuter_1.0.0.mtar
- 部署完成后的配置
登陆到您的SAP BTP子账号,浏览到Instances and Subscriptions,点击es5-destination进入destination的实例,从左边菜单栏选择Destination,然后编辑destination es5,添加Location ID。这个location ID可以在您配置的SAP Cloud Connector上找到并获取。Location ID是BTP子账号与OnPremise连接的重要识别依据,是必填字段。
部署完成后,并完成相应的配置,即可测试您的应用。登陆到您的BTP子账号,进入CF org/space/Applications, 找到部署好的应用destination_approuter,即可测试。