This commit is contained in:
sx1989827 2021-06-03 10:20:53 +08:00
parent d5c8e1e6e5
commit 5e107717c3
10 changed files with 75 additions and 11 deletions

View File

@ -1,7 +1,7 @@
import { Redis } from "../redis";
import {getRedisInstance} from "../../util/global"
class RedisBaseKey<T> {
protected redis:InstanceType<typeof Redis>=(<any>global).redis
protected redis:InstanceType<typeof Redis>=getRedisInstance()
protected type:T
protected name:string
constructor(name:string,type:T){

View File

@ -1,5 +1,6 @@
import { IServer_Common_Config_Redis } from './../types/config';
import * as IORedis from "ioredis"
export var g_redis:Redis
export class Redis {
private redis:InstanceType<typeof IORedis>
constructor(info:IServer_Common_Config_Redis){
@ -9,6 +10,7 @@ export class Redis {
password:info.password,
db:info.db
})
g_redis=this;
}
async get<T>(key:string,type:T):Promise<T> {
let ret=await this.redis.get(key)

View File

@ -0,0 +1,42 @@
import "reflect-metadata"
import {Connection, createConnection} from "typeorm"
import * as fs from "fs-extra"
import * as path from "path"
export var g_mysqlConnection:Connection
export var g_nosqlConnection:Connection
export async function createMysqlConnection(ip:string,port:number,username:string,password:string,database:string){
let files=await fs.readdir(path.join(__dirname,"./model/mysql"))
let arrCls=[]
for(let file of files){
let filePath=path.join(__dirname,"./model/mysql",file)
let cls=await import(filePath)
arrCls.push(cls)
}
let con=await createConnection({
type:"mongodb",
host:ip,
port:port,
username:username,
password:password,
database:database,
entities:arrCls,
synchronize:true,
logging:false
})
g_mysqlConnection=con;
return con;
}
export async function createNosqlConnection(ip:string,port:number,password:string,database:string) {
const connection = await createConnection({
type: "mongodb",
host: ip,
port: port,
database: database,
password:password,
synchronize:true,
logging:false
});
g_nosqlConnection=connection
return connection
}

View File

@ -42,7 +42,7 @@ export class Rpc{
{
let objRedis=REDIS_GATEWAY.instance(name)
let ret=await objRedis.get()
return ret[0]
return ret?ret[0]:null
}
}
@ -54,6 +54,11 @@ export function RPCSend(rpcName:EServer_Common_Config_Services){
if(!g_Socket[rpcName] || !g_Socket[rpcName].connected)
{
let instance=await Rpc.validInstance(rpcName)
if(!instance)
{
resolve(null)
return
}
let socket=ioClient.io(`ws://${instance.ip}:${instance.port}`)
g_Socket[rpcName]=socket
socket.on("connect",()=>{

View File

@ -0,0 +1,13 @@
import { g_mysqlConnection, g_nosqlConnection } from './../db/init';
import {g_redis} from "../cache/redis"
export function getRedisInstance(){
return g_redis;
}
export function getMysqlInstance(){
return g_mysqlConnection;
}
export function getNosqlInstance(){
return g_nosqlConnection;
}

View File

@ -1,7 +1,8 @@
import { Redis } from '../../common/cache/redis';
import single from '../util/single';
import Nacos from '../../common/nacos/nacos';
import {REDIS_GATEWAY} from "../../common/cache/keys/gateway"
import { getRedisInstance } from '../../common/util/global';
export class CacheService {
private redis:InstanceType<typeof Redis>
private timer:NodeJS.Timeout
@ -10,7 +11,7 @@ export class CacheService {
private nacos:InstanceType<typeof Nacos>
constructor(nacos:InstanceType<typeof Nacos>){
this.nacos=nacos
this.redis=single.redis
this.redis=getRedisInstance()
}
start(){
this.handle()

View File

@ -9,8 +9,7 @@ export async function init() {
let nacos=new Nacos<IServer_GateWay_Config>(process.env.nacosUrl,Number(process.env.nacosPort),process.env.nacosName,process.env.nacosId)
await nacos.init()
single.config=nacos.configInfo
single.redis=new Redis(nacos.redisInfo);
(<any>global).redis=single.redis
new Redis(nacos.redisInfo);
let cacheService=new CacheService(nacos)
cacheService.start()
}

View File

@ -21,12 +21,16 @@
"koa-static": "^5.0.0",
"koa-static-server": "^1.4.0",
"koa2-formidable": "^1.0.2",
"mongodb": "^3.6.4",
"mysql2": "^2.2.5",
"nacos": "^2.0.1",
"socket.io": "^3.1.1",
"socket.io-client": "^3.1.1",
"typeorm": "^0.2.31",
"yargs": "^16.2.0"
},
"devDependencies": {
"@types/fs-extra": "^9.0.7",
"@types/ioredis": "^4.19.4",
"@types/koa": "^2.11.8",
"@types/koa-bodyparser": "^4.3.0",

View File

@ -1,7 +1,6 @@
import { Redis } from '../../common/cache/redis';
import {IServer_User_Config} from "../types/config"
var obj={
config:<IServer_User_Config>{},
redis:<InstanceType<typeof Redis>>{}
config:<IServer_User_Config>{}
};
export default obj

View File

@ -10,6 +10,5 @@ export async function init() {
let nacos=new Nacos<IServer_User_Config>(process.env.nacosUrl,Number(process.env.nacosPort),process.env.nacosName,process.env.nacosId)
await nacos.init()
single.config=nacos.configInfo
single.redis=new Redis(nacos.redisInfo);
(<any>global).redis=single.redis
new Redis(nacos.redisInfo);
}