org.springframework.cloud.gateway.event.RefreshRoutesEvent Java Examples

The following examples show how to use org.springframework.cloud.gateway.event.RefreshRoutesEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NacosRouteDefinitionRepository.java    From microservices-platform with Apache License 2.0 7 votes vote down vote up
/**
 * 添加Nacos监听
 */
private void addListener() {
    try {
        nacosConfigProperties.configServiceInstance().addListener(SCG_DATA_ID, SCG_GROUP_ID, new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String configInfo) {
                publisher.publishEvent(new RefreshRoutesEvent(this));
            }
        });
    } catch (NacosException e) {
        log.error("nacos-addListener-error", e);
    }
}
 
Example #2
Source File: CachingRouteDefinitionLocatorTests.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void cacheIsRefreshedInTheBackgroundOnEvent() {
	RouteDefinition routeDef1 = routeDef(1);
	RouteDefinition routeDef2 = routeDef(2);

	CachingRouteDefinitionLocator locator = new CachingRouteDefinitionLocator(
			new StubRouteDefinitionLocator(Flux.just(routeDef1),
					Flux.defer(() -> Flux.just(routeDef1, routeDef2))));

	List<RouteDefinition> routes = locator.getRouteDefinitions().collectList()
			.block();
	assertThat(routes).containsExactlyInAnyOrder(routeDef1);

	locator.onApplicationEvent(new RefreshRoutesEvent(this));

	List<RouteDefinition> updatedRoutes = locator.getRouteDefinitions().collectList()
			.block();
	assertThat(updatedRoutes).containsExactlyInAnyOrder(routeDef1, routeDef2);
}
 
Example #3
Source File: JdbcRouteDefinitionLocator.java    From open-cloud with MIT License 5 votes vote down vote up
/**
 * 刷新路由
 *
 * @return
 */
public Mono<Void> refresh() {
    this.loadRoutes();
    // 触发默认路由刷新事件,刷新缓存路由
    this.publisher.publishEvent(new RefreshRoutesEvent(this));
    return Mono.empty();
}
 
Example #4
Source File: RouteRefreshListenerTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Test
public void onParentHeartbeatEvent() {
	ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
	RouteRefreshListener listener = new RouteRefreshListener(publisher);

	listener.onApplicationEvent(new ParentHeartbeatEvent(this, 1L));
	listener.onApplicationEvent(new ParentHeartbeatEvent(this, 1L));
	listener.onApplicationEvent(new ParentHeartbeatEvent(this, 2L));

	verify(publisher, times(2)).publishEvent(any(RefreshRoutesEvent.class));
}
 
Example #5
Source File: RouteRefreshListenerTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Test
public void onHeartbeatEvent() {
	ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
	RouteRefreshListener listener = new RouteRefreshListener(publisher);

	listener.onApplicationEvent(new HeartbeatEvent(this, 1L));
	listener.onApplicationEvent(new HeartbeatEvent(this, 1L));
	listener.onApplicationEvent(new HeartbeatEvent(this, 2L));

	verify(publisher, times(2)).publishEvent(any(RefreshRoutesEvent.class));
}
 
Example #6
Source File: WeightCalculatorWebFilter.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
	// from config file
	return PredicateArgsEvent.class.isAssignableFrom(eventType) ||
	// from java dsl
			WeightDefinedEvent.class.isAssignableFrom(eventType) ||
			// force initialization
			RefreshRoutesEvent.class.isAssignableFrom(eventType);
}
 
Example #7
Source File: WeightCalculatorWebFilter.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event instanceof PredicateArgsEvent) {
		handle((PredicateArgsEvent) event);
	}
	else if (event instanceof WeightDefinedEvent) {
		addWeightConfig(((WeightDefinedEvent) event).getWeightConfig());
	}
	else if (event instanceof RefreshRoutesEvent && routeLocator != null) {
		// forces initialization
		routeLocator.ifAvailable(locator -> locator.getRoutes().subscribe());
	}

}
 
Example #8
Source File: CachingRouteLocator.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(RefreshRoutesEvent event) {
	try {
		fetch().collect(Collectors.toList()).subscribe(list -> Flux.fromIterable(list)
				.materialize().collect(Collectors.toList()).subscribe(signals -> {
					applicationEventPublisher
							.publishEvent(new RefreshRoutesResultEvent(this));
					cache.put(CACHE_KEY, signals);
				}, throwable -> handleRefreshError(throwable)));
	}
	catch (Throwable e) {
		handleRefreshError(e);
	}
}
 
Example #9
Source File: RouteRefreshListenerTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Test
public void onInstanceRegisteredEvent() {
	ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
	RouteRefreshListener listener = new RouteRefreshListener(publisher);

	listener.onApplicationEvent(new InstanceRegisteredEvent<>(this, new Object()));

	verify(publisher).publishEvent(any(RefreshRoutesEvent.class));
}
 
Example #10
Source File: CachingRouteDefinitionLocatorTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Test
public void cacheIsNotClearedOnEvent() {
	RouteDefinition routeDef1 = routeDef(1);
	RouteDefinition routeDef2 = routeDef(2);

	CountDownLatch latch = new CountDownLatch(1);
	CachingRouteDefinitionLocator locator = new CachingRouteDefinitionLocator(
			new StubRouteDefinitionLocator(Flux.just(routeDef1), Flux.defer(() -> {
				try {
					latch.await();
				}
				catch (InterruptedException e) {
					Thread.currentThread().interrupt();
					throw new IllegalStateException(e);
				}
				return Flux.just(routeDef1, routeDef2);
			}).subscribeOn(Schedulers.single())));

	List<RouteDefinition> routes = locator.getRouteDefinitions().collectList()
			.block();
	assertThat(routes).containsExactlyInAnyOrder(routeDef1);

	locator.onApplicationEvent(new RefreshRoutesEvent(this));

	routes = locator.getRouteDefinitions().collectList().block();
	assertThat(routes).containsExactlyInAnyOrder(routeDef1);

	latch.countDown();
}
 
Example #11
Source File: CachingRouteLocatorTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
private void waitUntilRefreshFinished(CachingRouteLocator locator,
		List<RefreshRoutesResultEvent> resultEvents) throws InterruptedException {
	CountDownLatch cdl = new CountDownLatch(1);
	locator.setApplicationEventPublisher(o -> {
		resultEvents.add((RefreshRoutesResultEvent) o);
		cdl.countDown();
	});
	locator.onApplicationEvent(new RefreshRoutesEvent(this));

	assertThat(cdl.await(5, TimeUnit.SECONDS)).isTrue();
}
 
Example #12
Source File: RouteRefreshListener.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
private void reset() {
	this.publisher.publishEvent(new RefreshRoutesEvent(this));
}
 
Example #13
Source File: RouteRefresher.java    From syncope with Apache License 2.0 4 votes vote down vote up
public void refresh() {
    publisher.publishEvent(new RefreshRoutesEvent(this));
}
 
Example #14
Source File: SyncopeSRAWebExceptionHandler.java    From syncope with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(final RefreshRoutesEvent event) {
    CACHE.clear();
}
 
Example #15
Source File: OidcClientInitiatedServerLogoutSuccessHandler.java    From syncope with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(final RefreshRoutesEvent event) {
    CACHE.clear();
}
 
Example #16
Source File: AbstractRouteMatcher.java    From syncope with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(final RefreshRoutesEvent event) {
    Optional.ofNullable(CACHE.get(getCacheName())).ifPresent(Map::clear);
}
 
Example #17
Source File: CachingRouteDefinitionLocator.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(RefreshRoutesEvent event) {
	fetch().materialize().collect(Collectors.toList())
			.doOnNext(routes -> cache.put(CACHE_KEY, routes)).subscribe();
}
 
Example #18
Source File: AbstractGatewayControllerEndpoint.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@PostMapping("/refresh")
public Mono<Void> refresh() {
	this.publisher.publishEvent(new RefreshRoutesEvent(this));
	return Mono.empty();
}
 
Example #19
Source File: GatewayPropertiesRefresher.java    From apollo-use-cases with Apache License 2.0 4 votes vote down vote up
private void refreshGatewayRouteDefinition() {
    logger.info("Refreshing Gateway RouteDefinition!");
    this.publisher.publishEvent(new RefreshRoutesEvent(this));
    logger.info("Gateway RouteDefinition refreshed!");
}
 
Example #20
Source File: DynamicRouteServiceImpl.java    From spring-cloud-gateway-nacos with Apache License 2.0 4 votes vote down vote up
/**
 * 增加路由
 * @param definition
 * @return
 */
public String add(RouteDefinition definition) {
    routeDefinitionWriter.save(Mono.just(definition)).subscribe();
    this.publisher.publishEvent(new RefreshRoutesEvent(this));
    return "success";
}
 
Example #21
Source File: DynamicRouteConfigurationService.java    From wecube-platform with Apache License 2.0 4 votes vote down vote up
private void notifyChanged() {
    this.publisher.publishEvent(new RefreshRoutesEvent(this));
}
 
Example #22
Source File: DynamicRouteService.java    From momo-cloud-permission with Apache License 2.0 4 votes vote down vote up
private void notifyChanged() {
    this.publisher.publishEvent(new RefreshRoutesEvent(this));
}
 
Example #23
Source File: DynamicRouteService.java    From spring-microservice-exam with MIT License 2 votes vote down vote up
/**
 * 增加路由
 *
 * @param definition definition
 * @return String
 */
public String add(RouteDefinition definition) {
    routeDefinitionWriter.save(Mono.just(definition)).subscribe();
    this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this));
    return "success";
}