
package com.tmerclub.cloud.gateway.dev;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.gateway.config.GatewayLoadBalancerProperties;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author tmerclub
 * @date 2021/3/4
 */
@Component
@ConditionalOnProperty(value = "dubbo.consumer.loadbalance", havingValue = "dev")
public class DevReactiveLoadBalancerClientFilter extends ReactiveLoadBalancerClientFilter {
    public DevReactiveLoadBalancerClientFilter(LoadBalancerClientFactory clientFactory,
                                               GatewayLoadBalancerProperties properties) {
        super(clientFactory, properties);
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        try {
            DevWebFluxContext.set(exchange);
            return super.filter(exchange, chain);
        } finally {
            DevWebFluxContext.clean();

        }

    }
}
