Files
microservices/app/src/utils/consul.ts
2024-09-27 01:14:21 +08:00

31 lines
801 B
TypeScript

import Consul, { ConsulOptions } from "consul";
import Config from "../config";
export class ConsulConf {
consulInstance!: Consul.Consul;
constructor(option: ConsulOptions) {
this.consulInstance = new Consul(option);
// 批量注册各个服务
Config.servicesList.forEach(async v => {
await this.register(v)
})
}
/**
* 服务注册
*/
async register(option: Consul.Agent.Service.RegisterOptions) {
let res = await this.consulInstance.agent.service.register(option);
console.log("res: === :", res);
}
async list() {
return await this.consulInstance.agent.service.list();
}
async deregister(id: string) {
return await this.consulInstance.agent.service.deregister(id);
}
}