npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-seneca-subserver

v1.0.38

Published

基于 Egg.js + Seneca 的微服务模型,子服务插件

Downloads

6

Readme

egg-seneca-subserver

NPM version build status Test coverage David deps Known Vulnerabilities npm download

使用说明

插件以Egg.js为应用框架,以Seneca为服务交互支持,提供微服务方案。 整体结构分为两部分:

  • 网关部分:安装插件 egg-seneca-gateway,用以接收外部请求、被动接受子服务嵌入(发现)、提供子服务间交互;
  • 子服务部分: 安装插件 egg-seneca-subserver,子服务开发与普通的 Egg 应用开发无差异,仅配置和启动时机、其他服务交互方式不同。

启动顺序

  1. 先启动 网关 应用,被动等待子服务接入;
  2. 启动 子服务 部分,网关 会生成相应的对外接口、对其他子服务的交互方式;

使用场景

  • 基于 Egg.js + Seneca 的微服务模型, 子服务部分。
  • 暂不支持 RESTGraphQL 风格

安装

$ npm i egg-seneca-subserver --save

开启插件

// {app_root}/config/plugin.js
exports.senecaSubserver = {
  enable: true,
  package: 'egg-seneca-subserver',
};

详细配置

// {app_root}/config/config.default.js
/**
 * 配置服务器启动项
 */
// 你的服务器定位(必要)
exports.cluster = {
  listen: {
    port: 20980,
    hostname: '127.0.0.1',
    // path: '/var/run/egg.sock',
  },
};
// seneca subserver 配置
exports.senecaSubserver = {
  appid: 'your appid',
  appsecret: 'your appsecret',
  devLog: true, // 是否显示控制台交互日志, 默认为 false
  server: { // 本服务的服务器描述
    name: 'users', // service name
    port: 39091,
    title: '用户服务', // 当前服务的标题文本
    describe: '这个服务用于负责用户相关业务', // 当前服务的描述
  },
  gateway: { // 网关配置
    host: '127.0.0.1',
    port: 50005,
    type: 'tcp',
    version: 1.7, // 注入网关的服务版本号
  },
};

服务注册

  • egg-seneca-subserver会解析Egg应用中的Router集合, 并发布于你的网关(安装了egg-seneca-gateway的应用)。
  • 在网关,会生成相应的web API以便客户端调用。
  • 例如你的路由地址是/findAll,而senecaSubserver中配置的server.name=user
  • 则网关会生成: /user/findAll作为统一调用接口

微服务集视图一览

  • 为方便展示网关可调用服务集合,网关启动后在浏览器输入http://网关主机+端口号/则可以访问微服务集图形界面视图。
  • 注意:集成视图一览的网关需通过git clone https://github.com/Alalabu/sheu-gateway.git your-gateway进行获取, 项目包含了egg-seneca-gateway插件引用以及React + Ant design视图模块, 便于二次修改.
  • 为了配合更好的视图一览效果,您的Egg Router应该定义路由名称,例如:
// {app_root}/app/router.js
module.exports = app => {
  const { router, controller } = app;
  router.get('主页', '/', controller.home.index);
  router.get('查询', '/query', controller.home.query);
  router.post('检查Token', '/checkToken', controller.home.checkToken);
  router.post('登录', '/login', controller.user.login);
  router.post('注册', '/register', controller.user.register);
};

其他服务调用

  • 当您的应用需要调用其他微服务应用时,您应该使用seneca进行tcp模式调用:
// {app_root}/app/service/foo.js
async bar() {
  const { ctx } = this;
  // ctx.app.seneca(服务名, 命令, 参数对象);
  const res = await ctx.app.seneca('users', 'register', { account: 'abc1004' });
  return res;
}

更新日记

1.0.37:

  • 增加对 content-type: text/xml 网关数据发放的支持,在网关部分会将 xml 解析成 js 对象,并将子服务请求体改为 content-type: application/json,将 xml 数据作为请求参数封装于POST请求的 ctx.request.body.$options.xml 中,子服务可以判断该值是否存在并直接进行操作。 1.0.36:
  • 修复了一个由于响应数据中带有长数字的字符串,在 JSON.parse 时发生的数字截断,之后的响应数据将不在自动解析,由调用者自行处理; 1.0.35:
  • 配置添加 senecaSubserver.devLog = true | false, 用以开启或关闭控制台日志, 默认关闭

License

MIT