gpt4 book ai didi

spring-boot - 不重启网关动态删除zuul路由

转载 作者:行者123 更新时间:2023-12-04 12:38:54 24 4
gpt4 key购买 nike

嗨,我想删除 zuul动态创建的路由。我没有使用云服务器。我可以使用 discoveryclientroutelocator 添加路由.

但是我没有找到取消注册动态添加的路由的选项。此删除应该在不重新启动网关的情况下发生。帮助。

    ZuulRoute zuulRoute = new ZuulRoute();
zuulRoute.setId(externalapis.getServiceId());
zuulRoute.setServiceId(externalapis.getServiceId());
zuulRoute.setPath(externalapis.getPath());
zuulRoute.setUrl(externalapis.getUrl());
zuulRoute.setRetryable(true);
discoveryClientRouteLocator.addRoute(zuulRoute);

最佳答案

您可以扩展 DiscoveryClientRouteLocator并添加 removeRoute()方法:
这是我的例子

@SpringBootApplication
@EnableZuulProxy
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}


public static class DeregistrableDiscoveryClientRouteLocator extends DiscoveryClientRouteLocator {
private final ZuulProperties properties;
public DeregistrableDiscoveryClientRouteLocator(String servletPath, DiscoveryClient discovery, ZuulProperties properties, ServiceInstance localServiceInstance) {
super(servletPath, discovery, properties, localServiceInstance);
this.properties = properties;
}
public DeregistrableDiscoveryClientRouteLocator(String servletPath, DiscoveryClient discovery, ZuulProperties properties, ServiceRouteMapper serviceRouteMapper, ServiceInstance localServiceInstance) {
this(servletPath, discovery, properties, localServiceInstance);
}
//here is new method to remove route from .properties.getRoutes()
public void removeRoute(String path) {
this.properties.getRoutes().remove(path);
refresh();
}
}

@Bean
DiscoveryClientRouteLocator discoveryClientRouteLocator(ServerProperties server, ZuulProperties zuulProperties, DiscoveryClient discovery, ServiceRouteMapper serviceRouteMapper, @Autowired(required = false) Registration registration) {
return new DeregistrableDiscoveryClientRouteLocator(server.getServlet().getContextPath(),
discovery, zuulProperties, serviceRouteMapper,
registration);
}

@Component
public static class AppRunner implements ApplicationRunner {
@Autowired
DeregistrableDiscoveryClientRouteLocator discoveryClientRouteLocator;

@Override
public void run(ApplicationArguments args) throws Exception {

ZuulProperties.ZuulRoute zuulRoute = new ZuulProperties.ZuulRoute();
zuulRoute.setId("google");
zuulRoute.setServiceId("google");
zuulRoute.setPath("/");
zuulRoute.setUrl("http://google.com");
zuulRoute.setRetryable(true);
discoveryClientRouteLocator.addRoute(zuulRoute);

//now remove the pre-added route.
discoveryClientRouteLocator.removeRoute(zuulRoute.getPath());
}
}
}

因此,之后您可以创建一个休息端点,该端点将在不重新启动服务器的情况下删除路由。

关于spring-boot - 不重启网关动态删除zuul路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55119486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com