我可以将 drools .drl 文件存储在 bitbucket 存储库中并在运行时使用 spring boot 访问它们吗

Can I store drools .drl files in bitbucket repository and access them during runtime using spring boot

我想将 drools .drl 文件存储在 bitbucket 中(而不是将它们保存在应用程序类路径中)并在使用 spring 引导期间访问它们。这样如果规则有任何变化,我不需要重新打包应用程序并重新部署。

默认情况下,Bean 是单例的,因此它们只创建一次,仅此而已。我个人没有使用过 Drools,但是发出 HTTP 请求只是为了获取文件可能会 costly/slow.

所以我的建议是尝试以某种方式利用 Spring Cloud Config 并将规则存储在 application.yml

例如,您可以在 Git Spring config repo 中定义以下内容:

drools:
  myRule: >
    package com.baeldung.drools.rules;

    import com.baeldung.drools.model.Applicant;

    global com.baeldung.drools.model.SuggestedRole suggestedRole;

    dialect  "mvel"

    rule "Suggest Manager Role"
        when
            Applicant(experienceInYears > 10)
            Applicant(currentSalary > 1000000 && currentSalary <= 
            2500000)
        then
            suggestedRole.setRole("Manager");
    end

然后您将定义一个 ConfigurationProperties bean,例如:

@Configuration
@ConfigurationProperties("drools")
public class ConfigProperties {
    private String myRule;

    // getters/setters ...
}

由于我们要使用 Spring Cloud Config,您需要添加 @RefreshScope

@Configuration
@ConfigurationProperties("drools")
@RefreshScope
public class ConfigProperties {
    private String myRule;

    // getters/setters ...
}

因此,现在无论何时您在配置存储库中进行更改,它都应该反映在您的应用程序中,而无需使用更新的文件重新部署。

我不知道你是如何构建你的 Drool bean 的,但我的假设是你可能会传入一个 InputStream 如此简单地将 String 转换为 InputStream:

@Configuration
public class MyDroolConfig {
    private final ConfigProperties properties;

    ConfigProperties(ConfigProperties properties) {
        this.properties = properties;
    }

    @Bean
    public MyRuleObject myRuleObject() {
        try (InputStream in = new ByteArrayInputStream(properties.getMyRule().getBytes())) {
            // do something with the rule
        }
    }
}