Giter VIP home page Giter VIP logo

microservice's Introduction

微服务框架

快速开始

访问:http://localhost:8080/base/user/1001 ,得到响应如下

{
  "code": 0,
  "data": {
    "bar": "hello world"
  }
}

目录结构

microservice
|-- package.json
|-- config // 应用配置
|     |-- app.yaml
|-- src
|     |-- app.js // 入口文件
|     |-- router.js // 路由
|     |-- controller
|     |     |-- user.js
|     |-- service
|     |     |-- foo.js
|     |-- middleware
|     |     |-- error.js
|     |-- lib
|     |     |-- config.js
|     |     |-- cronus.js // 核心依赖

配置

微服务框架启动后,会默认加载config/app.yaml文件

基础配置

serviceName: &serviceName my-service

port: 8080

log:
  name: *serviceName
  level: debug

baseURI: '/base'

中间件

微服务框架基于 Koa 的中间件形式,用户可自行添加所需中间件

cronus.addMiddleware(async (ctx, next) => {
  ctx.body = 'hello world'
  await next()
})

路由

微服务框架约定src/router.js文件用于路由规则配置

定义示例:

module.exports = app => {
  router.get('/user/:id', app.controller.user.info)
}

控制器(Controller)

如何编写 Controller

所有的 Controller 文件都必须放在src/controller目录下,我们可以通过定义 Controller 类的方式来编写代码,所有 Controller 需继承基类

const Controller = require('./base')

module.exports = class UserController extends Controller {
  async info() {
    const { ctx, service } = this
    const ret = await service.foo.query()
    
    ctx.body = {
      code: 0,
      data: ret
    }
  }
}

基类默认提供以下属性:

  • this.ctx:当前请求的上下文 Context 对象
  • this.service:应用定义的 Service,通过它我们可以访问到抽象出的业务层

服务(Service)

与 Controller 类似,所有的 Service 文件必须放在src/service目录下,也需继承基类

foo.js

const Service = require('./base')

module.exports = class FooService extends Service {
  async query() {
    const { service } = this
    const ret = await service.bar.query()
    
    return {
      bar: ret
    }
  }
}

bar.js

const Service = require('./base')

module.exports = class BarService extends Service {
  async query() {
    return 'hello world'
  }
}

基类默认提供以下属性:

  • this.ctx:当前请求的上下文 Context 对象
  • this.service:应用定义的 Service,通过它我们可以访问到抽象出的业务层

内置对象

日志

logger 对象,上面有四个方法(debuginfowarnerror),分别代表打印四个不同级别的日志

const cronus = require('./lib/cronus')
const logger = cronus.getLogger()
logger.debug()
logger.info()
logger.warn()
logger.error()

microservice's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.