Spring Zuul:动态禁用通往服务的路由 [英] Spring Zuul: Dynamically disable a route to a service

查看:360
本文介绍了Spring Zuul:动态禁用通往服务的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行时禁用通向Eureka注册的微服务的Zuul路由(我正在使用Spring Boot).

I'm trying to disable a Zuul route to a microservice registered with Eureka at runtime (I'm using spring boot).

这是一个示例:

localhost/hello
localhost/world

这两个是已注册的微服务.我想在运行时禁用到其中一个的路由而不关闭它.

Those two are the registered microservices. I would like to disable the route to one of them at runtime without shutting it down.

有没有办法做到这一点?

Is there a way to do this?

谢谢

纳米

推荐答案

经过大量的努力,我想到了这个解决方案.首先,我使用Netflix Archaius观看属性文件.然后我进行如下操作:

After a lot of efforts I came up with this solution. First, I used Netflix Archaius to watch a property file. Then I proceeded as follows:

public class ApplicationRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {

public ApplicationRouteLocator(String servletPath, ZuulProperties properties) {
    super(servletPath, properties );
}


@Override
public void refresh() {
   doRefresh();
}
}

通过扩展SimpleRouteLocator并在接口RefreshableRouteLocator的重写之一中调用doRefresh()方法来使其公开.

Made the doRefresh() method public by extending SimpleRouteLocator and calling its method in the overridden one of the interface RefreshableRouteLocator.

然后,用自定义实现重新定义bean RouteLocator:

Then I redefined the bean RouteLocator with my custom implementation:

@Configuration
@EnableConfigurationProperties( { ZuulProperties.class } )
public class ZuulConfig {

public static ApplicationRouteLocator simpleRouteLocator;

@Autowired
private ZuulProperties zuulProperties;

@Autowired
private ServerProperties server;

@Bean
@Primary
public RouteLocator routeLocator() {
    logger.info( "zuulProperties are: {}", zuulProperties );
    simpleRouteLocator = new ApplicationRouteLocator( this.server.getServletPrefix(),
            this.zuulProperties );


    ConfigurationManager.getConfigInstance().addConfigurationListener( configurationListener );

    return simpleRouteLocator;
}


private ConfigurationListener configurationListener =
        new ConfigurationListener() {

            @Override
            public void configurationChanged( ConfigurationEvent ce ) {

                            // zuulProperties.getRoutes() do something
                            // zuulProperties.getIgnoredPatterns() do something
                            simpleRouteLocator.refresh();
                        }



                }

}

每次修改文件中的属性时,都会触发一个事件,并且ConfigurationEvent能够处理该事件(getPropertyName()和getPropertyValue()从事件中提取数据).由于我还自动为ZuulProperties布线,因此可以访问它.遵循正确的规则,我可以找到Zuul的财产

Every time a property in the file was modified an event was triggered and the ConfigurationEvent was able to deal with it (getPropertyName() and getPropertyValue() to extract data from the event). Since I also Autowired the ZuulProperties I was able to get access to it. With the right rule I could find whether the property of Zuul

zuul.ignoredPatterns

已被修改,相应地更改了其在ZuulProperties中的值.

was modified changing its value in the ZuulProperties accordingly.

这篇关于Spring Zuul:动态禁用通往服务的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆