hydra nodejs 微服务框架简单试用

hydra 是一个以来redis 的nodejs 微服务框架

安装

需要redis,使用docker 进行运行

  • redis
docker run  -d -p 6379:6379 redis
  • 安装yo && hydra-cli
sudo npm install -g yo generator-fwsp-hydra hydra-cli

配置redis 环境

instance 名称随意,具体环境配置即可

hydra-cli config dalong
redisUrl: 127.0.0.1
redisPort: 6379
redisDb: 15

使用脚手架工具,生成demo

yo fwsp-hydra
fwsp-hydra generator v0.3.1 yeoman-generator v3.0.0 yo v2.0.4
? Name of the service (`-service` will be appended automatically) userlogin
? Your full name? dalongrong
? Your email address? 1141591465@qq.com
? Your organization or username? (used to tag docker images) dalongrong
? Host the service runs on?
? Port the service runs on? 0
? What does this service do?
? Does this service need auth? No
? Is this a hydra-express service? Yes
? Set up a view engine? No
? Set up logging? No
? Enable CORS on serverResponses? Yes
? Run npm install? No
   create userlogin-service/specs/test.js
   create userlogin-service/specs/helpers/chai.js
   create userlogin-service/.editorconfig
   create userlogin-service/.eslintrc
   create userlogin-service/.gitattributes
   create userlogin-service/.nvmrc
   create userlogin-service/.gitignore
   create userlogin-service/package.json
   create userlogin-service/README.md
   create userlogin-service/userlogin-service.js
   create userlogin-service/config/sample-config.json
   create userlogin-service/config/config.json
   create userlogin-service/scripts/docker.js
   create userlogin-service/routes/userlogin-v1-routes.js

Done!
'cd userlogin-service' then 'npm install' and 'npm start'

启动&&服务访问

  • 启动
yarn run start

node userlogin-service.js
INFO
{ event: 'start',
message: 'userlogin-service (v.0.0.1) server listening on port 28418' }
INFO
{ event: 'info', message: 'Using environment: development' }
serviceInfo { serviceName: 'userlogin-service',
serviceIP: '10.6.204.19',
servicePort: 28418 }

  • 访问
  • redis 服务注册信息

集成docker

  • build

使用hydra-cli 查看信息

  • 配置redis
hydra-cli config dalong
redisUrl: 127.0.0.1
redisPort: 6379
redisDb: 15
  • 选择instance
hydra-cli use dalong
  • 查看nodes
  • 更多支持的命令
hydra-cli
hydra-cli version 1.5.5
Usage: hydra-cli command [parameters]
See docs at: https://github.com/flywheelsports/hydra-cli

A command line interface for Hydra services

Commands:
  help - this help list
  cfg list serviceName - display a list of config versions
  cfg pull label - download configuration file
  cfg push label filename - update configuration file
  cfg remove label - remove a configuration version
  config instanceName - configure connection to redis
  config list - display current configuration
  use instanceName - name of redis instance to use
  health [serviceName] - display service health
  healthlog serviceName - display service health log
  message create - create a message object
  message send message.json - send a message
  nodes [serviceName] - display service instance nodes
  redis info - display redis info
  refresh node list - refresh list of nodes
  rest path [payload.json] - make an HTTP RESTful call to a service
  routes [serviceName] - display service API routes
  services [serviceName] - display list of services
  shell - display command to open redis shell

说明

meduim 上有一个详细介绍实现细节的文章
https://medium.com/hydramicroservices/building-light-weight-microservices-using-redis-dc5b3bca741

参考资料

https://medium.com/hydramicroservices/building-light-weight-microservices-using-redis-dc5b3bca741

posted on 2018-07-18 16:20  荣锋亮  阅读(2289)  评论(0编辑  收藏  举报

导航