Pivotal Cloud foundry Access Service from Java App

不羁的心 提交于 2019-12-25 09:02:00

问题


How would I access credentials from a user provided service bound to a java app.

For example if I create a service like so:

cf cups <service instance> -p "DB_URL, DB_USERNAME, DB_PASSWORD".

and bind to a java app

In Java Main what would I do to access that service?

`  public static void main(String[] args) throws Exception{ 

    ??????
}`

回答1:


In your Java app, you would access the VCAP_SERVICES environment variable.

You can find detailed information here: http://docs.pivotal.io/pivotalcf/1-7/services/binding-credentials.html




回答2:


Spring Cloud Connectors can be used to parse the VCAP_SERVICES environment variable and provide the credentials in a Java object model. If you format that user-provided service instance credentials in a custom way, then you would need to write some extension code to tell Connectors how to parse the credentials.

A better way is to format the service instance credentials in a way that Connectors already understands. The easiest way to do this is usually by providing a single connection string. There are some hints in the Connectors docs showing the credentials formats that Connectors understands for various database types. So you could do something like this:

cf cups <service instance> -p '{"url", "mysql://username:password@hostname:3306/dbname"}'

cf cups <service instance> -p '{"jdbcUrl": "jdbc:mysql://hostname:3306/dbname?user=username&password=password"}'

or the equivalents for Postgres, Oracle, DB2, or SqlServer.

If you include the Spring Service Connector in your project, Connectors will detect the bound service and create the necessary connection objects (e.g. DataSource) for you.



来源:https://stackoverflow.com/questions/37715329/pivotal-cloud-foundry-access-service-from-java-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!