node-consul-service 1.0.9 → 1.0.11
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 +36 -57
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,87 +5,66 @@ A Node.js service that integrates with HashiCorp Consul for service discovery an
|
|
|
5
5
|
### Features
|
|
6
6
|
- Service registration and discovery
|
|
7
7
|
- Health checking
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
- Distributed locking
|
|
11
|
-
- Leader election
|
|
8
|
+
- Service communication
|
|
9
|
+
- Service instance management
|
|
12
10
|
|
|
13
11
|
### Prerequisites
|
|
14
12
|
- Node.js (v14 or higher)
|
|
15
13
|
- Consul server
|
|
16
|
-
- npm
|
|
14
|
+
- npm
|
|
17
15
|
|
|
18
16
|
### Installation
|
|
19
17
|
```bash
|
|
20
|
-
|
|
21
|
-
git clone https://github.com/your-username/node-consul-service.git
|
|
22
|
-
|
|
23
|
-
# Navigate to project directory
|
|
24
|
-
cd node-consul-service
|
|
25
|
-
|
|
26
|
-
# Install dependencies
|
|
27
|
-
npm install
|
|
18
|
+
npm install node-consul-service
|
|
28
19
|
```
|
|
29
20
|
|
|
30
21
|
### Configuration
|
|
31
|
-
Create a `.env` file in
|
|
22
|
+
Create a `.env` file in your project root with the following variables:
|
|
32
23
|
```env
|
|
33
|
-
CONSUL_HOST=
|
|
24
|
+
CONSUL_HOST=your-consul-host
|
|
34
25
|
CONSUL_PORT=8500
|
|
35
|
-
|
|
36
|
-
SERVICE_PORT=3000
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
### Usage
|
|
40
|
-
```bash
|
|
41
|
-
# Start the service
|
|
42
|
-
npm start
|
|
43
|
-
|
|
44
|
-
# Run tests
|
|
45
|
-
npm test
|
|
26
|
+
CONSUL_SECURE=false
|
|
46
27
|
```
|
|
47
28
|
|
|
48
29
|
### Function Usage
|
|
49
30
|
Here are the main functions available in the service:
|
|
50
31
|
|
|
51
32
|
```javascript
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
33
|
+
const {
|
|
34
|
+
registerService,
|
|
35
|
+
callService,
|
|
36
|
+
listServices,
|
|
37
|
+
getServiceInstances,
|
|
38
|
+
getRandomServiceInstance
|
|
39
|
+
} = require('node-consul-service');
|
|
40
|
+
|
|
41
|
+
// Register a new service
|
|
42
|
+
await registerService({
|
|
43
|
+
name: 'service-name',
|
|
44
|
+
id: 'service-id',
|
|
45
|
+
port: 3000,
|
|
46
|
+
address: 'localhost',
|
|
47
|
+
check: {
|
|
48
|
+
name: 'service health check',
|
|
49
|
+
http: 'http://localhost:3000/health',
|
|
50
|
+
interval: '10s',
|
|
51
|
+
timeout: '5s',
|
|
52
|
+
deregistercriticalserviceafter: '1m'
|
|
53
|
+
}
|
|
56
54
|
});
|
|
57
|
-
await service.register();
|
|
58
|
-
|
|
59
|
-
// Service Discovery
|
|
60
|
-
const services = await service.discover('service-name');
|
|
61
55
|
|
|
62
|
-
//
|
|
63
|
-
await
|
|
64
|
-
const value = await service.getKey('config/key');
|
|
56
|
+
// Call another service
|
|
57
|
+
const result = await callService('service-name', '/endpoint');
|
|
65
58
|
|
|
66
|
-
//
|
|
67
|
-
await
|
|
59
|
+
// List all registered services
|
|
60
|
+
const services = await listServices();
|
|
68
61
|
|
|
69
|
-
//
|
|
70
|
-
const
|
|
71
|
-
if (lock) {
|
|
72
|
-
// Perform locked operations
|
|
73
|
-
await service.releaseLock('resource-name');
|
|
74
|
-
}
|
|
62
|
+
// Get all instances of a specific service
|
|
63
|
+
const instances = await getServiceInstances('service-name');
|
|
75
64
|
|
|
76
|
-
//
|
|
77
|
-
await
|
|
78
|
-
service.on('leader', () => {
|
|
79
|
-
// Handle leader election
|
|
80
|
-
});
|
|
65
|
+
// Get a random instance of a service
|
|
66
|
+
const instance = await getRandomServiceInstance('service-name');
|
|
81
67
|
```
|
|
82
68
|
|
|
83
|
-
### Contributing
|
|
84
|
-
1. Fork the repository
|
|
85
|
-
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
86
|
-
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
87
|
-
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
88
|
-
5. Open a Pull Request
|
|
89
|
-
|
|
90
69
|
### License
|
|
91
70
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|