What exactly setenv.sh is used for in Tomcat?

浪尽此生 提交于 2019-12-12 16:27:11

问题


I searched a lot but couldn't find any useful information about this:

What exactly setenv.sh is used for in Tomcat ?

Let's say we have a REST API (built with Java EE or Spring) which uses some parameters/variables like AWS Credentials, Database Credentials etc.

Does it make sense to parametrize the application with environment variables for these things I mentioned above and put their values to env vars on setenv.sh for each Tomcat instance in case we use more than one instance and with different parameters/variables ?

Or setenv.sh isn't for things like that ?

Thanks in advance


回答1:


I've only ever seen it used for specifying CATALINA_OPTS, but I noticed that the RUNNING.txt file mentions using it to set JRE_HOME and JAVA_HOME.

RUNNING.txt also states under Advanced Configuration - Multiple Tomcat Instances:

In many circumstances, it is desirable to have a single copy of a Tomcat binary distribution shared among multiple users on the same server. To make this possible, you can set the CATALINA_BASE environment variable to the directory that contains the files for your 'personal' Tomcat instance.

So I guess the answer is that a setenv file can be created for each app and can probably can be used to store credentials, but I don't think that it's commonly used for that purpose.




回答2:


~/Programs/apache-tomcat-9.0.7/bin/setenv.sh export CATALINA_OPTS="$CATALINA_OPTS -DENV_TARGET=prod -DMy_Env_Var1=Whatever -DMy_Env_Var2=CapitalOneIsTheBest"

So everything in between "" above in setenv.sh gets set as environmental variables for your applications in Tomcat. To retrieve/use those environmental variables, use this in your Java (Spring) applications:

Ex1) String myWhatever = java.lang.System.getProperties().getProperty("My_Env_Var1"); Ex2) String env= System.getProperties().getProperty("My_Env_Var2");

To change the environmental variable (for example when you are writing unit tests), do this: System.setProperty("My_Env_Var1", "newEnv");



来源:https://stackoverflow.com/questions/43218169/what-exactly-setenv-sh-is-used-for-in-tomcat

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