@ti-engine/core 1.0.4 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -6
- package/{settings.json → bin/settings.json} +0 -0
- package/{start-instance.js → bin/start-instance.js} +7 -7
- package/components/exchange/default/default-message-receiver.js +2 -3
- package/components/service-provider.js +6 -11
- package/docs/diagram1.png +0 -0
- package/package.json +3 -6
- package/utils/tools.js +1 -1
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Flexible framework for the creation of microservices with [node.js](https://nodejs.org/).
|
|
3
3
|
|
|
4
4
|
## introduction
|
|
5
|
-
The **ti-engine** is an open source, free to use - both for personal and commercial projects - framework for the creation of microservice-based solutions using **node.js**. The
|
|
5
|
+
The **ti-engine** is an open source, free to use - both for personal and commercial projects - framework for the creation of microservice-based solutions using **node.js**. The architectural concept of the framework is based on a standard _messaging system_ that allows for certain customization but also provides predictability and traceability of its behavior.
|
|
6
6
|
|
|
7
|
-
Being a
|
|
7
|
+
Being a messaging system, the **ti-engine** relies on a message broker for the actual exchange of messages between microservice instances. The default implementation of the framework uses [Redis](https://redis.io/) cache, however, you could create your own implementation using something like [Rabbit MQ](https://www.rabbitmq.com/). See the [advanced topics](#advanced-topics) section of this documentation for guides on how to do this.
|
|
8
8
|
|
|
9
9
|
Please be aware, that this framework is under active development and will expand in the near future. Also, this documentation is still in the process of being created and refined. Make sure to keep an eye on the changes in case you want to use it in the meantime.
|
|
10
10
|
|
|
@@ -23,17 +23,102 @@ These are just some benefits **ti-engine** offers. Get to know it better to find
|
|
|
23
23
|
|
|
24
24
|
## prerequisites & installation
|
|
25
25
|
In order to run the basic ti-engine framework you will need a couple of things:
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
* A local [node.js installation](https://nodejs.org/en/download/) with a minimum version of **14.17.0**
|
|
27
|
+
* A local or remote [Redis cache installation](https://redis.io/download) with a minimum version of **5.0.14**
|
|
28
|
+
|
|
29
|
+
If you are working under Windows OS and you need to install Redis, take a look at this [guide](https://redis.com/blog/redis-on-windows-10/).
|
|
28
30
|
|
|
29
31
|
To get the framework itself, use the command `npm install @ti-engine/core`. And to include it directly in your package.json dependencies execute `npm install @ti-engine/core --save-prod`.
|
|
30
32
|
|
|
31
33
|
## getting started
|
|
32
|
-
To start using the ti-engine
|
|
34
|
+
To start using the **ti-engine**, you will have to make sure that all prerequisites are available and operational. However, before we get to the fun part you need to also consider a couple of very important things while working with this framework:
|
|
35
|
+
1. The runtime configuration of the framework can be customized using ENV variables. These can be provided to node.js in all the standard ways, but there is also an option to include an `.env` file.
|
|
36
|
+
2. It loads your framework-related custom scripts and files dynamically, but it always assumes their provided paths are relative to the _current working directory_ (i.e. it uses `process.cwd()`). Be mindful of that whenever you declare file paths in the various settings.
|
|
33
37
|
|
|
34
|
-
|
|
38
|
+
Once you have everything else ready, you should download the **ti-engine** tester module with the command `npm install @ti-engine/tester`. The tester module packages an example microservice that shows the basic approach for using the framework. To make sure everything is working properly, you should try and start the tester service:
|
|
39
|
+
1. Open a command prompt and navigate to the directory of the tester module; it should be something like that:
|
|
40
|
+
`[path to your project]/node_modules/@ti-engine/tester`
|
|
41
|
+
2. Execute the following command `node ../core/bin/start-instance.js`
|
|
42
|
+
3. If everything was done properly, you should see the following output:
|
|
43
|
+
```text
|
|
44
|
+
[timestamp]: [instance-id] - notice - Starting new instance of type 'tester-service' with instance ID '[instance-id]'.
|
|
45
|
+
[timestamp]: [instance-id] - info - Starting service registration process. There is NO default service handler provided.
|
|
46
|
+
[timestamp]: [instance-id] - info - Registration of defined services completed with 2 successful out of 2 total.
|
|
47
|
+
[timestamp]: [instance-id] - notice - Instance '[instance-id]' started successfully.
|
|
48
|
+
[timestamp]: [instance-id] - info - Connection to Redis server 127.0.0.1:6379 (re)established by client 'connection-msg-responses-out' and is ready to be used.
|
|
49
|
+
[timestamp]: [instance-id] - info - Connection to Redis server 127.0.0.1:6379 (re)established by client 'system' and is ready to be used.
|
|
50
|
+
[timestamp]: [instance-id] - info - Connection to Redis server 127.0.0.1:6379 (re)established by client 'connection-msg-requests-in' and is ready to be used.
|
|
51
|
+
[timestamp]: [instance-id] - info - Connection to Redis server 127.0.0.1:6379 (re)established by client 'connection-msg-requests-out' and is ready to be used.
|
|
52
|
+
[timestamp]: [instance-id] - info - Connection to Redis server 127.0.0.1:6379 (re)established by client 'connection-msg-responses-in' and is ready to be used.
|
|
53
|
+
[timestamp]: [instance-id] - notice - Execution of service1 result:
|
|
54
|
+
{ exception: undefined, isSuccessful: true, payload: { s1Timestamp: [timestamp] } }
|
|
55
|
+
[timestamp]: [instance-id] - notice - Execution of service2 result:
|
|
56
|
+
{ exception: undefined, isSuccessful: true, payload: { s1Timestamp: [timestamp], s2TimestampStart: [timestamp], s2TimestampEnd: [timestamp] } }
|
|
57
|
+
```
|
|
58
|
+
Now let's analyse that output. For the sake of completeness, the `[timestamp]` and `[instance-id]` are placeholders of the actual values you'll see there. The timestamps are in UTC and show a date followed by time.
|
|
59
|
+
|
|
60
|
+
At the start of the output log you can see a NOTICE that tells you a couple of important things:
|
|
61
|
+
* The instance name - in this case `tester-service`. In the terminology of the framework, this is also known as a _service domain name_.
|
|
62
|
+
* The _instance identificator_. It is an uuid string with a `ti-` prefix, that is generated by the framework at process start. It can and will be used to trace the messages during their movement through the microservice ecosystem. But more on that later.
|
|
63
|
+
|
|
64
|
+
Following that come a couple of INFO lines that inform you about the microservice interface state. The framework starts with the process of registration of _business services_ within the service domain of the microservice `tester-service` and successfully adds 2 such services. The necessary information for this is read from a JSON config file included in the package. We'll get into more details on what this all means in the section [creating a microservice](#creating-a-microservice).
|
|
65
|
+
|
|
66
|
+
Once the initialization sequence has completed the framework informs you that the microservice instance has started successfully. If the framework encountered an error during initialization instead, you would see something like this:
|
|
67
|
+
```text
|
|
68
|
+
[timestamp]: [instance-id] - notice - Starting new instance of type 'tester-service' with instance ID '[instance-id]'.
|
|
69
|
+
[timestamp]: [instance-id] - alert - Error detected in the instance startup script!
|
|
70
|
+
```
|
|
71
|
+
The following 5 lines inform you about the successful connection to Redis. The default configuration assumes that your Redis is running on localhost and uses the default port. If you have a different setup, you can provide the host and port via ENV variables. We'll cover that in the section [using the framework](#using-the-framework).
|
|
72
|
+
|
|
73
|
+
Finally, you should see a couple of execution statements with their results in JSON format.
|
|
74
|
+
|
|
75
|
+
You can now kill the node process which should show you the following two lines:
|
|
76
|
+
```text
|
|
77
|
+
[timestamp]: [instance-id] - notice - SIGINT event detected in main instance process.
|
|
78
|
+
[timestamp]: [instance-id] - notice - Instance '[instance-id]' shut down successfully.
|
|
79
|
+
```
|
|
80
|
+
The framework will always try to capture the shut-down event and log it. This should work even in container environment, but it might depend on your setup whether the last two entries will reach the logging system or not.
|
|
81
|
+
|
|
82
|
+
The tester module gets its starting configuration from an `.env` file included in the package. If you open it, this is what you'll see:
|
|
83
|
+
```text
|
|
84
|
+
TI_INSTANCE_CLASS=tester-service.js
|
|
85
|
+
TI_INSTANCE_CONFIG=tester-service.json
|
|
86
|
+
TI_INSTANCE_NAME=tester-service
|
|
87
|
+
TI_LOG_MIN_LEVEL=200
|
|
88
|
+
```
|
|
89
|
+
The first variable `TI_INSTANCE_CLASS` is mandatory for every microservice you create with the **ti-engine**. It needs to specify the path to the module that is your microservice. Remember, that this path has to be relative to the working directory in which you plan to execute the `node` command. This is especially important when you're configuring your microservices to work in containers. You can find the full list of available ENV variables and what they do below.
|
|
90
|
+
|
|
91
|
+
Before moving on, also take a good look at the file `bin/start-instance.js`. It should give you an idea on how to the process of starting and stopping a microservice operates. In most cases this file should be sufficient as a starting script for your **ti-engine** based microservice applications. You can, of course, create your own starting script, but then you'll have to consider all necessary steps to properly handle the microservice instance.
|
|
35
92
|
|
|
36
93
|
## architecture
|
|
94
|
+
The architectural approach for the **ti-engine** is done in _tiers_ with lower tiers being unaware of the tiers above them. The framework prefers a high level of abstraction in all its tiers and provides many options for customization and extension. While the language is JavaScript, the structuring of the framework follows the OOP principles, and you will find a lot of abstract classes and methods that require you to implement them. These are always marked with the `@abstract` annotation but if you happen to miss one, the framework will raise an exception when you try to use it in your solution.
|
|
95
|
+
|
|
96
|
+
There are three general tiers in the **ti-engine**:
|
|
97
|
+
1. message exchange
|
|
98
|
+
2. service interface
|
|
99
|
+
3. solution implementation
|
|
100
|
+
|
|
101
|
+
See the following sections for more information on each of them.
|
|
102
|
+
|
|
103
|
+
### tier 1 - message exchange
|
|
104
|
+
This is the lowest framework tier, unless we count the actual data objects processed by the framework. As you already know, the foundational **ti-engine** concept is that of a messaging system. Therefore, the first tier provides an abstraction over a chosen message broker (Redis by default). That abstraction makes it easy to switch between message brokers whenever you want to without having to change anything above tier 1. It also provides several added bonuses that can accelerate your work - message encryption, message tracing, message observers, and others. More details about each of these features will be covered in section [using the framework](#using-the-framework).
|
|
105
|
+
|
|
106
|
+
Another important aspect for you to remember is that the message exchange is entirely _asynchronous_. This helps reduce system load and optimizes the usage of the available resources. Even so each node.js process can handle a limited amount of load. Therefore, you should plan for running multiple identical senders and receives in order to scale your solution. But more on that later.
|
|
107
|
+
|
|
108
|
+
For now, take a look at the following diagram:
|
|
109
|
+
|
|
110
|
+

|
|
111
|
+
|
|
112
|
+
It shows the standard flow of a message exchange between one sender and _n_ identical message receivers. The sender splits each message to an _envelope_ and a _payload_, then stores the payload in the shared cache and enqueues the envelope in the requests (destination) queue. Receivers can subscribe to that queue in order to fetch enqueued messages and process their contents. During the fetch sequence a receiver assembles the full message by getting the payload from the storage. This process is depicted by the blue flow lines.
|
|
113
|
+
|
|
114
|
+
After the processing is done the message payload is modified and the receiver sends the message back to the original sender using the same mechanism. It splits the message in envelope and payload, stores the payload in the storage and enqueues the envelope in the sender response (source) queue. The sender will then assemble the message back and process the contained results. This process is depicted by the red flow lines.
|
|
115
|
+
|
|
116
|
+
In this scenario the framework utilizes _Redis lists_ as queues for the message envelopes and _Redis hash_ as message payload storage. Other message brokers might utilize a slightly different approach, but they should still adhere to the same logical flow.
|
|
117
|
+
|
|
118
|
+
### tier 2 - service interface
|
|
119
|
+
Under development...
|
|
120
|
+
|
|
121
|
+
### tier 3 - solution implementation
|
|
37
122
|
Under development...
|
|
38
123
|
|
|
39
124
|
## creating a microservice
|
|
File without changes
|
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
|
|
6
6
|
"use strict";
|
|
7
7
|
|
|
8
|
-
// load any ENV variables defined in a .env file:
|
|
9
|
-
require( "dotenv" ).config();
|
|
10
|
-
|
|
11
8
|
const _ = require( "lodash" );
|
|
12
|
-
const
|
|
9
|
+
const path = require( "path" );
|
|
13
10
|
const tools = require( "#tools" );
|
|
14
11
|
const logger = require( "#logger" );
|
|
15
12
|
|
|
13
|
+
// load any ENV variables defined in a .env file:
|
|
14
|
+
require( "dotenv" ).config( { path: path.join( process.cwd(), ".env" ) } );
|
|
15
|
+
|
|
16
16
|
// configure the current instance variables before requiring any platform modules and store the necessary ones in memory cache:
|
|
17
17
|
process.env.TI_INSTANCE_ID = "ti-" + tools.getUUID();
|
|
18
18
|
process.env.TI_INSTANCE_CLASS = process.env.TI_INSTANCE_CLASS || "";
|
|
@@ -75,11 +75,11 @@ try {
|
|
|
75
75
|
logger.log( `Starting new instance of type '${ process.env.TI_INSTANCE_NAME }' with instance ID '${ process.env.TI_INSTANCE_ID }'.`, logger.logSeverity.NOTICE );
|
|
76
76
|
|
|
77
77
|
/** @type ServiceInstance */
|
|
78
|
-
const serviceConstructor = require( process.env.TI_INSTANCE_CLASS );
|
|
78
|
+
const serviceConstructor = require( path.join( process.cwd(), process.env.TI_INSTANCE_CLASS ) );
|
|
79
79
|
const serviceConfigPath = process.env.TI_INSTANCE_CONFIG;
|
|
80
80
|
let serviceConfig = {};
|
|
81
|
-
if (
|
|
82
|
-
serviceConfig = require( process.env.TI_INSTANCE_CONFIG );
|
|
81
|
+
if ( serviceConfigPath ) {
|
|
82
|
+
serviceConfig = require( path.join( process.cwd(), process.env.TI_INSTANCE_CONFIG ) );
|
|
83
83
|
}
|
|
84
84
|
const mainInstance = new serviceConstructor( process.env.TI_INSTANCE_NAME, serviceConfig );
|
|
85
85
|
|
|
@@ -23,10 +23,9 @@ class DefaultMessageReceiver extends MessageReceiver {
|
|
|
23
23
|
* @constructor
|
|
24
24
|
* @param {string} identifier An identifier for this message handler. Should be unique in the context of the message exchange.
|
|
25
25
|
* @param {string} receiveQueue The queue from which the messages will be received.
|
|
26
|
-
* @param {string} [processingQueue=undefined] The queue in which the messages will be put for processing (if necessary).
|
|
27
26
|
*/
|
|
28
|
-
constructor( identifier, receiveQueue
|
|
29
|
-
super( identifier, receiveQueue
|
|
27
|
+
constructor( identifier, receiveQueue ) {
|
|
28
|
+
super( identifier, receiveQueue );
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
/**
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
const ServiceConsumer = require( "#service-consumer" );
|
|
7
7
|
const _ = require( "lodash" );
|
|
8
|
-
const
|
|
8
|
+
const path = require( "path" );
|
|
9
9
|
const exceptions = require( "#exceptions" );
|
|
10
10
|
const logger = require( "#logger" );
|
|
11
11
|
const messageDispatcher = require( "#message-dispatcher" );
|
|
@@ -137,16 +137,11 @@ class ServiceProvider extends ServiceConsumer {
|
|
|
137
137
|
/** @type {ServiceHandlerMethod} */
|
|
138
138
|
let serviceHandler = null;
|
|
139
139
|
if ( serviceDefinition.serviceFile ) {
|
|
140
|
-
let
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
} catch ( error ) {
|
|
146
|
-
logger.log( `Specified service handler file '${ serviceDefinition.serviceFile }' could not be loaded!`, logger.logSeverity.ERROR, error );
|
|
147
|
-
}
|
|
148
|
-
} else {
|
|
149
|
-
logger.log( `Specified service handler file '${ serviceDefinition.serviceFile }' was not found in the specified location!`, logger.logSeverity.WARNING );
|
|
140
|
+
let serviceFilePath = path.join( process.cwd(), serviceDefinition.serviceFile );
|
|
141
|
+
try {
|
|
142
|
+
serviceHandler = require( serviceFilePath ).service;
|
|
143
|
+
} catch ( error ) {
|
|
144
|
+
logger.log( `Specified service handler file '${ serviceFilePath }' could not be loaded!`, logger.logSeverity.ERROR, error );
|
|
150
145
|
}
|
|
151
146
|
} else {
|
|
152
147
|
if ( typeof ( defaultServiceHandler ) === "function" ) {
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ti-engine/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "The ti-engine is an open source, free to use - both for personal and commercial projects - framework for the creation of microservice-based solutions using node.js.",
|
|
5
5
|
"author": "Boris Kostadinov <kostadinov.boris@gmail.com>",
|
|
6
6
|
"license": "ISC",
|
|
7
|
-
"main": "./start-instance.js",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"start": "node ./start-instance.js"
|
|
10
|
-
},
|
|
11
7
|
"exports": {
|
|
12
8
|
"./exceptions": "./utils/exceptions.js",
|
|
13
9
|
"./logger": "./utils/logger.js",
|
|
@@ -41,10 +37,11 @@
|
|
|
41
37
|
"#service-executor": "./components/service-executor.js",
|
|
42
38
|
"#service-instance": "./components/service-instance.js",
|
|
43
39
|
"#service-provider": "./components/service-provider.js",
|
|
44
|
-
"#settings": "./settings.json",
|
|
40
|
+
"#settings": "./bin/settings.json",
|
|
45
41
|
"#tools": "./utils/tools.js"
|
|
46
42
|
},
|
|
47
43
|
"dependencies": {
|
|
44
|
+
"blake2": "^4.0.2",
|
|
48
45
|
"dotenv": "^10.0.0",
|
|
49
46
|
"fs-extra": "^10.0.0",
|
|
50
47
|
"lodash": "^4.17.21",
|