Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { scope, ScopeEnum, provide, init, plugin } from 'midway';
const dataId = 'acm.test.1';
const group = 'DEFAULT_GROUP';
@provide()
@scope(ScopeEnum.Singleton)
export class ACMService {
acmText;
@plugin()
acm;
@init()
async init() {
// 放在这里是因为设置成了单例,全局只会有一份实例
const str = `example_test_${Math.random()}_${Date.now()}`;
await this.acm.publishSingle(dataId, group, str);
this.acm.subscribe({
dataId,
group
}, (data) => {
console.log('get data from acm', data);
this.acmText = data;
});
this.acmText = await this.acm.getConfig(dataId, group);
}
const NacosNamingClient = require('nacos').NacosNamingClient;
@provide()
@scope(ScopeEnum.Singleton)
export class NamingService {
nacosClient;
@config('nacosNaming')
nacosConfig;
@logger()
logger;
@init()
async init() {
this.nacosClient = new NacosNamingClient({
...this.nacosConfig,
logger: this.logger,
namespace: 'public',
});
await this.nacosClient.ready();
// 这里的服务名请从 nacos 服务上找
this.nacosClient.subscribe('nacos.test.3', hosts => {
console.log('-----------naming result start --------------');
console.log(hosts);
console.log('-----------naming result end--------------');
});
}
}
export interface IDB {
sequelize: Sequelize;
options: IDBOptions;
}
@scope(ScopeEnum.Singleton)
@async()
@provide('DB')
export default class DB implements IDB {
public sequelize: Sequelize;
@config('sequelize')
public options: IDBOptions;
@init()
public connect() {
this.sequelize = new Sequelize(
this.options.database,
this.options.username,
this.options.password,
{
dialect: this.options.dialect,
host: this.options.host,
port: this.options.port,
timezone: '+08:00',
logging: false,
},
);
}
}