您好,我想删除已动态创建的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);
答案 0 :(得分:3)
您可以扩展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());
}
}
}
因此,在此之后,您可以创建一个rest端点,该端点将删除路由而无需重新启动服务器。
答案 1 :(得分:0)
您可以使用@RefreshScope注释来刷新属性:
1 .- 在课程上添加@RefreshScope
@RefreshScope
@Component
public class ApplicationTest {
@Autowired
DiscoveryClientRouteLocator discoveryClientRouteLocator;
@Value("${custom.property.id}")
private String id;
@Value("${custom.property.serviceId}")
private String serviceId;
@Value("${custom.property.path}")
private String path;
@Value("${custom.property.url}")
private String url;
@Value("${custom.property.retryable}")
private boolean retryable;
public void buildNewRoute(){
ZuulRoute zuulRoute = new ZuulRoute();
zuulRoute.setId(id);
zuulRoute.setServiceId(serviceId);
zuulRoute.setPath(path);
zuulRoute.setUrl(url);
zuulRoute.setRetryable(retryable);
discoveryClientRouteLocator.addRoute(zuulRoute);
}
}
2 .- 。添加标志属性并允许公开端点/refresh
,以刷新新属性。
application.properties
custom.property.id=1
custom.property.serviceId=service-id-01
custom.property.path=/this/path
custom.property.url=http://localhost:7070
custom.property.retryable=true
management.endpoints.web.exposure.include=*
3 .- 例如,一旦修改了application.properties:
custom.property.id=3
custom.property.serviceId=service-id-03
custom.property.path=/this/path/new3
custom.property.url=http://localhost:9999
custom.property.retryable=false
然后您可以refresh
进行配置注入:
curl localhost:8080/actuator/refresh -d {} -H "Content-Type: application/json"
答案 2 :(得分:0)
我使用以下代码添加,删除和更新。它无需重启网关即可工作
添加路线:
<nav>
删除路线:
this.zuulProperties.getRoutes().put(externalapis.getServiceId(), zuulRoute);
更新路线:
this.zuulProperties.getRoutes().remove(externalapis.getServiceId());