[英]How to edit application.properties during runtime (for next time use)
I want to be able to check if a script exists in a Redis cluster. 我希望能够检查Redis集群中是否存在脚本。 If it doesn't, I will need to load a new script from my resources folder
and save the corresponding SHA value of that new script. 如果没有,则需要从resources folder
加载新脚本并保存该新脚本的相应SHA值。 I would like to save that SHA value for next time the application starts up, inside of the application.properties
. 我想保存下一次SHA值的应用程序启动时,内部application.properties
。 This would ideally be done by overwriting the previous entry for the sha value 理想情况下,这将通过覆盖sha值的先前条目来完成
I know that the properties file is read once during startup, but this does not matter as I only want to save that SHA value to the application.properties
for use next time, ie preventing the overhead of checking for a script and loading each time. 我知道属性文件在启动过程中会被读取一次,但这无关紧要,因为我只想将该SHA值保存到application.properties
,以备下次使用,即避免了每次检查脚本和加载的开销。
This is my method for preparing the scripts 这是我准备脚本的方法
static String prepareScripts() throws ExecutionException, InterruptedException, IOException {
List <Boolean> list = (List) asyncCommands.scriptExists(sha).get();
shaDigest = sha;
if (list.get(0) == false) {
URL url = AbstractRedisDao.class.getClassLoader().getResource("script.txt");
File file = new File(url.getPath());
String str = FileUtils.readFileToString(file, "ISO_8859_1");
shaDigest = (String) asyncCommands.scriptLoad(str).get();
Properties properties = new Properties();
try {
FileWriter writer = new FileWriter("application.properties");
BufferedWriter bw = new BufferedWriter(writer);
Iterator propertyIt = properties.entrySet().iterator();
while (propertyIt.hasNext() ) {
Map.Entry nextHolder = (Map.Entry) propertyIt.next();
while (nextHolder.getKey() != ("redis.scriptActiveDev")) {
bw.write(nextHolder.getKey() + "=" + nextHolder.getValue());
}
}
bw.write("redis.scriptActiveDev=" + shaDigest);
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
return shaDigest;
} else {
return shaDigest;
}
}
these are the details for redis in application.properties: 这些是application.properties中redis的详细信息:
redis.enabled=true
redis.hostname=xxxx
redis.port=xxxx
redis.password=xxxx
redis.sha=xxxx
Is this on the right track? 这是在正确的轨道上吗? also, how would i go about saving the application.properties
back to the resources
folder after rebuilding it with the new property? 另外,用新属性重建application.properties
后,我该如何将其保存回resources
文件夹? Is there a more efficient way to do this without recreating the whole application.properties
just to add one line? 是否有一种更有效的方法来执行此操作,而无需重新创建整个application.properties
仅添加一行?
There is no need to store SHA digests for Lua scripts in application.properties
. 无需在application.properties
存储Lua脚本的SHA摘要。
Use the API of your Redis client to get SHA digest on application startup. 使用Redis客户端的API在应用程序启动时获取SHA摘要。
For instance, Lettuce provides the following API for scripting: 例如,Lettuce提供以下用于脚本编写的API :
String digest(V script)
String scriptLoad(V script)
List<Boolean> scriptExists(String... digests)
You can execute the following code on each application startup to get the digest for your script: 您可以在每次应用程序启动时执行以下代码,以获取脚本的摘要:
public String sha(String script) {
String shaDigest = redisScriptingCommands.digest(script);
boolean scriptExists = redisScriptingCommands.scriptExists(shaDigest).get(0);
if (!scriptExists) {
redisScriptingCommands.scriptLoad(script);
}
return shaDigest;
}
You can externalize your configuration in a folder outside the classpath. 您可以在类路径之外的文件夹中外部化配置。
java -jar myproject.jar --spring.config.location=/var/config
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment: SpringApplication在以下位置从application.properties文件加载属性,并将它们添加到Spring Environment:
- A config subdirectory of the current directory 当前目录的config子目录
- The current directory 当前目录
- A classpath /config package 类路径/ config包
- The classpath root 类路径根
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations). 该列表按优先级排序(在列表较高位置定义的属性会覆盖在较低位置定义的属性)。
If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. 如果您不喜欢application.properties作为配置文件名,则可以通过指定spring.config.name环境属性来切换到另一个文件名。 You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths). 您还可以通过使用spring.config.location环境属性(这是目录位置或文件路径的逗号分隔列表)来引用显式位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.