如何在运行时编辑application.properties(供下次使用)

How to edit application.properties during runtime (for next time use)

我希望能够检查脚本是否存在于 Redis 集群中。如果没有,我将需要从我的 resources folder 加载一个新脚本并保存该新脚本的相应 SHA 值。我想在应用程序下次启动时保存该 SHA 值,在 application.properties 内。理想情况下,这将通过覆盖 sha 值

的先前条目来完成

我知道属性文件在启动期间被读取一次,但这并不重要,因为我只想将该 SHA 值保存到 application.properties 以供下次使用,即防止检查的开销一个脚本,每次加载。

这是我编写脚本的方法

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;
    }
}

这些是 application.properties 中 redis 的详细信息:

redis.enabled=true
redis.hostname=xxxx
redis.port=xxxx
redis.password=xxxx
redis.sha=xxxx

这是在正确的轨道上吗?另外,在用新的 属性 重建后,我如何将 application.properties 保存回 resources 文件夹?有没有更有效的方法来做到这一点而无需重新创建整个 application.properties 只是添加一行?

您可以在类路径之外的文件夹中外部化您的配置。

 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:

  1. A config subdirectory of the current directory
  2. The current directory
  3. A classpath /config package
  4. 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. 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).

Externalized Configuration

application.properties 中的 Lua 个脚本无需存储 SHA 摘要。

使用 Redis 客户端的 API 在应用程序启动时获取 SHA 摘要。

例如,Lettuce 为脚本提供了以下 API
String digest(V script)
String scriptLoad(V script)
List<Boolean> scriptExists(String... digests)

您可以在每次应用程序启动时执行以下代码以获取脚本的摘要:

public String sha(String script) {
  String shaDigest = redisScriptingCommands.digest(script);
  boolean scriptExists = redisScriptingCommands.scriptExists(shaDigest).get(0);
  if (!scriptExists) {
    redisScriptingCommands.scriptLoad(script);
  }
  return shaDigest;
}