Consumer Module

From Configuration to Proxy

  • Configuration Parsing: ReferenceConfig, DubboBootstrap, read and merge configurations
  • Registry Subscription: RegistryDirectory, subscribe to service list from Nacos/ZooKeeper
  • Directory Routing: RouterChain, filter Provider list according to rules
  • Cluster Encapsulation: Cluster, ClusterInvoker, implement fault tolerance mechanism
  • Load Balancing: LoadBalance, support Random/RoundRobin/ConsistentHash/LeastActive
  • Dynamic Proxy: ProxyFactory, generate local proxy class
  • Connection Pool & Network: ExchangeClient (Netty), support Triple/Dubbo protocol

Runtime Invocation Chain

Business Code → Interface Proxy → Filter Chain → ClusterInvoker
             ↓                 ↓
          Async/Future      LoadBalance
             ↓                 ↓
       ExchangeClient → Netty → Provider

Key Extension Points (SPI)

  • Filter: ExceptionFilter, TraceFilter, SentinelFilter
  • Cluster: Failover, Failfast, Broadcast, Forking
  • LoadBalance: Random, RoundRobin, LeastActive, ConsistentHash

POM Dependency

<dependencies>
    <dependency>
        <groupId>icu.wzk</groupId>
        <artifactId>wzk-service-api</artifactId>
    </dependency>
</dependencies>

Consumer Definition

@Component
public class ConsumerComponent {
    @Reference
    private WzkHelloService wzkHelloService;

    public String sayHello(String name) {
        return wzkHelloService.sayHello(name);
    }
}

Configuration File

dubbo.application.name=service-consumer
dubbo.registry.address=zookeeper://10.10.52.38:2181

Startup Class

Producer Startup

public class DubboPureMain {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(ProviderConfiguration.class);
        context.start();
        System.in.read();
    }
}

Consumer Startup

public class AnnotationConsumerMain {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();

        ConsumerComponent service = context.getBean(ConsumerComponent.class);
        while (true) {
            try {
                String hello = service.sayHello("world!");
                System.out.println("result: " + hello);
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

High-Frequency Configuration Parameters

ParameterDescription
timeoutRPC call timeout (ms)
retriesNumber of retries on failure
checkWhether to verify Provider existence at startup
lazyCreate connection only on first call
cacheEnable local method cache
mockService degradation strategy
asyncWhether to invoke asynchronously