Demand background

  • The middle tier appeals to the registry and the API gateway

Decision-making basis

Language decision

  • There are three major categories of selection (JAVA,GO, NodeJS)
  • JAVA community to improve the scene support rich, disadvantage front-end team language bottleneck
  • GO performance advantages, cross-platform inherent advantages, disadvantages Team fit (should have been the best choice)
  • Nodejs team language compatibility, disadvantages Poor performance, low security, environment dependence (still selected)

Framework to choose

  • Frame selection Express (50K + STAR) and Egg.js (10K + STAR)
  • The Express community and solution details of strengths, weaknesses, and corporate scenario support all had to be maintained from scratch, and I had implemented a similar EPress template myself before looking at Egg. Some scenes are still missing.
  • Egg.js has the advantages of high degree of standardization, scene coverage is more complete than personal imagination, weak community and solution support is not very friendly, problems are difficult to solve and locate, it is not strictly out of the box, the expansion ability is not strong.
  • Egg.js was chosen from the perspective of time cost and team specification

Project details

Scene: the support

  • Logging, process daemons, directory planning, and configuration schemes are largely out of the box

specification

  • Code specification
  • Git specification
{
    "lint-staged:js": "npm run lint -- --fix && npm run lint:style"."lint:prettier": "prettier --check \"**/*\" --end-of-line auto"."lint:style": ""."prettier": "prettier -c --write \"**/*\""  
}, 
 "husky": {   
   "hooks": {      
     "pre-commit": "npm run lint-staged"."commit-msg": "commitlint -e $HUSKY_GIT_PARAMS"}},"lint-staged": {    
   "**/*.{js,jsx,ts,tsx}": "npm run lint-staged:js"."**/*.{js,jsx,tsx,ts,md,json}":
     [      "prettier --write"]}Copy the code
  • Addressing specification issues using Husky and Lint-staged Precautions Version 9.1.2 @commitlint/ CLI has a bug

Error cannot be submitted, you can select the version below. Commitlint configuration information is as follows

  module.exports = {  extends: ['@commitlint/config-conventional']};Copy the code

Middleware loading order (it should be official that the built-in middleware order causes some feature bugs)

  • Reverse proxy middleware is required prior to bodyParser
  • Graphql middleware needs bodyParser after that
  • Need to customize the startup app.js
/ * * @ Description: custom startup * @ Author: wen-zhou wu * @ making: http://gitlab.yzf.net/wuwenzhou * @ the Date: 2020-06-28 13:38:19 * @Lasteditors: Wu Wenzhou * @LasteditTime: 2020-08-24 22:08:00 */
 'use strict';
  const Consul = require('./app/utils/consul');
  class AppBootHook {  
    constructor(app) {    
      this.app = app;  
   }  
   configWillLoad() {    
   // The config file has been read and merged, but has not yet taken effect
   // This is the last time for the application layer to modify the configuration
   // Load the direction proxy service before bodyParser
     const statusIdx = this.app.config.coreMiddleware.indexOf('bodyParser');    
     this.app.config.coreMiddleware.splice(statusIdx, 0.'proxy');    
     this.app.config.coreMiddleware.splice(statusIdx + 2.0.'graphql');  
   }  
   async didLoad() {    
   // All configurations have been loaded
   // It can be used to load customized application files and start customized services
   // Example: Creating a custom application
   }  
   async willReady() {    
     // All plug-ins are started, but the application is not ready yet
     // You can perform some operations, such as data initialization, to start the application only after these operations succeed
     // For example, load data from database into memory cache
   }  
   async didReady() {    
     // The application has been started}}module.exports = AppBootHook;
Copy the code

Docker container,

  • Docker is not only a matter of operation and maintenance, but also a closed loop of the whole process should be considered in the development stage. Environmental issues are really important in the process of cross-team cooperation. The benefits go beyond that, of course.
  • Step 1 Install docker(ignore)
  • The second step is to add a Dockerfile to the project
# set base mirror, if there is no local mirror, ENV TZ=Asia/ShanghaiRUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime&& echo $TZ >/etc/RUN mkdir -p /usr/src/node-app/server # Copy package.json file to working directory (annotated flow will result in dependency without sending download) #!! Important: Package. json needs to be added separately. # Docker builds the image layer by layer. Only when this layer changes, the corresponding layer will be rebuilt. If package.json is added to the image with the source code, the NPM module will need to be reinstalled every time the source code is modified. # So, the correct order is: add package.json; Install the NPM module. Add source code. /usr/ SRC /node-app/server/package.json /usr/ SRC /node-app/server/package`RUN npm i`. /usr/ SRC /node-app/server /usr/ SRC /node-app/server /usr/ SRC /node-app/server`RUN npm i`. RUN NPM I --registry= XXX # EXPOSE container port7001CMD NPM run dockerCopy the code
  • Step 3 Add.dockerignore(note the previous ones)
# Logs
logs
npm-debug.log*
# Coverage directory used by tools like istanbul
coverage
# Dependency directories
node_modules
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
.idea
.node_modules
.vscode
run
typings
Copy the code
  • Json add run to remove –daemon –title(optional)
"docker": "egg-scripts start".Copy the code
  • Step 5 Start the Docker process
Docker run -d --name egg -p9002:7001 egg 
Copy the code
  • Check whether the command is executed successfully
Docker ps # whether the generated container existsCopy the code
  • If no, the startup fails. Check startup logs
Docker logs container IDCopy the code
  • Native IP +9002 to access egg docker service
  • Other Key Commands
Docker images docker images rmI docker images RMI docker images RMI docker images RMI docker images RMI docker images RMI docker images RMI docker images RMI docker images RMI docker imagesCopy the code

The end

And the flower