在PCF Spring Boot应用程序上访问VCAP属性 [英] Accessing VCAP properties on PCF Spring Boot app

查看:70
本文介绍了在PCF Spring Boot应用程序上访问VCAP属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在PCF上运行的简单Spring Boot应用程序.我想知道是否有更好的方法可以从PCF访问VCAP环境变量.具体来说,我正在尝试访问PCF上运行并绑定到我的应用程序的RabbitMQ服务实例的服务凭据.

I have a simple Spring Boot app running on PCF. I am wondering if there is a better way to access VCAP environment variables from PCF. Specifically, I am trying to access service credentials for a RabbitMQ service instance running on PCF and bound to my application.

此刻,我使用System.getenv访问凭据:

At the moment, I access the credentials using System.getenv:

JSONObject vcapServices = new JSONObject(System.getenv("VCAP_SERVICES"));
JSONArray rabbitmq = (JSONArray) vcapServices.get("p-rabbitmq");
JSONObject serviceInfo = (JSONObject) rabbitmq.get(0);
JSONObject credentials = (JSONObject) serviceInfo.get("credentials");

hostname = credentials.getString("hostname");
virtualHost = credentials.getString("vhost");
username = credentials.getString("username");
password = credentials.getString("password");

我试图使其与@Value注释一起使用,以尝试访问VCAP环境变量,如下所示:

I was trying to get it working with the @Value annotation to try to access the VCAP environment variables like this:

@Value("${vcap.services.p-rabbitmq.credentials.hostname}")
private String hostname;

但是我还无法获取这些值.

but I haven't been able to grab the values yet.

我可以通过@Value注释访问VCAP变量吗?或者,除了System.getenv之外,还有其他更好的方法可以在我部署应用程序后从PCF获取这些凭据吗?

Is there any way I can access VCAP variables via the @Value annotation? Or is there a better way other than System.getenv to get these credentials from PCF once I deploy my application?

推荐答案

我可以通过@Value注释访问VCAP变量吗?

Is there any way I can access VCAP variables via the @Value annotation?

是的,我认为您需要稍微更改格式.它是 vcap.services.< service-instance-name> .credentials.hostname .您具有服务提供者本身的名称,而不是服务实例的名称.在上面的示例中看不到该名称,但是我发现Javadoc很有帮助.它列出了以下示例.

Yes, I think you need to change your format slightly. It's vcap.services.<service-instance-name>.credentials.hostname. You have the name of the service provider itself, not the name of your service instance. I can't see that name in the example above, but I've found the Javadoc to be helpful. It lists the following example.

VCAP_SERVICES: {
    "rds-mysql-1.0": [
        {
            "credentials": {
                "host": "mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com",
                "hostname": "mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com",
                "name": "d04fb13d27d964c62b267bbba1cffb9da",
                "password": "pxLsGVpsC9A5S",
                "port": 3306,
                "user": "urpRuqTf8Cpe6",
                "username": "urpRuqTf8Cpe6"
            },
            "label": "rds-mysql-1.0",
            "name": "mysql",
            "plan": "10mb"
        }
    ]
}

在此示例中,服务提供商名称为"rds-mysql-1.0",与"p-rabbitmq"相同.在您的示例中.然后,其服务实例名称为"mysql".&这就是您要在媒体占位符中使用的内容.

In this example, the service provider name is "rds-mysql-1.0", which would be the same as "p-rabbitmq" in your example. It then has a service instance name of "mysql" & that is what you'd use in your property placeholder.

例如: vcap.services.mysql.credentials.host

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/cloud/CloudFoundryVcapEnvironmentPostProcessor.html

已经解释了所有这一切,将您的所有资产连线起来仍然是一项相当大的工作.尤其是如果您要使用多种服务,或者您要使用的情况是使用RabbitMQ,它具有通过VCAP_SERVICES传递的大量信息.

Having explained all that, it can still be a fair bit of work to wire up all of your properties. Especially if you're utilizing multiple services or in your case, you're using RabbitMQ which has a lot of info that is passed through VCAP_SERVICES.

更新

Spring Cloud Connector仍然是一个选项,将其保留在下面以保留历史背景,但是 java-cfenv是以后访问服务属性的推荐方法.它与Spring Boot配合使用时效果更好,并且功能强大.

Spring Cloud Connectors is still an option, leaving it below for historical context, but java-cfenv is the recommended way to access service properties going forward. It plays nicer with Spring Boot and is every bit as capable.

已弃用

要考虑的另一种选择是 Spring Cloud连接器.这是一个基本上可以为您解决所有问题的库.您需要一个DataSource或ConnectionFactory,并确保将其连接到您的应用程序中.

Another option to consider is Spring Cloud Connectors. This is a library that will basically handle all of this for you. You ask for a DataSource or ConnectionFactory and it makes sure one is wired into your application.

例如:

@Bean
public RabbitConnectionFactory rabbitFactory() {
    return connectionFactory().rabbitConnectionFactory();
}

请参见入门对此感兴趣.

这篇关于在PCF Spring Boot应用程序上访问VCAP属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆