node-consul-service 1.0.0
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/ .gitignore +38 -0
- package/dist/consul/ registry.js +32 -0
- package/dist/consul/client.js +3 -0
- package/dist/consul/discovery.js +16 -0
- package/dist/consul/httpCaller.js +8 -0
- package/dist/consul/types.js +1 -0
- package/dist/index.cjs.js +19004 -0
- package/dist/index.esm.js +18997 -0
- package/dist/index.js +3 -0
- package/package.json +32 -0
package/ .gitignore
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# node_modules
|
|
2
|
+
node_modules/
|
|
3
|
+
|
|
4
|
+
# build output
|
|
5
|
+
dist/
|
|
6
|
+
|
|
7
|
+
# TypeScript cache
|
|
8
|
+
*.tsbuildinfo
|
|
9
|
+
|
|
10
|
+
# local env files
|
|
11
|
+
.env
|
|
12
|
+
.env.*
|
|
13
|
+
|
|
14
|
+
# logs
|
|
15
|
+
npm-debug.log*
|
|
16
|
+
yarn-debug.log*
|
|
17
|
+
yarn-error.log*
|
|
18
|
+
pnpm-debug.log*
|
|
19
|
+
|
|
20
|
+
# OS files
|
|
21
|
+
.DS_Store
|
|
22
|
+
Thumbs.db
|
|
23
|
+
|
|
24
|
+
# IDEs
|
|
25
|
+
.vscode/
|
|
26
|
+
.idea/
|
|
27
|
+
|
|
28
|
+
# test coverage
|
|
29
|
+
coverage/
|
|
30
|
+
.nyc_output/
|
|
31
|
+
|
|
32
|
+
# misc
|
|
33
|
+
*.log
|
|
34
|
+
*.swp
|
|
35
|
+
*.swo
|
|
36
|
+
|
|
37
|
+
# Rollup cache
|
|
38
|
+
.rollup.cache/
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import client from './client';
|
|
2
|
+
const registeredServices = new Set();
|
|
3
|
+
export async function registerService(name, id, port, address = 'localhost') {
|
|
4
|
+
if (registeredServices.has(id)) {
|
|
5
|
+
console.log(`⚠️ Service "${id}" is already registered.`);
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
await client.agent.service.register({
|
|
9
|
+
name,
|
|
10
|
+
id,
|
|
11
|
+
address,
|
|
12
|
+
port,
|
|
13
|
+
check: {
|
|
14
|
+
name: `${name}-check`,
|
|
15
|
+
http: `http://${address}:${port}/health`,
|
|
16
|
+
interval: '10s',
|
|
17
|
+
timeout: '5s',
|
|
18
|
+
deregistercriticalserviceafter: '1m',
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
registeredServices.add(id);
|
|
22
|
+
console.log(`✅ Service "${id}" registered successfully.`);
|
|
23
|
+
}
|
|
24
|
+
export async function deregisterService(id) {
|
|
25
|
+
if (!registeredServices.has(id)) {
|
|
26
|
+
console.log(`⚠️ Service "${id}" is not registered.`);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
await client.agent.service.deregister(id);
|
|
30
|
+
registeredServices.delete(id);
|
|
31
|
+
console.log(`🛑 Service "${id}" deregistered successfully.`);
|
|
32
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import client from './client';
|
|
2
|
+
export async function listServices() {
|
|
3
|
+
const services = await client.agent.service.list();
|
|
4
|
+
return Object.values(services);
|
|
5
|
+
}
|
|
6
|
+
export async function getServiceInstances(serviceName) {
|
|
7
|
+
const services = await client.catalog.service.nodes(serviceName);
|
|
8
|
+
return services;
|
|
9
|
+
}
|
|
10
|
+
export async function getRandomServiceInstance(serviceName) {
|
|
11
|
+
const instances = await getServiceInstances(serviceName);
|
|
12
|
+
if (!instances.length)
|
|
13
|
+
throw new Error(`Service "${serviceName}" not found`);
|
|
14
|
+
const randomIndex = Math.floor(Math.random() * instances.length);
|
|
15
|
+
return instances[randomIndex];
|
|
16
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { getRandomServiceInstance } from './discovery';
|
|
3
|
+
export async function callService(serviceName, path = '/hello') {
|
|
4
|
+
const instance = await getRandomServiceInstance(serviceName);
|
|
5
|
+
const url = `http://${instance.ServiceAddress}:${instance.ServicePort}${path}`;
|
|
6
|
+
const response = await axios.get(url);
|
|
7
|
+
return response.data;
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|