Spring Cloud Zuul 和 Groovy 过滤器

Spring Cloud Zuul and Groovy Filters

我有一个基于 Spring Cloud Netflix 包的 Zuul 网关 运行。我还创建了一个基于 Zuul "groovy" 的过滤器,如 blog here 所示。当我添加 groovy 文件时,由于 IDE / 编译器抱怨 class 路径上没有 groovy 库,我还必须将下面的 jar 添加到我的pom.xml 允许将 groovy 个文件编译到项目中 -

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.3.10</version>
</dependency>

从这一点开始,我的 groovy 过滤器按预期加载,并针对每个传入的 Zuul 请求进行评估。到目前为止一切都很好,除了当我尝试修改 groovy 文件时,除非我重新启动服务器,否则我看不到对我的文件的更改是动态获取的。

我对 Groovy 很陌生。我应该怎么做才能在 Netflix Zuul 及其过滤器的上下文中利用 Groovy 过滤器的动态特性。来自 Netflix 的痛苦 Zuul 库扫描特定目录以查找 Groovy 过滤器。 Spring Netflix Zuul 的云包装器有类似的东西吗?

请注意,我已阅读 Spring 4 支持 groovy 等动态语言,但使用它需要使用 lang:groovy 等配置 XML 文件

但是,就我而言 - 使用 Spring Cloud Netflix 库,没有 groovy classes 的 XML 配置文件,我能够获得 Spring 云/Zuul 库拿起我的 groovy class。我无法动态刷新它,想知道是否有办法做到这一点。

我确实通读了 spring-cloud 文档,但我没有看到任何关于加载 groovy 过滤器的信息,除了一条语句 - Zuul’s rule engine allows rules and filters to be written in essentially any JVM language, with built in support for Java and Groovy.

请指教

所以,我通过查看 github netflix zuul wiki 找到了答案。

对于遇到此问题并正在寻找简单答案的任何其他人,请开始,我为您节省了一些时间:)

第 1 步 - 添加 groovy-all jar 到类路径

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.3.10</version>
</dependency>

第 2 步 - 将文件放置在 src/main/resources 路径中,文件夹结构为

    groovy
    └── pre
    └── route
    └── post

第 3 步 - 在我的 application.yml -

中配置 Groovy 过滤器路径
zuul:
  groovyFiltersPath:
    - groovy/pre
    - groovy/route
    - groovy/post

第 4 步 - 在 Zuul Spring 启动应用程序中注入 yaml 配置

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "zuul")
public class CustomPathZuulFilterConfig {

    private List<String> groovyFiltersPath;

    public List<String> getGroovyFiltersPath() {
        return groovyFiltersPath;
    }

    public void setGroovyFiltersPath(List<String> groovyFiltersPath) {
        this.groovyFiltersPath = groovyFiltersPath;
    }
}

第 5 步 - 最后,初始化 Groovy 过滤器加载器和文件管理器

@Component
public class GroovyFiltersInitializer {

    private Logger logger = LoggerFactory.getLogger(GroovyFiltersInitializer.class);

    @Autowired
    private CustomPathZuulFilterConfig config;

    @PostConstruct
    private void initGroovyFilters() throws Exception {

        List<String> groovyFiltersPath = config.getGroovyFiltersPath();

        if(groovyFiltersPath == null || groovyFiltersPath.size() == 0) {
            return;
        }

        FilterLoader.getInstance().setCompiler(new GroovyCompiler());
        FilterFileManager.setFilenameFilter(new GroovyFileFilter());

        String[] filterDirectoryList = groovyFiltersPath.toArray(new String[0]);
        FilterFileManager.init(5, filterDirectoryList);

        logger.info("Groovy Filter file manager started");
    }
}

现在,对 groovy 过滤器的任何更改都应该动态获取。

特定于 IntelliJ 用户 - 要检查是否提取了对 groovy 文件的更改,您需要更改调试器 "out" 目录下的文件,而不是 src/main/resources/[=47= 下的文件] 目录。这是因为,应用程序运行基于 "out" 目录中的代码。但是,要调试您的更改 - 您必须在 "out" 和 src/main/resources/groovy 目录中进行更改,因为 IntelliJ 不会将文件复制到 "out",而应用程序是 运行。希望这会有所帮助。

祝你好运!