1.0.141 • Published 3 years ago

txk-mvc v1.0.141

Weekly downloads
138
License
ISC
Repository
github
Last release
3 years ago

txk-mvc 一个基于NodeJs的MVC微服务框架

定义

txk-mvc是一个基于NodeJs的微服务MVC框架。 我的名字全拼是tangxuke,因此使用txk-mvc作为框架名字。

设计思想

txk-mvc框架用于快速开发一个具有基本MVC三层架构的微服务,利用此框架开发出来的微服务,可独立运行服务,也可以无缝注册到以此框架开发的微服务注册中心。

如何使用(以下例子将示范如何利用txk-mvc开发一个微服务)

首先,创建一个目录,以myapp为例

mkdir myapp
cd myapp

初始化npm

npm init

保持默认选项或者根据需要修改即可。

引入txk-mvc框架

npm i txk-mvc -s

如果之前使用了txk-mvc低版本,必须升级到最新的版本

npm update txk-mvc

txk-mvc包含了以下基础的类库,用户必须继承这些基类并实现其中一些配置,利用这些类可以方便的创建自己的微服务:

BaseController: 控制器原型
BaseRequest:    请求验证原型
BaseService:    服务原型
BaseValidator:  验证器原型
AllValidators:  内置几个验证器(必填、不能为空、文本长度、正则表达式等几个验证器)
BaseRepository: 数据仓库原型
MySQL:          封装Mysql原型(只有query一个方法)
Redis:          封装Redis原型(高频使用的redis命令封装,如get、set等命令)
BaseApplication:微服务应用原型,由这个对象服务入口,从Koa继承
BaseRegister:   注册中心注册器(将微服务注册到注册中心,或者调用任何注册中心的微服务功能)
MicroRepository:基于微服务的数据仓库原型,调用的是微服务的方法,而不是实体mysql或redis的物理命令

示范(一个提供mysql功能的微服务)

app.js

const { MyApplication } = require('txk-service')
const config = require('./config')
const router = require('./router')

class MySqlApplocation extends MyApplication {
    getName() {
        return config.app_name
    }
    getPort() {
        return config.app_port
    }
    getDesc() {
        return config.app_desc
    }
    getServiceUrl() {
        return config.service_url
    }
}

const app = new MySqlApplocation(router)
app.listen()

说明:txk-service是对txk-mvc的一个默认封装,包括一个注册器和一个应用对象

运行的效果:

node app.js

生产环境使用:

pm2 start app.js -i max
PS D:\GitHub\stall\micro-services\mysql-service-read> node app.js
mysql-stall-read stall mysql 微服务(主读) 正在监听端口 20101
本地测试地址: http://localhost:20101
公开访问地址: https://xxx.cn/service/mysql/stall/read
已经登记到注册中心
已经更新微服务列表
最新微服务列表如下:
-----------------------------------------
[ { app: 'mysql-stall-master',
    url: 'https://xxx.cn/service/mysql/stall/write' },
  { app: 'mysql-stall-write',
    url: 'https://xxx.cn/service/mysql/stall/write' },
  { app: 'redis-master',
    url: 'https://xxx.cn/service/redis/write' },
  { app: 'redis-slave',
    url: 'https://xxx.cn/service/redis/read' },
  { app: 'mysql-stall-read',
    url: 'https://xxx.cn/service/mysql/stall/read' } ]
-----------------------------------------
注册成功!

config.js 配置文件

module.exports = {
    mysql_host: '127.0.0.1',
    mysql_port: 3306,
    mysql_user: 'root',
    mysql_password: '123456',
    mysql_database: 'stall',
    app_port: 20101,
    app_name: 'mysql-stall-read',
    app_desc: 'stall mysql 微服务(主读)',
    service_url: 'https://xxx.cn/service/mysql/stall/read'
}

router.js 路由文件

const Router = require('koa-router')
const Controller = require('./controller')

module.exports = new Router()
    .post('/query', ctx => new Controller(ctx).query())

request.js 请求验证器

const { BaseRequest, AllValidators } = require('txk-mvc')

class Request extends BaseRequest {
    query() {
        return this.use('body', 'sql', new AllValidators.NotEmptyValidator()).check()
    }
}

module.exports = Request

请求验证器主要由use方法和check方法。 use方法用于加载验证器,可通过链式调用加载多个验证器。 check方法用于返回验证结果给到控制器,返回格式:

{
    result:boolean,
    code:number,
    msg:string,
    data:{
        query:any,
        body:any,      
        params:any,
        headers:any
    }
}

service.js 服务对象

const { BaseService } = require('txk-mvc')
const MySql = require('./mysql')     //对mysql原型的默认封装

class MySqlService extends BaseService {
    query() {
        return new MySql().query(this.request.body.sql, this.request.body.params)
    }
}

module.exports = MySqlService

mysql.js 对mysql基类的进行具体的配置

const { MySQL } = require('txk-mvc')
const config = require('./config')

class DefaultMySql extends MySQL {
    getHost() {
        return config.mysql_host
    }
    getPort() {
        return config.mysql_port
    }
    getUser() {
        return config.mysql_user
    }
    getPassword() {
        return config.mysql_password
    }
    getDatabase() {
        return config.mysql_database
    }
}

module.exports = DefaultMySql

controller.js 控制器

const { BaseController } = require('txk-mvc')
const Request = require('./request')
const Service = require('./service')

class Controller extends BaseController {
    async query() {
        let request = await new Request(this.ctx).query()
        if (!request.result) {
            return this.error(request.code, request.msg)
        }

        let result = await new Service(request.data).query()

        return this.success(result)
    }
}

module.exports = Controller

调用微服务测试:

a.js

const { MyRegister } = require('txk-service')
new MyRegister().getService('mysql-stall-read', 'query', {
    sql: 'select * from user'
}).then(value => {
    console.log(value)
}).catch(reason => {
    console.log(reason.message)
})

输出:

PS D:\GitHub\stall\api> node a.js
[ { id: 7,
    openid: 'oGNfy5Bit3kugmizUR2rjvvloL_Y',
    nickName: '唐旭克',
    avatarUrl:
     'https://wx.qlogo.cn/mmopen/vi_32/4FYsd8bWiaR9FfnQ950s9ViaSjsCXKpvkpEgESjfMebUL7ibvqhDguttQsKd255fNLYiaV4gKTXHOs4AbN8eKcNIuA/132',
    userid: '',
    city: 'Dongguan',
    province: 'Guangdong',
    country: 'China',
    gender: 1,
    language: 'zh_CN' } ]
PS D:\GitHub\stall\api>

参数验证不通过的示范: a.js

const { MyRegister } = require('txk-service')
new MyRegister().getService('mysql-stall-read', 'query', {
    sql1: 'select * from user'     //参数错误
}).then(value => {
    console.log(value)
}).catch(reason => {
    console.log(reason)
})

输出:

PS D:\GitHub\stall\api> node a.js
{ code: 1001, msg: 'sql 字段内容为空!' }
PS D:\GitHub\stall\api> 

txk-service源码,目的是设置项目公共的注册中心地址,并设置应用类的默认注册中心

const { BaseRegister, BaseApplication } = require('txk-mvc')

class MyRegister extends BaseRegister {
    getMasterRedis() {
        return 'https://xxx.cn/service/redis/write'
    }
    getSlaveRedis() {
        return 'https://xxx.cn/service/redis/write'
    }
}

class MyApplication extends BaseApplication {
    getRegister() {
        return new MyRegister()
    }
}

module.exports = {
    MyRegister, MyApplication
}
1.0.141

3 years ago

1.0.140

3 years ago

1.0.139

3 years ago

1.0.138

3 years ago

1.0.136

3 years ago

1.0.137

3 years ago

1.0.135

3 years ago

1.0.134

3 years ago

1.0.133

3 years ago

1.0.132

3 years ago

1.0.131

3 years ago

1.0.130

3 years ago

1.0.129

3 years ago

1.0.128

3 years ago

1.0.127

3 years ago

1.0.126

3 years ago

1.0.125

3 years ago

1.0.124

3 years ago

1.0.123

3 years ago

1.0.122

3 years ago

1.0.121

3 years ago

1.0.120

3 years ago

1.0.119

3 years ago

1.0.118

3 years ago

1.0.117

3 years ago

1.0.114

3 years ago

1.0.116

3 years ago

1.0.115

3 years ago

1.0.113

3 years ago

1.0.112

3 years ago

1.0.111

3 years ago

1.0.110

3 years ago

1.0.109

4 years ago

1.0.107

4 years ago

1.0.106

4 years ago

1.0.108

4 years ago

1.0.103

4 years ago

1.0.105

4 years ago

1.0.104

4 years ago

1.0.101

4 years ago

1.0.100

4 years ago

1.0.102

4 years ago

1.0.99

4 years ago

1.0.98

4 years ago

1.0.95

4 years ago

1.0.97

4 years ago

1.0.96

4 years ago

1.0.94

4 years ago

1.0.93

4 years ago

1.0.92

4 years ago

1.0.91

4 years ago

1.0.90

4 years ago

1.0.84

4 years ago

1.0.88

4 years ago

1.0.87

4 years ago

1.0.86

4 years ago

1.0.85

4 years ago

1.0.89

4 years ago

1.0.83

4 years ago

1.0.82

4 years ago

1.0.81

4 years ago

1.0.80

4 years ago

1.0.79

4 years ago

1.0.77

4 years ago

1.0.78

4 years ago

1.0.66

4 years ago

1.0.65

4 years ago

1.0.69

4 years ago

1.0.68

4 years ago

1.0.67

4 years ago

1.0.73

4 years ago

1.0.72

4 years ago

1.0.71

4 years ago

1.0.70

4 years ago

1.0.76

4 years ago

1.0.75

4 years ago

1.0.74

4 years ago

1.0.64

4 years ago

1.0.63

4 years ago

1.0.62

4 years ago

1.0.61

4 years ago

1.0.60

4 years ago

1.0.59

4 years ago

1.0.58

4 years ago

1.0.55

4 years ago

1.0.57

4 years ago

1.0.56

4 years ago

1.0.51

4 years ago

1.0.54

4 years ago

1.0.53

4 years ago

1.0.52

4 years ago

1.0.50

4 years ago

1.0.44

4 years ago

1.0.48

4 years ago

1.0.47

4 years ago

1.0.46

4 years ago

1.0.45

4 years ago

1.0.49

4 years ago

1.0.43

4 years ago

1.0.42

4 years ago

1.0.41

4 years ago

1.0.40

4 years ago

1.0.39

4 years ago

1.0.38

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.35

4 years ago

1.0.33

4 years ago

1.0.32

4 years ago

1.0.34

4 years ago

1.0.31

4 years ago

1.0.30

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.27

4 years ago

1.0.26

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.22

4 years ago

1.0.20

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago