Skip to main content

dawdler-client

Module Introduction​

Client-side core code: filters, service discovery, connection pool, dynamic proxy, AOP implementation, load balancing, etc.

1. Adding the dependency in pom​

<groupId>club.dawdler</groupId>
<artifactId>dawdler-client</artifactId>

2. client-conf.xml configuration file reference​

Example:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<server-channel-group channel-group-id="user"
connection-num="2"
session-num="4" serializer="2"
user="jackson.song" password="srchen">
<!-- channel-group-id The identifier id, generally used with @Service(identifier id); on the server side it is the project name deployed under dawdler's deploys directory.
connection-num Number of connections
session-num Number of sessions
serializer Serialization type (1 = JDK default, 2 = Kryo; extensible)
user Account
password Password
-->
</server-channel-group>
</config>

client-conf.xml supports multi-environment configuration. See Unified Configuration Center and Multi-Environment Support.

3. Calling via the API​

public static void main(String[] args) throws Exception {
RemoteCall call = RemoteCallProvider.getCall("simple-service");//simple-service is the service name
call.setServiceName("club.dawdler.demo.service.HelloService");//Fully qualified interface name
call.setMethod("say");//Method name
call.addString("jackson");//Parameter of type String with a value; parameter methods include addString/addObject, see RemoteCall for details
Object obj = call.executeResult();//Execute

System.out.println(obj);

ConnectionPool.shutdown();
}

4. Calling via interface proxy​

@Service("simple-service")
public interface HelloService {

public String say(String text);

public List<Message> responseList(Map<String, Object> data);

}
HelloService hs = ServiceFactory.getService(HelloService.class);
String response = hs.say("jackson");

5. Client-side filter DawdlerClientFilter​

Implement the DawdlerClientFilter interface and register it via SPI; see CircuitBreakerFilter in the dawdler-circuit-breaker module for reference.

The invocation order is: pre-connection filters → acquire connection/load balance/wait for a session → post-connection filters → send request. beforeConnection() defaults to false, so existing filters still run after the session is ready and can read the request sequence number and the local/remote addresses; filters that do not depend on a session, such as a circuit breaker, can return true to short-circuit before the connection is acquired or to wrap the whole invocation. A pre-connection filter may only use invocation metadata (service, method, parameters, attachments, etc.) before calling downstream and must not depend on the session address or an allocated sequence number.

The phase takes precedence over @Order, and within a phase filters run in ascending @Order. Therefore an ordinary filter runs after the pre-connection circuit-breaker check even if its Order is smaller. RemoteCallProvider.getCall only constructs the call; channel-group validation and connection acquisition are deferred until execution. pureExecuteResult still bypasses all filters.

6. Client-side load balancing SPI extension​

The load balancing SPI has been moved down to the dawdler-discovery-center-core module and is shared with the gateway.

Random load balancing and round-robin load balancing are currently provided.

Extend the AbstractLoadBalance base class; see RoundRobinLoadBalance. The name passed to the constructor corresponds to the loadBalance attribute of the Service annotation (roundRobin by default). Extend via SPI by declaring provides club.dawdler.core.discoverycenter.loadbalance.LoadBalance with <your implementation> in your module's module-info.

7. Asynchronous calls​

dawdler provides a way to make asynchronous RPC calls.

7.1 API calls​

public static void main(String[] args) throws Exception {
RemoteCall call = RemoteCallProvider.getCall("user");
call.setServiceName("club.dawdler.demo.service.HelloService");//Fully qualified interface name
call.setMethod("say");//Method name
call.addString("jackson");//Parameter of type String with a value; parameter methods include addString/addObject, see RemoteCall for details
call.setAsync(true);//Set to asynchronous execution
Object obj = call.executeResult();//An asynchronous execution yields no result here; returns null

obj = AsyncInvokeFutureHolder.getContext().getInvokeFuture().getResult();//Obtain the asynchronous result
System.out.println(obj);

ConnectionPool.shutdown();
}

7.2 Interface proxy calls​

@Service("simple-service")
public interface HelloService {

@RemoteServiceAssistant(async = true)//Declare asynchronous
public String say(String text);

public List<Message> responseList(Map<String, Object> data);
}
HelloService hs = ServiceFactory.getService(HelloService.class);
hs.say("jackson");
InvokeFuture<String> future = AsyncInvokeFutureHolder.getContext().getInvokeFuture();
System.out.println(future.getResult());
ConnectionPool.shutdown();

8. System properties​

PropertyDefaultDescription
dawdler.client.await.available.timeout5Maximum number of seconds to wait when no session is available during a call (e.g. all disconnected and reconnecting). Polls every 500 ms and wakes up when the connection changes; on timeout a DawdlerOperateException is thrown. Set to 0 to fail immediately without waiting
dawdler.client.metrics.enabledfalseEnables client call metrics and JMX monitoring
dawdler.client.metrics.latency.window1024Window length of the latency ring histogram, i.e. the last N calls included in statistics; the value is normalized to a power of two in [16, 1048576]. There is one window globally and one per service (gid)
dawdler.client.virtualThread.enabledfalseMakes the client AIO channel group use virtual threads to run completion callbacks (same semantics as virtualThread in the server's server-conf.xml). Must be set before the client first connects; once set, the dawdler-Client-connector platform thread pool is no longer created
-Ddawdler.client.await.available.timeout=10
-Ddawdler.client.metrics.enabled=true
-Ddawdler.client.virtualThread.enabled=true

Enabling dawdler.client.metrics.enabled registers MBeans and instruments the statistics; when disabled, all instrumentation methods are no-ops (ENABLED is static final, so JIT constant folding eliminates the hot-path cost) and no MBeans are registered. MBeans are registered under the club.dawdler.client domain, lazily when the first channel group registers:

  • ClientStatus: client call metrics (total requests / success / failure / timeout / circuit-broken, average/P50/P95/P99 processing latency in the most recent window, cumulative latency sample count, uptime, number of channel groups, total sessions, number of disconnected addresses, a per-service count summary)
  • JvmMonitor: reuses dawdler-core's JVM runtime metrics

Latency statistics use a ring histogram: the window rolls with calls and only reflects recent traffic (use total requests for cumulative volume); a percentile is the lower bound of its power-of-two bucket, and the true value lies within [percentile, 2 × percentile) (relative error under 0.1% at the 1 ms scale). There is one independent window globally and one per service (gid).