不重启网关动态删除zuul路由
Delete zuul route dynamically without restarting the gateway
您好,我想删除动态创建的 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());
}
}
}
因此之后您可以创建一个 rest 端点,它将在不重新启动服务器的情况下删除路由。
您可以使用 @RefreshScope 注释来刷新属性:
1.-在class
上添加@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"
参考文献
- https://spring.io/guides/gs/centralized-configuration/
我使用下面的代码添加、删除和 update.It 无需重新启动网关就可以工作
添加路线:
this.zuulProperties.getRoutes().put(externalapis.getServiceId(), zuulRoute);
删除路线:
this.zuulProperties.getRoutes().remove(externalapis.getServiceId());
更新路线:
this.zuulProperties.getRoutes().remove(oldExternalapis.getServiceId());
this.zuulProperties.getRoutes().put(newExternalapis.getServiceId(), zuulRoute);
您好,我想删除动态创建的 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());
}
}
}
因此之后您可以创建一个 rest 端点,它将在不重新启动服务器的情况下删除路由。
您可以使用 @RefreshScope 注释来刷新属性:
1.-在class
上添加@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"
参考文献 - https://spring.io/guides/gs/centralized-configuration/
我使用下面的代码添加、删除和 update.It 无需重新启动网关就可以工作
添加路线:
this.zuulProperties.getRoutes().put(externalapis.getServiceId(), zuulRoute);
删除路线:
this.zuulProperties.getRoutes().remove(externalapis.getServiceId());
更新路线:
this.zuulProperties.getRoutes().remove(oldExternalapis.getServiceId());
this.zuulProperties.getRoutes().put(newExternalapis.getServiceId(), zuulRoute);