简体   繁体   中英

Not able to read VCAP_SERVICES in Bluemix

I used the following code to read my VCAP_SERVICES environment variable of Liberty application. And I am not getting any values, result shows null or "not found".

private void readECaaSEnvVars() {

          Map<?, ?> env = System.getenv();
          Object vcap = env.get("VCAP_SERVICES");       

          if (vcap == null) {
           System.out.println("No VCAP_SERVICES found");
          }    
          else {
            try {
              JSONObject obj = new JSONObject(vcap);
              String[] names = JSONObject.getNames(obj);
              if (names != null) {
                for (String name : names) {
                  if (name.startsWith("DataCache")) {
                   JSONArray val = obj.getJSONArray(name);
                   JSONObject serviceAttr = val.getJSONObject(0);
                   JSONObject credentials = serviceAttr.getJSONObject("credentials");
                  String  username = credentials.getString("username");
                 String  password = credentials.getString("password");
                String endpoint=credentials.getString("catalogEndPoint");
                String  gridName= credentials.getString("gridName");
                   System.out.println("Found configured username: " + username);
                   System.out.println("Found configured password: " + password);
                   System.out.println("Found configured endpoint: " + endpoint);                
                   System.out.println("Found configured gridname: " + gridName);    

                   break;                   
                  }             
                 }
               }
              } catch (Exception e) {
                  e.printStackTrace();
           }
         }
        }

Your parsing code is OK.

  1. In your Bluemix application dashboard, confirm you have the DataCache service bound to your application.
  2. After a new service gets bound, you need to restage the application for the environment variable to get updated. cf restage <appname>
  3. Output the environment variable to confirm DataCache credentials are in there System.out.println("VCAP_SERVICES: " + System.getenv("VCAP_SERVICES"));

You should also know that by default the the Liberty buildpack generates or updates existing server.xml file configuration stanzas for the Data Cache instance. The bound Data Cache instance can be accessed by the application using JNDI. The cache instance can either be injected into the application with an @Resource annotation, or can be looked up by the application with the javax.naming.InitialContext.

To see your server.xml on Bluemix for Liberty application:

cf files myLibertyApplication app/wlp/usr/servers/defaultServer/server.xml

You should see something like:

<xsBindings>
  <xsGrid jndiName="wxs/myCache" 
  id="myCache"
  gridName="${cloud.services.myCache.connection.gridName}" 
  userName="${cloud.services.myCache.connection.username}"
  password="${cloud.services.myCache.connection.password}" 
  clientDomain="${cloud.services.myCache.name}"/>
</xsBindings>

where your JNDI name is wxs/myCache. This avoids the need for parsing VCAP_SERVICES.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM