My Gateway 连接到 Eureka Service Discovery。每次网关客户端在 Eureka 中刷新其状态时,都会重建网关中的所有路由。这对我来说很重要,因为我已经重新定义了 Route Locator 并正在从另一项服务下载我的路线。我怎样才能禁用或更改此行为?
@Profile({"ui", "internal", "rec"})
@Configuration
public class RouteConfiguration {
@Bean
@RefreshScope
public RouteLocator routeLocator(RouteLocatorBuilder routeLocatorBuilder, RouteLoader routeLoader) {
return new RouteLocatorImpl(routeLocatorBuilder, routeLoader);
}
}
server.port=30037
spring.application.name=isf-gateway-rec
spring.cloud.loadbalancer.ribbon.enabled=false
eureka.client.service-url.defaultZone= http://127.0.0.1:30035/eureka/
configuration.server.url=http://127.0.0.1:30035
logging.level.root=DEBUG
spring.cloud.gateway.discovery.locator.enabled=false
gateway.load-route.path.all=/routes
EXECUTION_ENV=LOCAL
#Kafka
spring.kafka.bootstrap-servers=127.0.0.1:9092
gateway.kafka.topic=gateway-route-refresh
PARTITION_NUMBER=1
REPLICATION_FACTOR=1
FETCH_MAX_BYTES_CONFIG=5000000
MAX_PARTITION_FETCH_BYTES_CONFIG=5000000
#15 minutes
MAX_POLL_INTERVAL_MS=300000
CONSUMER_SESSION_TIMEOUT_MS=600000
HEARTBEAT_INTERVAL_MS=60000
整个问题是org.springframework.cloud.gateway.route.RouteRefreshListener。 eureka client创建HeartbeatEvent时,会触发路由的重建,请问有什么方法可以覆盖这个Bean吗?它没有实现接口((
public class RouteRefreshListener implements ApplicationListener<ApplicationEvent> {
private final ApplicationEventPublisher publisher;
private HeartbeatMonitor monitor = new HeartbeatMonitor();
public RouteRefreshListener(ApplicationEventPublisher publisher) {
Assert.notNull(publisher, "publisher may not be null");
this.publisher = publisher;
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
ContextRefreshedEvent refreshedEvent = (ContextRefreshedEvent) event;
if (!WebServerApplicationContext.hasServerNamespace(
refreshedEvent.getApplicationContext(), "management")) {
reset();
}
}
else if (event instanceof RefreshScopeRefreshedEvent
|| event instanceof InstanceRegisteredEvent) {
reset();
}
else if (event instanceof ParentHeartbeatEvent) {
ParentHeartbeatEvent e = (ParentHeartbeatEvent) event;
resetIfNeeded(e.getValue());
}
else if (event instanceof HeartbeatEvent) {
HeartbeatEvent e = (HeartbeatEvent) event;
resetIfNeeded(e.getValue());
}
}
private void resetIfNeeded(Object value) {
if (this.monitor.update(value)) {
reset();
}
}
private void reset() {
this.publisher.publishEvent(new RefreshRoutesEvent(this));
}
@Bean
@ConditionalOnClass(
name = "org.springframework.cloud.client.discovery.event.HeartbeatMonitor")
public RouteRefreshListener routeRefreshListener(
ApplicationEventPublisher publisher) {
return new RouteRefreshListener(publisher);
}
我尝试使用该属性,但没有帮助 spring.cloud.gateway.discovery.locator.enabled=false