spring cloud gateway 负载均衡流程

网关被扫描后会注册四个核心配置类,当然在配置类中仍然有各种流程中需要的bean后面会详细解释。

l  GatewayAutoConfiguration  网关基础配置类,当中承载着核心的配置逻辑

l  GatewayClassPathWarningAutoConfiguration  网关类加载配置类,就是用于校验是否加载的时webFlux依赖,而不是普通的web依赖。

l  GatewayLoadBalancerClientAutoConfiguration  网关客户端负载均衡配置类

l  GatewayRedisAutoConfiguration   网关限流器配置类

我们先回过头,假如你已经搭建了一个gateway的demo,那么在网关中进行负载均衡,会添加注解如下:

@RibbonClient(name = "pay-server", configuration = {IRule2.class})

@Configuration
@ConditionalOnClass({LoadBalancerClient.class, RibbonAutoConfiguration.class, DispatcherHandler.class})
@AutoConfigureAfter(RibbonAutoConfiguration.class)
public class GatewayLoadBalancerClientAutoConfiguration {

    // GlobalFilter beans

    @Bean
    @ConditionalOnBean(LoadBalancerClient.class)
    public LoadBalancerClientFilter loadBalancerClientFilter(LoadBalancerClient client) {
        return new LoadBalancerClientFilter(client);
    }

}
RibbonClientConfigurationRegistrar
private void registerClientConfiguration(BeanDefinitionRegistry registry,
            Object name, Object configuration) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder
                .genericBeanDefinition(RibbonClientSpecification.class);
        builder.addConstructorArgValue(name);
        builder.addConstructorArgValue(configuration);
        registry.registerBeanDefinition(name + ".RibbonClientSpecification",
                builder.getBeanDefinition());
    }

相关推荐