On Enterprise Distributed Application Service (EDAS), you can define custom environment variables on the application server and access them at runtime from your application code.
Prerequisites
Before you begin, make sure that you have:
An EDAS application deployed on an Elastic Compute Service (ECS) instance
SSH access to the application server
The
adminaccount credentials
Procedure
Log on to the application server and switch to the
adminaccount:su - adminOpen the
~/.bash_profilefile and add your custom environment variables. For example: Save the file and exit the editor.export API_ENDPOINT="https://api.example.com" export DB_HOST="rm-xxxxxxx.mysql.rds.aliyuncs.com"Load the environment variables into the current session:
source ~/.bash_profileAccess the environment variables from your application code. Call methods such as
System.getenv()to obtain the custom environment variables. For example:String endpoint = System.getenv("API_ENDPOINT");
After you modify the application code, restart the application for the changes to take effect.
Verify the environment variable
Run the following command on the server to confirm that the environment variable is set:
echo $API_ENDPOINTThe command returns the value you defined in ~/.bash_profile.
Notes
Variables defined in
~/.bash_profileare loaded each time theadminuser starts a login shell.Before you modify configurations or data on an ECS instance or an ApsaraDB RDS instance, create snapshots or enable backup features such as log backup for ApsaraDB RDS.
If you submitted sensitive information such as passwords in the Alibaba Cloud Management Console, update the credentials promptly.