Implements: IConfigurable, IReferenceable, IOpenable
Description
The DirectClientclass allows you to create clients that call a controller directly in the same memory space.
Important points
- It is used when multiple microservices are deployed in a single container (monolyth) and communication between them can be done by direct calls rather than through the network.
Configuration parameters
- dependencies:
- controller: override controller descriptor
References
- *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
- *:counters:*:*:1.0 - (optional) ICounters components to pass collected measurements
- *:traces:*:*:1.0 - (optional) ITracer components to record traces
- *:discovery:*:*:1.0 - (optional) IDiscovery services to resolve a connection
Constructors
Creates a new instance of the client.
DirectClient()
Fields
Instance methods
close
Closes a component and frees used resources.
@override
Future close(String? correlationId)
- correlationId: string - (optional) transaction id used to trace execution through a call chain.
configure
Configures component by passing configuration parameters.
@override
void configure(ConfigParams config)
- config: ConfigParams - configuration parameters to be set.
instrument
Adds instrumentation to log calls and measures call time. It returns a CounterTiming object that is used to end the time measurement.
InstrumentTiming instrument(String? correlationId, String name)
- correlationId: String? - (optional) transaction id used to trace execution through a call chain.
- name: String - method name.
- returns: InstrumentTiming - InstrumentTiming object used to end the time measurement.
isOpen
Checks if the component is open.
@override
bool isOpen()
- returns: bool - true if the component is open and false otherwise.
open
Opens the component.
@override
Future open(String? correlationId)
- correlationId: string - (optional) transaction id used to trace execution through a call chain.
setReferences
Sets references to dependent components.
@override
void setReferences(IReferences references)
- references: IReferences - references to locate the component’s dependencies.
Examples
class MyDirectClient extends DirectClient<IMyController> implements IMyClient {
MyDirectClient(): super() {
dependencyResolver.put('controller', Descriptor(
"mygroup", "controller", "*", "*", "*"));
}
...
Future<MyData> getData(String? correlationId, String id) async {
var counter_timing = instrument(correlationId, 'myclient.get_data');
try {
var result = await controller.getData(correlationId, id)
counter_timing.endTiming();
return result;
} catch (err){
counter_timing.endTiming();
instrumentError(correlationId, 'myclient.get_data', err, reerror=true);
});
}
...
}
var client = MyDirectClient();
client.setReferences(References.fromTuples([
Descriptor("mygroup","controller","default","default","1.0"), controller
]));
var result = await client.getData("123", "1")
...