如何在Java代码中使用SAP云平台CloudFoundry环境的环境变量

本文使用的例子源代码在我的github上。

在我的公众号文章在SAP云平台的CloudFoundry环境下消费ABAP On-Premise OData服务介绍了如何通过Cloud Connector连接ABAP On-Premise系统。

我创建了三个service的实例,详情如上图和下表:

服务类型

服务实例名称

connectivity

connectivity-jerry-demo

destination

jerry-destination-lite

xsuaa

xsuaa-jerry-demo

这三个服务实例的名称维护在应用的manifest.xml中。

将应用使用cf push部署到CloudFoundry之后,在Cockpit里能观察到生成的系统变量VCAP_SERVICES:

在Java代码里,使用如下代码将该环境变量的值读取出来存储到字符串中。

private static final String VCAP_SERVICES = System.getenv("VCAP_SERVICES");

该Json对象的具体字段通过getJSONObject读取出来。

下图是connectivity实例的credentials明细:

要获取更多Jerry的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:

附录

VCAP_SERVICES全部内容:

{
	"VCAP_SERVICES": {
		"xsuaa": [
			{
				"name": "xsuaa-jerry-demo",
				"instance_name": "xsuaa-jerry-demo",
				"binding_name": null,
				"credentials": {
					"uaadomain": "authentication.eu10.hana.ondemand.com",
					"tenantmode": "dedicated",
					"sburl": "https://internal-xsuaa.authentication.eu10.hana.ondemand.com",
					"clientid": "sb-connectivity-jerry-demo!t3704",
					"verificationkey": "-----BEGIN PUBLIC KEY-----MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwThn6OO9kj0bchkOGkqYBnV1dQ3zU/xtj7Kj7nDd8nyRMcEWCtVzrzjzhiisRhlrzlRIEY82wRAZNGKMnw7cvCwNixcfcDJnjzgr2pJ+5/yDZUc0IXXyIWPZD+XdL+0EogC3d4+fqyvg/BF/F0t2hKHWr/UTXE6zrGhBKaL0d8rKfYd6olGWigFd+3+24CKI14zWVxUBtC+P9Fhngc9DRzkXqhxOK/EKn0HzSgotf5duq6Tmk9DCNM4sLW4+ERc6xzrgbeEexakabvax/Az9WZ4qhwgw+fwIhKIC7WLwCEJaRsW4m7NKkv+eJR2LKYesuQ9SVAJ3EXV86RwdnH4uAv7lQHsKURPVAQBlranSqyQu0EXs2N9OlWTxe+FyNkIvyZvoLrZl/CdlYc8AKxRm5rn2/88nkrYQ0XZSrnICM5FRWgVF2hn5KfZGwtBN85/D4Yck6B3ocMfyX7e4URUm9lRPQFUJGTXaZnEIge0R159HUwhTN1HvyXrs6uT1ZZmW+c3p47dw1+LmUf/hIf8zd+uvHQjIeHEJqxjqfyA8yqAFKRHKVFrwnwdMHIsRap2EKBhHMfeVf0P2th5C9MggYoGCvdIaIUgMBX3TtCdvGrcWML7hnyS2zkrlA8SoKJnRcRF2KxWKs355FhpHpzqyZflO5l98+O8wOsFjGpL9d0ECAwEAAQ==-----END PUBLIC KEY-----",
					"xsappname": "connectivity-jerry-demo!t3704",
					"identityzone": "jerry",
					"identityzoneid": "f3bd7f84-ebf1-4c11-b7c7-a278ebd54dcb",
					"clientsecret": "WAxzZxYQCgB7Dkml8dFdU+kaLSE=",
					"tenantid": "f3bd7f84-ebf1-4c11-b7c7-a278ebd54dcb",
					"url": "https://jerry.authentication.eu10.hana.ondemand.com"
				},
				"syslog_drain_url": null,
				"volume_mounts": [],
				"label": "xsuaa",
				"provider": null,
				"plan": "application",
				"tags": [
					"xsuaa"
				]
			}
		],
		"connectivity": [
			{
				"name": "connectivity-jerry-demo",
				"instance_name": "connectivity-jerry-demo",
				"binding_name": null,
				"credentials": {
					"uaadomain": "authentication.eu10.hana.ondemand.com",
					"onpremise_proxy_port": "20003",
					"tenantmode": "dedicated",
					"clientid": "sb-clone5282c913c9164bb284239adcc4d74210!b3704|connectivity!b17",
					"verificationkey": "-----BEGIN PUBLIC KEY-----MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwThn6OO9kj0bchkOGkqYBnV1dQ3zU/xtj7Kj7nDd8nyRMcEWCtVzrzjzhiisRhlrzlRIEY82wRAZNGKMnw7cvCwNixcfcDJnjzgr2pJ+5/yDZUc0IXXyIWPZD+XdL+0EogC3d4+fqyvg/BF/F0t2hKHWr/UTXE6zrGhBKaL0d8rKfYd6olGWigFd+3+24CKI14zWVxUBtC+P9Fhngc9DRzkXqhxOK/EKn0HzSgotf5duq6Tmk9DCNM4sLW4+ERc6xzrgbeEexakabvax/Az9WZ4qhwgw+fwIhKIC7WLwCEJaRsW4m7NKkv+eJR2LKYesuQ9SVAJ3EXV86RwdnH4uAv7lQHsKURPVAQBlranSqyQu0EXs2N9OlWTxe+FyNkIvyZvoLrZl/CdlYc8AKxRm5rn2/88nkrYQ0XZSrnICM5FRWgVF2hn5KfZGwtBN85/D4Yck6B3ocMfyX7e4URUm9lRPQFUJGTXaZnEIge0R159HUwhTN1HvyXrs6uT1ZZmW+c3p47dw1+LmUf/hIf8zd+uvHQjIeHEJqxjqfyA8yqAFKRHKVFrwnwdMHIsRap2EKBhHMfeVf0P2th5C9MggYoGCvdIaIUgMBX3TtCdvGrcWML7hnyS2zkrlA8SoKJnRcRF2KxWKs355FhpHpzqyZflO5l98+O8wOsFjGpL9d0ECAwEAAQ==-----END PUBLIC KEY-----",
					"xsappname": "clone5282c913c9164bb284239adcc4d74210!b3704|connectivity!b17",
					"identityzone": "jerry",
					"clientsecret": "nyiGD9QQSOZO+NTeEJdexWWjxYY=",
					"tenantid": "f3bd7f84-ebf1-4c11-b7c7-a278ebd54dcb",
					"url": "https://jerry.authentication.eu10.hana.ondemand.com",
					"onpremise_proxy_host": "10.0.85.1"
				},
				"syslog_drain_url": null,
				"volume_mounts": [],
				"label": "connectivity",
				"provider": null,
				"plan": "lite",
				"tags": [
					"connectivity",
					"conn",
					"connsvc"
				]
			}
		],
		"destination": [
			{
				"name": "jerry-destination-lite",
				"instance_name": "jerry-destination-lite",
				"binding_name": null,
				"credentials": {
					"uaadomain": "authentication.eu10.hana.ondemand.com",
					"tenantmode": "dedicated",
					"clientid": "sb-clone39a01503ac1845fbbda5d5ffccb1a469!b3704|destination-xsappname!b404",
					"instanceid": "39a01503-ac18-45fb-bda5-d5ffccb1a469",
					"verificationkey": "-----BEGIN PUBLIC KEY-----MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwThn6OO9kj0bchkOGkqYBnV1dQ3zU/xtj7Kj7nDd8nyRMcEWCtVzrzjzhiisRhlrzlRIEY82wRAZNGKMnw7cvCwNixcfcDJnjzgr2pJ+5/yDZUc0IXXyIWPZD+XdL+0EogC3d4+fqyvg/BF/F0t2hKHWr/UTXE6zrGhBKaL0d8rKfYd6olGWigFd+3+24CKI14zWVxUBtC+P9Fhngc9DRzkXqhxOK/EKn0HzSgotf5duq6Tmk9DCNM4sLW4+ERc6xzrgbeEexakabvax/Az9WZ4qhwgw+fwIhKIC7WLwCEJaRsW4m7NKkv+eJR2LKYesuQ9SVAJ3EXV86RwdnH4uAv7lQHsKURPVAQBlranSqyQu0EXs2N9OlWTxe+FyNkIvyZvoLrZl/CdlYc8AKxRm5rn2/88nkrYQ0XZSrnICM5FRWgVF2hn5KfZGwtBN85/D4Yck6B3ocMfyX7e4URUm9lRPQFUJGTXaZnEIge0R159HUwhTN1HvyXrs6uT1ZZmW+c3p47dw1+LmUf/hIf8zd+uvHQjIeHEJqxjqfyA8yqAFKRHKVFrwnwdMHIsRap2EKBhHMfeVf0P2th5C9MggYoGCvdIaIUgMBX3TtCdvGrcWML7hnyS2zkrlA8SoKJnRcRF2KxWKs355FhpHpzqyZflO5l98+O8wOsFjGpL9d0ECAwEAAQ==-----END PUBLIC KEY-----",
					"xsappname": "clone39a01503ac1845fbbda5d5ffccb1a469!b3704|destination-xsappname!b404",
					"identityzone": "jerry",
					"clientsecret": "QCnO+2k8gvUkdTyiCZI5rhzylbQ=",
					"tenantid": "f3bd7f84-ebf1-4c11-b7c7-a278ebd54dcb",
					"uri": "https://destination-configuration.cfapps.eu10.hana.ondemand.com",
					"url": "https://jerry.authentication.eu10.hana.ondemand.com"
				},
				"syslog_drain_url": null,
				"volume_mounts": [],
				"label": "destination",
				"provider": null,
				"plan": "lite",
				"tags": [
					"destination",
					"document"
				]
			}
		],
		"application-logs": [
			{
				"name": "jerry-log",
				"instance_name": "jerry-log",
				"binding_name": null,
				"credentials": {},
				"syslog_drain_url": "https://10.0.104.16:4433/syslogv2/ZmE4OTNmMjYtNjllZS00YTRhLWFhMDQtOGY4YWUwNjc2NDc1L2Nvbm5lY3Rpdml0eS1qZXJyeS1kZW1vLzY5YWJmZDQ2LWQyMjgtNDkwMy1iMjQ1LWRlODhjZWZmMzY0Mi9kZXYvMDE2ZGUwODktMDc0MS00NzlhLTgyOGEtNDVjNGVmMTY5MzUyL0kwNDI0MTZ0cmlhbF9qZXJyeS8wNWMyYzY0Ny1lYjYwLTQwYjQtYjhiYi02NTEyYTBlZmQ2NjIvYXBwbGljYXRpb24tbG9ncy8zZTQ2NmZjNi04MzNlLTQzMGEtYTVhMS1iOWFhMjMxOTUwYzgvbGl0ZS81MWYzYWE3Mi1lYzE4LTQ4YTgtYTVjNS1lNGJlMWVlMDQ2MTUvamVycnktbG9nLzA3Y2MwOWZlZTFmM2VjNDNhYWIzZmMzMTAwMzQ5NTMwYmIzMjJmODE0NDE1ZGI3MTBkM2JlM2NmMDliNTI4Mjc",
				"volume_mounts": [],
				"label": "application-logs",
				"provider": null,
				"plan": "lite",
				"tags": []
			}
		]
	}
}
{
	"VCAP_APPLICATION": {
		"cf_api": "https://api.cf.eu10.hana.ondemand.com",
		"limits": {
			"fds": 16384,
			"mem": 512,
			"disk": 1024
		},
		"application_name": "connectivity-jerry-demo",
		"application_uris": [
			"connectivity-jerry-demo-i042416.cfapps.eu10.hana.ondemand.com"
		],
		"name": "connectivity-jerry-demo",
		"space_name": "dev",
		"space_id": "69abfd46-d228-4903-b245-de88ceff3642",
		"uris": [
			"connectivity-jerry-demo-i042416.cfapps.eu10.hana.ondemand.com"
		],
		"users": null,
		"application_id": "fa893f26-69ee-4a4a-aa04-8f8ae0676475",
		"version": "d964d7cc-e067-4810-b834-25c995fe1bc5",
		"application_version": "d964d7cc-e067-4810-b834-25c995fe1bc5"
	}
}

要获取更多Jerry的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏一枝花算不算浪漫

[Java面试三]JavaWeb基础知识总结.

4699
来自专栏小尘哥的专栏

要“鱼”也要“渔”-java手写代码生成器

思路:依然依赖mp组件,因为mp已经提供了对应的BaseService,BaseMapper等,但是本文重点不在于“怎么用”,而在于“怎么写”。“怎么写”属于自...

882
来自专栏SpringBoot 核心技术

第五章:使用QueryDSL与SpringDataJPA实现查询返回自定义对象

3684
来自专栏个人分享

Kudu存储实战笔记

  就像官网所说,Kudu是一个针对Apache hadoop 平台而开发的列式存储管理器,在本菜鸟看来,它是一种介于hdfs与hbase的一种存储。它的优势在...

2483
来自专栏数据库

使用VBA创建Access数据表

导读: 本期介绍如何在Access数据库中创建一张空数据表。下期将介绍如何将工作表中的数据存入数据库对应的表中,随后还将介绍如何从数据库的表中取出数据输出到Ex...

2077
来自专栏分布式系统和大数据处理

数据库对象命名参考

编码规范是一个优秀程序员的必备素质,然而,有很多人非常注重程序中变量、方法、类的命名,却忽视了同样重要的数据库对象命名。这篇文章结合许多技术文章和资料,以及我自...

702
来自专栏Jerry的SAP技术分享

使用ABAP正则表达式解析HTML标签

需求就是我用ABAP的某个函数从数据库读取一个字符串出来,该字符串的内容是一个网页。

972
来自专栏Java帮帮-微信公众号-技术文章全总结

高级框架-springDate-JPA 第二天【悟空教程】

通过annotation(注解)来映射实体类和数据库表的对应关系,基于annotation的主键标识为@Id注解, 其生成规则由@GeneratedValue ...

801
来自专栏王小雷

SAS进阶《深入解析SAS》之Base SAS基础、读取外部数据到SAS数据集

SAS进阶《深入解析SAS》之Base SAS基础、读取外部数据到SAS数据集 前言:在学习完《SAS编程与商业案例》后,虽然能够接手公司的基本工作,但是为了更...

1967
来自专栏恰同学骚年

《T-SQL查询》读书笔记Part 3.索引的基本知识

索引优化是查询优化中最重要的一部分,索引是一种用于排序和搜索的结构,在查找数据时索引可以减少对I/O的需要;当计划中的某些元素需要或是可以利用经过排序的数据时,...

1113

扫码关注云+社区