public class ServiceBroker extends ContextSource implements MetricConstants
ServiceBroker broker = new ServiceBroker("node-1");
broker.createService(new Service("math") {
Action add = ctx -> {
return ctx.params.get("a").asInteger() + ctx.params.get("b").asInteger();
};
});
broker.start();
broker.call("math.add", "a", 5, "b", 3).then(rsp -> {
broker.getLogger().info("Response: " + rsp.asInteger());
});
This project is based on the idea of Moleculer Microservices Framework for
Node.js (https://moleculer.services). Special thanks to the Moleculer's
project owner (https://github.com/icebob) for the consultations.| Modifier and Type | Field and Description |
|---|---|
protected ServiceBrokerConfig |
config
Configuration settings and internal components (event bus, cacher,
service registry, etc.) of this node / broker.
|
protected static org.slf4j.Logger |
logger
SLF4J logger of this class.
|
protected java.util.LinkedHashSet<Middleware> |
middlewares
Middlewares which defined and added to the Broker before the boot
process.
|
protected java.util.LinkedHashSet<java.lang.String> |
serviceNames
Service names (keys).
|
protected ServiceRegistry |
serviceRegistry
Implementation of the service registry of the current node.
|
protected java.util.concurrent.ConcurrentHashMap<java.lang.String,Service> |
services
Services which defined and added to the Broker before the boot process.
|
static java.lang.String |
SOFTWARE_VERSION
Version of the Java ServiceBroker API.
|
protected StrategyFactory |
strategyFactory
Default (round-robin) service invocation factory.
|
protected Transporter |
transporter
Implementation of the Transporter.
|
eventbus, nodeID, serviceInvoker, uidGeneratorMOLECULER_CACHER_CLEAN_TIME, MOLECULER_CACHER_CLEAN_TIME_DESC, MOLECULER_CACHER_CLEAN_TOTAL, MOLECULER_CACHER_CLEAN_TOTAL_DESC, MOLECULER_CACHER_DEL_TIME, MOLECULER_CACHER_DEL_TIME_DESC, MOLECULER_CACHER_DEL_TOTAL, MOLECULER_CACHER_DEL_TOTAL_DESC, MOLECULER_CACHER_EXPIRED_TOTAL, MOLECULER_CACHER_EXPIRED_TOTAL_DESC, MOLECULER_CACHER_FOUND_TOTAL, MOLECULER_CACHER_FOUND_TOTAL_DESC, MOLECULER_CACHER_GET_TIME, MOLECULER_CACHER_GET_TIME_DESC, MOLECULER_CACHER_GET_TOTAL, MOLECULER_CACHER_GET_TOTAL_DESC, MOLECULER_CACHER_SET_TIME, MOLECULER_CACHER_SET_TIME_DESC, MOLECULER_CACHER_SET_TOTAL, MOLECULER_CACHER_SET_TOTAL_DESC, MOLECULER_CIRCUIT_BREAKER_HALF_OPENED_ACTIVE, MOLECULER_CIRCUIT_BREAKER_HALF_OPENED_ACTIVE_DESC, MOLECULER_CIRCUIT_BREAKER_OPENED_ACTIVE, MOLECULER_CIRCUIT_BREAKER_OPENED_ACTIVE_DESC, MOLECULER_CIRCUIT_BREAKER_OPENED_TOTAL, MOLECULER_CIRCUIT_BREAKER_OPENED_TOTAL_DESC, MOLECULER_EVENT_BROADCAST_TOTAL, MOLECULER_EVENT_BROADCAST_TOTAL_DESC, MOLECULER_EVENT_BROADCASTLOCAL_TOTAL, MOLECULER_EVENT_BROADCASTLOCAL_TOTAL_DESC, MOLECULER_EVENT_EMIT_TOTAL, MOLECULER_EVENT_EMIT_TOTAL_DESC, MOLECULER_EVENT_RECEIVED_ACTIVE, MOLECULER_EVENT_RECEIVED_ACTIVE_DESC, MOLECULER_EVENT_RECEIVED_ERROR_TOTAL, MOLECULER_EVENT_RECEIVED_ERROR_TOTAL_DESC, MOLECULER_EVENT_RECEIVED_TIME, MOLECULER_EVENT_RECEIVED_TIME_DESC, MOLECULER_EVENT_RECEIVED_TOTAL, MOLECULER_EVENT_RECEIVED_TOTAL_DESC, MOLECULER_EXECUTOR, MOLECULER_REQUEST_ACTIVE, MOLECULER_REQUEST_ACTIVE_DESC, MOLECULER_REQUEST_ERROR_TOTAL, MOLECULER_REQUEST_ERROR_TOTAL_DESC, MOLECULER_REQUEST_LEVELS, MOLECULER_REQUEST_LEVELS_DESC, MOLECULER_REQUEST_RETRY_ATTEMPTS_TOTAL, MOLECULER_REQUEST_RETRY_ATTEMPTS_TOTAL_DESC, MOLECULER_REQUEST_TIME, MOLECULER_REQUEST_TIME_DESC, MOLECULER_REQUEST_TIMEOUT_TOTAL, MOLECULER_REQUEST_TIMEOUT_TOTAL_DESC, MOLECULER_REQUEST_TOTAL, MOLECULER_REQUEST_TOTAL_DESC, MOLECULER_SCHEDULER, MOLECULER_TRANSIT_CONNECTED, MOLECULER_TRANSIT_CONNECTED_DESC, MOLECULER_TRANSIT_ORPHAN_RESPONSE_TOTAL, MOLECULER_TRANSIT_ORPHAN_RESPONSE_TOTAL_DESC, MOLECULER_TRANSIT_REQUESTS_ACTIVE, MOLECULER_TRANSIT_REQUESTS_ACTIVE_DESC, MOLECULER_TRANSIT_STREAMS_RECEIVE_ACTIVE, MOLECULER_TRANSIT_STREAMS_RECEIVE_ACTIVE_DESC, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_BYTES, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_BYTES_DESC, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_TOTAL, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_TOTAL_DESC, MOLECULER_TRANSPORTER_PACKETS_SENT_BYTES, MOLECULER_TRANSPORTER_PACKETS_SENT_BYTES_DESC, MOLECULER_TRANSPORTER_PACKETS_SENT_TOTAL, MOLECULER_TRANSPORTER_PACKETS_SENT_TOTAL_DESC| Constructor and Description |
|---|
ServiceBroker()
Creates a new ServiceBroker without
Transporter. |
ServiceBroker(ServiceBrokerConfig config)
Creates a new ServiceBroker by the specified
configuration. |
ServiceBroker(java.lang.String nodeID)
Creates a new ServiceBroker without
Transporter and with the
specified ContextSource.nodeID. |
ServiceBroker(java.lang.String nodeID,
Cacher cacher,
Transporter transporter)
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
broadcastServicesChanged() |
static ServiceBrokerBuilder |
builder()
Creates a new
ServiceBrokerBuilder instance. |
ServiceBroker |
createService(Service service)
Installs a new service instance and notifies other nodes about the
actions/listeners of the new service.
|
ServiceBroker |
createService(java.lang.String name,
Service service)
Installs a new service with the specified name (eg.
|
Action |
getAction(java.lang.String actionName)
Returns an action by name.
|
Action |
getAction(java.lang.String actionName,
java.lang.String nodeID)
Returns an action by name and nodeID.
|
ServiceBrokerConfig |
getConfig()
Returns the configuration settings and internal components (event bus,
cacher, service registry, etc.) of this node / broker.
|
Service |
getLocalService(java.lang.String serviceName)
Returns a local service by name (eg.
|
org.slf4j.Logger |
getLogger()
Returns the SLF4J logger of this broker instance.
|
org.slf4j.Logger |
getLogger(java.lang.Class<?> clazz)
Returns a logger named corresponding to the class passed as parameter.
|
org.slf4j.Logger |
getLogger(java.lang.String name)
Return a logger named according to the name parameter.
|
java.lang.String |
getNodeID()
Returns the unique
ContextSource.nodeID of this node (~= ServiceBroker
instance). |
java.lang.String |
getProtocolVersion()
Returns the version of the implemented Moleculer Protocol.
|
protected void |
initJsonReader()
Set global JSON reader API (Jackson, Gson, Boon, FastJson, etc.).
|
protected void |
initJsonWriter()
Set global JSON writer API (Jackson, Gson, Boon, FastJson, etc.)
|
io.datatree.Promise |
ping(long timeoutMillis,
java.lang.String nodeID)
Sends a PING message to the specified node.
|
io.datatree.Promise |
ping(java.lang.String nodeID)
Sends a PING message to the specified node.
|
boolean |
repl()
Starts a local (System in/out) developer console.
|
boolean |
repl(boolean local)
Starts a local (System in/out) or a remote (telnet-based) developer
console.
|
ServiceBroker |
start()
Start broker.
|
protected <TYPE extends MoleculerLifecycle> |
start(TYPE component)
Starts the specified
MoleculerComponent. |
ServiceBroker |
stop()
Stop broker and all internal components (event bus, context factory,
etc.).
|
protected void |
stop(MoleculerComponent component)
Stops the specified
MoleculerComponent. |
ServiceBroker |
use(java.util.Collection<Middleware> middlewares)
Installs a collection of middlewares.
|
ServiceBroker |
use(Middleware... middlewares)
Installs one or an array of middleware(s).
|
io.datatree.Promise |
waitForServices(long timeoutMillis,
java.util.Collection<java.lang.String> services)
Waits for a collection of services.
|
io.datatree.Promise |
waitForServices(long timeoutMillis,
java.lang.String... services)
Waits for one (or an array of) service(s).
|
io.datatree.Promise |
waitForServices(java.lang.String... services)
Waits for one or more services.
|
broadcast, broadcast, broadcast, broadcast, broadcast, broadcast, broadcastLocal, broadcastLocal, broadcastLocal, call, call, call, call, call, call, createStream, emit, emit, emit, emit, emit, emitpublic static final java.lang.String SOFTWARE_VERSION
protected static final org.slf4j.Logger logger
protected final ServiceBrokerConfig config
getConfig method to access this object.protected final java.util.concurrent.ConcurrentHashMap<java.lang.String,Service> services
protected final java.util.LinkedHashSet<java.lang.String> serviceNames
protected final java.util.LinkedHashSet<Middleware> middlewares
protected StrategyFactory strategyFactory
getConfig().getStrategyFactory() to access this instance.protected ServiceRegistry serviceRegistry
getConfig().getServiceRegistry() to access this instance.ContextSource.call(String, Object...)protected Transporter transporter
getConfig().getTransporter() to access this instance. Can be
null.public ServiceBroker(ServiceBrokerConfig config)
configuration.config - configuration of the Brokerpublic ServiceBroker()
Transporter. The
ContextSource.nodeID is generated from the host name and process ID.public ServiceBroker(java.lang.String nodeID)
Transporter and with the
specified ContextSource.nodeID.nodeID - the unique ContextSource.nodeID of this nodepublic ServiceBroker(java.lang.String nodeID,
Cacher cacher,
Transporter transporter)
nodeID - the unique ContextSource.nodeID of this nodecacher - Cacher of this broker instancetransporter - Transporter of this broker instancepublic static ServiceBrokerBuilder builder()
ServiceBrokerBuilder instance. Sample of usage:public java.lang.String getProtocolVersion()
public ServiceBrokerConfig getConfig()
public java.lang.String getNodeID()
ContextSource.nodeID of this node (~= ServiceBroker
instance).ContextSource.nodeIDpublic ServiceBroker start() throws java.lang.Exception
java.lang.Exception - fatal error (missing classes or JARs, used port, etc.)protected void initJsonReader()
protected void initJsonWriter()
protected <TYPE extends MoleculerLifecycle> TYPE start(TYPE component) throws java.lang.Exception
MoleculerComponent.TYPE - Moleculer component (service registry, transporter, etc.)component - component to startjava.lang.Exception - any configuration or I/O exceptionspublic ServiceBroker stop()
protected void stop(MoleculerComponent component)
MoleculerComponent.component - component to stoppublic org.slf4j.Logger getLogger()
public org.slf4j.Logger getLogger(java.lang.Class<?> clazz)
clazz - the returned logger will be named after clazzpublic org.slf4j.Logger getLogger(java.lang.String name)
name - the name of the loggerpublic ServiceBroker createService(Service service)
service - the new service instancepublic ServiceBroker createService(java.lang.String name, Service service)
name - custom service name (eg. "user", "logger", "configurator",
etc.)service - the new service instanceprotected void broadcastServicesChanged()
public Service getLocalService(java.lang.String serviceName)
serviceName - service name (eg. "user", "logger", "configurator", etc.)java.util.NoSuchElementException - if the service name is not validpublic ServiceBroker use(java.util.Collection<Middleware> middlewares)
middlewares - collection of middlewarespublic ServiceBroker use(Middleware... middlewares)
middlewares - array of middlewarespublic Action getAction(java.lang.String actionName)
actionName - name of the action (in "service.action" syntax, eg.
"math.add")public Action getAction(java.lang.String actionName, java.lang.String nodeID)
actionName - name of the action (in "service.action" syntax, eg.
"math.add")nodeID - node identifier where the service is locatedpublic io.datatree.Promise waitForServices(java.lang.String... services)
services - service namespublic io.datatree.Promise waitForServices(long timeoutMillis,
java.lang.String... services)
timeoutMillis - timeout in millisecondsservices - array of service namespublic io.datatree.Promise waitForServices(long timeoutMillis,
java.util.Collection<java.lang.String> services)
timeoutMillis - timeout in millisecondsservices - collection of service namespublic io.datatree.Promise ping(java.lang.String nodeID)
nodeID - node ID of the destination nodepublic io.datatree.Promise ping(long timeoutMillis,
java.lang.String nodeID)
timeoutMillis - ping timeout in millisecondsnodeID - node ID of the destination nodepublic boolean repl()
public boolean repl(boolean local)
local - true = local console, false = telnet-based console