node-consul-service 1.0.18 → 1.0.20
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 +152 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,44 +1,60 @@
|
|
|
1
1
|
# Node Consul Service
|
|
2
2
|
|
|
3
|
-
A Node.js service that integrates with HashiCorp Consul for service discovery and configuration management. This service provides a
|
|
3
|
+
A robust Node.js service that integrates with HashiCorp Consul for service discovery and configuration management. This service provides a comprehensive solution for managing distributed systems and microservices architecture, making it easier to handle service communication, health checks, and configuration management in a distributed environment.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
-
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
5
|
+
## Table of Contents
|
|
6
|
+
- [Features](#features)
|
|
7
|
+
- [Prerequisites](#prerequisites)
|
|
8
|
+
- [Installation](#installation)
|
|
9
|
+
- [Configuration](#configuration)
|
|
10
|
+
- [API Documentation](#api-documentation)
|
|
11
|
+
- [Examples](#examples)
|
|
12
|
+
- [Contributing](#contributing)
|
|
13
|
+
- [License](#license)
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
## Features
|
|
16
|
+
- **Service Registration & Discovery**: Automatically register and discover services in your distributed system
|
|
17
|
+
- **Health Checking**: Built-in health check mechanisms for service monitoring
|
|
18
|
+
- **Service Communication**: Simplified inter-service communication with automatic load balancing
|
|
19
|
+
- **Service Instance Management**: Easy management of multiple service instances
|
|
20
|
+
- **Data Linking**: Efficient data aggregation and linking across services
|
|
21
|
+
- **Caching Support**: Optional caching mechanism for improved performance
|
|
22
|
+
|
|
23
|
+
## Prerequisites
|
|
12
24
|
- Node.js (v14 or higher)
|
|
13
|
-
- Consul server
|
|
14
|
-
- npm
|
|
25
|
+
- HashiCorp Consul server
|
|
26
|
+
- npm or yarn package manager
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
15
29
|
|
|
16
|
-
### Installation
|
|
17
30
|
```bash
|
|
31
|
+
# Using npm
|
|
18
32
|
npm install node-consul-service
|
|
33
|
+
|
|
34
|
+
# Using yarn
|
|
35
|
+
yarn add node-consul-service
|
|
19
36
|
```
|
|
20
37
|
|
|
21
|
-
|
|
38
|
+
## Configuration
|
|
39
|
+
|
|
22
40
|
Create a `.env` file in your project root with the following variables:
|
|
41
|
+
|
|
23
42
|
```env
|
|
24
43
|
CONSUL_HOST=your-consul-host
|
|
25
44
|
CONSUL_PORT=8500
|
|
26
45
|
CONSUL_SECURE=false
|
|
46
|
+
CONSUL_TOKEN=your-consul-token # Optional
|
|
47
|
+
CONSUL_DC=your-datacenter # Optional
|
|
27
48
|
```
|
|
28
49
|
|
|
29
|
-
|
|
30
|
-
|
|
50
|
+
## API Documentation
|
|
51
|
+
|
|
52
|
+
### Core Functions
|
|
31
53
|
|
|
54
|
+
#### Service Registration
|
|
32
55
|
```javascript
|
|
33
|
-
const {
|
|
34
|
-
registerService,
|
|
35
|
-
callService,
|
|
36
|
-
listServices,
|
|
37
|
-
getServiceInstances,
|
|
38
|
-
getRandomServiceInstance
|
|
39
|
-
} = require('node-consul-service');
|
|
56
|
+
const { registerService } = require('node-consul-service');
|
|
40
57
|
|
|
41
|
-
// Register a new service
|
|
42
58
|
await registerService({
|
|
43
59
|
name: 'service-name',
|
|
44
60
|
id: 'service-id',
|
|
@@ -52,10 +68,31 @@ await registerService({
|
|
|
52
68
|
deregistercriticalserviceafter: '1m'
|
|
53
69
|
}
|
|
54
70
|
});
|
|
71
|
+
```
|
|
55
72
|
|
|
56
|
-
|
|
73
|
+
#### Service Communication
|
|
74
|
+
```javascript
|
|
75
|
+
const { callService } = require('node-consul-service');
|
|
76
|
+
|
|
77
|
+
// Simple GET request
|
|
57
78
|
const result = await callService('service-name', '/endpoint');
|
|
58
79
|
|
|
80
|
+
// POST request with data
|
|
81
|
+
const response = await callService('service-name', {
|
|
82
|
+
method: 'POST',
|
|
83
|
+
path: '/endpoint',
|
|
84
|
+
data: { key: 'value' }
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### Service Discovery
|
|
89
|
+
```javascript
|
|
90
|
+
const {
|
|
91
|
+
listServices,
|
|
92
|
+
getServiceInstances,
|
|
93
|
+
getRandomServiceInstance
|
|
94
|
+
} = require('node-consul-service');
|
|
95
|
+
|
|
59
96
|
// List all registered services
|
|
60
97
|
const services = await listServices();
|
|
61
98
|
|
|
@@ -66,5 +103,97 @@ const instances = await getServiceInstances('service-name');
|
|
|
66
103
|
const instance = await getRandomServiceInstance('service-name');
|
|
67
104
|
```
|
|
68
105
|
|
|
69
|
-
###
|
|
106
|
+
### Data Linking
|
|
107
|
+
|
|
108
|
+
The `dataLink` function provides a powerful way to aggregate and link data from multiple services efficiently.
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
const { dataLink } = require('node-consul-service');
|
|
112
|
+
|
|
113
|
+
const data = [
|
|
114
|
+
{ userId: '123', name: 'John' },
|
|
115
|
+
{ userId: '456', name: 'Jane' }
|
|
116
|
+
];
|
|
117
|
+
|
|
118
|
+
const schema = [
|
|
119
|
+
{
|
|
120
|
+
filed: 'userId', // Field name in the original data
|
|
121
|
+
service: 'user-service', // Target service name
|
|
122
|
+
path: '/api/users/batch', // API endpoint path
|
|
123
|
+
cacheGetter: async (ids) => {
|
|
124
|
+
// Optional function to get data from cache
|
|
125
|
+
return await cache.getUsers(ids);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
];
|
|
129
|
+
|
|
130
|
+
const result = await dataLink(data, schema);
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### Schema Properties
|
|
134
|
+
- `filed`: Field name in the original data that contains the identifier
|
|
135
|
+
- `service`: Name of the service to be called
|
|
136
|
+
- `path`: API endpoint path in the target service
|
|
137
|
+
- `cacheGetter`: Optional function to get data from cache before calling the service
|
|
138
|
+
|
|
139
|
+
## Examples
|
|
140
|
+
|
|
141
|
+
### Basic Service Setup
|
|
142
|
+
```javascript
|
|
143
|
+
const { registerService, callService } = require('node-consul-service');
|
|
144
|
+
|
|
145
|
+
// Register your service
|
|
146
|
+
await registerService({
|
|
147
|
+
name: 'my-service',
|
|
148
|
+
id: 'my-service-1',
|
|
149
|
+
port: 3000,
|
|
150
|
+
address: 'localhost',
|
|
151
|
+
check: {
|
|
152
|
+
http: 'http://localhost:3000/health',
|
|
153
|
+
interval: '10s'
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// Call another service
|
|
158
|
+
const response = await callService('other-service', '/api/data');
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### Advanced Data Linking
|
|
162
|
+
```javascript
|
|
163
|
+
const { dataLink } = require('node-consul-service');
|
|
164
|
+
|
|
165
|
+
// Complex data linking example
|
|
166
|
+
const orders = [
|
|
167
|
+
{ orderId: '1', userId: '123', productId: 'P1' },
|
|
168
|
+
{ orderId: '2', userId: '456', productId: 'P2' }
|
|
169
|
+
];
|
|
170
|
+
|
|
171
|
+
const schema = [
|
|
172
|
+
{
|
|
173
|
+
filed: 'userId',
|
|
174
|
+
service: 'user-service',
|
|
175
|
+
path: '/api/users/batch'
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
filed: 'productId',
|
|
179
|
+
service: 'product-service',
|
|
180
|
+
path: '/api/products/batch'
|
|
181
|
+
}
|
|
182
|
+
];
|
|
183
|
+
|
|
184
|
+
const enrichedOrders = await dataLink(orders, schema);
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Contributing
|
|
188
|
+
|
|
189
|
+
We welcome contributions! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
|
190
|
+
|
|
191
|
+
1. Fork the repository
|
|
192
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
193
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
194
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
195
|
+
5. Open a Pull Request
|
|
196
|
+
|
|
197
|
+
## License
|
|
198
|
+
|
|
70
199
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|