node-consul-service 1.0.7 → 1.0.9
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 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Node Consul Service
|
|
2
|
+
|
|
3
|
+
A Node.js service that integrates with HashiCorp Consul for service discovery and configuration management. This service provides a robust solution for managing distributed systems and microservices architecture.
|
|
4
|
+
|
|
5
|
+
### Features
|
|
6
|
+
- Service registration and discovery
|
|
7
|
+
- Health checking
|
|
8
|
+
- Key-value store integration
|
|
9
|
+
- Configuration management
|
|
10
|
+
- Distributed locking
|
|
11
|
+
- Leader election
|
|
12
|
+
|
|
13
|
+
### Prerequisites
|
|
14
|
+
- Node.js (v14 or higher)
|
|
15
|
+
- Consul server
|
|
16
|
+
- npm or yarn
|
|
17
|
+
|
|
18
|
+
### Installation
|
|
19
|
+
```bash
|
|
20
|
+
# Clone the repository
|
|
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
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Configuration
|
|
31
|
+
Create a `.env` file in the root directory with the following variables:
|
|
32
|
+
```env
|
|
33
|
+
CONSUL_HOST=localhost
|
|
34
|
+
CONSUL_PORT=8500
|
|
35
|
+
SERVICE_NAME=my-service
|
|
36
|
+
SERVICE_PORT=3000
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Usage
|
|
40
|
+
```bash
|
|
41
|
+
# Start the service
|
|
42
|
+
npm start
|
|
43
|
+
|
|
44
|
+
# Run tests
|
|
45
|
+
npm test
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Function Usage
|
|
49
|
+
Here are the main functions available in the service:
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
// Service Registration
|
|
53
|
+
const service = new ConsulService({
|
|
54
|
+
name: 'my-service',
|
|
55
|
+
port: 3000
|
|
56
|
+
});
|
|
57
|
+
await service.register();
|
|
58
|
+
|
|
59
|
+
// Service Discovery
|
|
60
|
+
const services = await service.discover('service-name');
|
|
61
|
+
|
|
62
|
+
// Key-Value Operations
|
|
63
|
+
await service.setKey('config/key', 'value');
|
|
64
|
+
const value = await service.getKey('config/key');
|
|
65
|
+
|
|
66
|
+
// Health Check
|
|
67
|
+
await service.checkHealth();
|
|
68
|
+
|
|
69
|
+
// Distributed Lock
|
|
70
|
+
const lock = await service.acquireLock('resource-name');
|
|
71
|
+
if (lock) {
|
|
72
|
+
// Perform locked operations
|
|
73
|
+
await service.releaseLock('resource-name');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Leader Election
|
|
77
|
+
await service.startLeaderElection('election-key');
|
|
78
|
+
service.on('leader', () => {
|
|
79
|
+
// Handle leader election
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
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
|
+
### License
|
|
91
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|