1.0.5 • Published 3 years ago
stone-etcd v1.0.5
使用方法
$ npm install stone-etcd -S
引入模块
在configuration.ts中
import { App, Configuration } from '@midwayjs/decorator';
import { ILifeCycle } from '@midwayjs/core';
import { Application } from 'egg';
import { join } from 'path';
import * as etcd from 'stone-etcd';
@Configuration({
imports: [etcd],
importConfigs: [join(__dirname, './config')],
})
export class ContainerLifeCycle implements ILifeCycle {
@App()
app: Application;
async onReady() {
}
}
配置
在config.default.ts中配置:
export const etcd = {
hosts: "http://127.0.0.1:2379"
}
调用方法
import { Controller, Get, Inject, Post, Provide } from "@midwayjs/decorator";
import { BaseController } from "../common/baseController";
import { EtcdService } from 'stone-etcd'
@Provide()
@Controller('/api/etcd')
export class EtcdController extends BaseController {
@Inject()
ectdService: EtcdService;
/**
* 读取etcd配置
* @author: stone-jin
* @date: 2021-11-12 23:00:03
*/
@Get("/config")
async getConfig() {
let res = await this.ectdService.get(this.ctx.query.key).string();
return this.ok(res);
}
/**
* 设置etcd配置
* @author: stone-jin
* @date: 2021-11-12 23:00:11
*/
@Post("/config")
async setConfig() {
let { key, value } = this.ctx.request.body as any;
let res = await this.ectdService.put(key, value);
return this.ok(res);
}
}