如何在Cloud Foundry中为Java应用添加多个目标地?
Hi Sankeerth, great question! Adding a second destination to your app just requires updating the existing destinations environment variable with an additional entry in the JSON array. Here are two reliable ways to do this:
Method 1: Update the Environment Variable Directly
Since cf set-env overwrites the existing value, you need to include both your original destination and the new one in the command. Use a valid JSON array (note the comma separating the two destination objects):
cf set-env firstapp destinations '[ { "name": "ErpQueryEndpoint", "url": "https://URL", "username": "USER", "password": "PASSWORD" }, { "name": "SecondDestination", "url": "https://SECOND_DESTINATION_URL", "username": "SECOND_USER", "password": "SECOND_PASSWORD" } ]'
Pro tip: While you can write the JSON on a single line, formatting it with line breaks makes it easier to read and avoid syntax errors.
Method 2: Safely Modify Existing Configuration
If you want to avoid retyping your original destination details, follow these steps:
- First, retrieve your current
destinationsconfiguration:
Look for thecf env firstappdestinationsentry underUser-Provided Environment Variablesand copy the entire JSON array. - Add your second destination object to the array (make sure to add a comma after the first object):
[ { "name": "ErpQueryEndpoint", "url": "https://URL", "username": "USER", "password": "PASSWORD" }, { "name": "SecondDestination", "url": "https://SECOND_DESTINATION_URL", "username": "SECOND_USER", "password": "SECOND_PASSWORD" } ] - Update the environment variable with the modified JSON:
cf set-env firstapp destinations 'PASTE_YOUR_MODIFIED_JSON_HERE'
Final Step: Apply the Changes
After updating the environment variable, you need to restage your app for the changes to take effect:
cf restage firstapp
Restaging ensures the app picks up the new environment variables during the build process, which is more reliable than a simple restart for Java apps.
内容的提问来源于stack exchange,提问作者Sankeerth

