At present, in the enterprise microservice architecture, the system with Java as the development language and Spring Cloud as the development framework still accounts for the majority of the market, which indirectly leads to the lack of corresponding micro-service implementation practice for front-end developers with Node.js as the main development environment. In this paper, node.js (service) + Nginx (static resource hosting) architecture, so that front-end developers can quickly build applications, “zero intrusion” to obtain the registration and discovery, service governance, monitoring operation and maintenance, configuration change and other micro-service related capabilities, greatly reducing the application access, transformation, operation and maintenance costs.

Author: Cui Kai

Source: Tencent Cloud middleware official account

Cui Kai, tencent cloud CSIG micro service center product architect, distributed, high concurrency e-commerce system development, system architecture design experience, good at ground and implementation of the main micro service architecture technology platform, the current focus to the study of micro service architecture related middleware precipitation in the promotion and best practices, to help enterprises to complete the digital transformation

background

TSF Mesh is a Service framework product constructed by Tencent cloud micro-service platform TSF team following the design concept of Service Mesh and based on CNCF cloud native open source software LSTIo /Envoy.

TSF Mesh provides users with the following values:

  • TSF provides you with one-stop application lifecycle management services. Manage the entire process from application deployment to application running, including creating, deleting, deploying, rolling back, expanding capacity, going offline, starting and stopping applications, and supporting version tracing.

  • TSF provides you with efficient service registry discovery capabilities. Provides a high availability guarantee mechanism, such as second-level service registration discovery and local registration information caching, service instance discovery exception alarm, and cross-AZ disaster recovery.

  • TSF provides you with fine-grained service governance capabilities. Supports multi-level service governance of services and apis. Fine-grained traffic control in the form of labels is implemented to implement functions such as grayscale advertising, nearby routing, fusing traffic limiting, service fault tolerance, and access authentication.

  • TSF provides you with three-dimensional application data operations. Provides tools for monitoring application performance indicators, analyzing distributed call chain, service dependency topology, and log services, helping you efficiently analyze application performance bottlenecks and troubleshoot faults.

This paper will rely on TSF Mesh technology to host Node.js and Nginx as services, and finally verify that Node.js and Nginx can be registered, discovered and successfully invoked as services. The outline architecture diagram is as follows:

The general steps of the whole practice are as follows:

  1. Create and upload node. js images
  2. Nginx image creation and upload
  3. Node.js is deployed on the TSF platform
  4. Deploy Nginx on TSF platform
  5. Node.js and Nginx are accessed separately via IP:PORT/ service name
  6. Node.js and Nginx access each other by service name

Node.js image creation

  1. Prepare the Node.js application code

Wechat public account background send keyword [demo] to obtain the complete demo file.

Configuration files in the Node. Js may refer to the demo build, other language TSF mesh demo sample may refer to: cloud.tencent.com/document/pr…

2. Make node.js + Express application image (please make the image of Node :14 by yourself, WORKDIR can be modified according to your own needs)

The following is an example of a Dockerfile file:

FROM node:14 RUN mkdir -p /usr/src/app WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["./start.sh"] #Copy the code

3. Prepare related configuration files (The stop.sh and cmdline files are not required to run in container mode, but are required for VMS).

The contents of the start.sh file are as follows:

#! /bin/bash already_run=`ps-ef|grep "node"|grep -v grep|wc -l` if [${already_run} -ne 0 ]; then echo "nodejs-john alreadyRunning!!!!" exit -1 fi mkdir -p/opt/tsf/app_config/apis cp/usr/src/app/spec.yaml /opt/tsf/app_config/ cp -r/usr/src/app/apis /opt/tsf/app_config/ cd/usr/src/app/ nohupnode ./bin/www 1>./logs/nodejs-service.log 2>&1Copy the code

The spec.yaml file contains the following contents:

ApiVersion :v1 kind:Application spec: services: -name: nodejs-service # Ports: -targetPort: HTTP2 is currently supported. You can use HTTP2 to deploy gRPC services. /users # health check URL test command: curl http://nodejs-service/usersCopy the code

The apis/nodejs-service.yaml file contains the following contents:

Openapi :3.0.0 info: version: "1.0.0" title: nodejs-service paths: /users: get: responses: '200': description: OK '401': description: Unauthorized '402': description: Forbidden '403': description: Not FoundCopy the code

4. The project catalog is as follows

5. Generate local image according to Dockerfile and upload it to Tencent Cloud image warehouse (note to modify user UID)

CD {Dockerfile directory} Docker pull node:14 docker build -t mesh-nodejs:v1.0Copy the code

Sudo docker login --username={uid} ccr.ccs.tencentyun.com sudo docker tag {id} Ccr.ccs.tencentyun.com/tsf_ uid} {/ nodejs: v1.0 sudo docker push ccr.ccs.tencentyun.com/tsf_ uid} {/ nodejs: v1.0Copy the code

After the push, you can see the image of the push in the mirror repository

Nginx image creation

1. Create an Nginx application image

The following is an example of a Dockerfile file:

FROMnginx:stable-alpine RUN echo"ip_resolve=4" >> /etc/yum.conf RUN/bin/cp /usr/share/zoneinfo/Asia/Shanghai / etc/localtime RUN echo "Asia/Shanghai" > / etc/timezone WORKDIR/usr/share/nginx/HTML COPY. # according to the need to COPY the static resource files to HTML directory RUNchmod +x start.sh stop.sh CMD["./start.sh"] # start the Nginx process with start.sh and generate /opt/ TSF /app_config in the sh script otherwise it will be clearedCopy the code

2. Prepare configuration files

The contents of the start.sh file are as follows:

#! /opt/ TSF /app_config cpspec.yaml /opt/ TSF /app_config/ # do already_run=`ps -ef|grep"nginx"|grep -v grep|wc -l` if [ ${already_run} -eq 0 ]; then nohup nginx -c/etc/nginx/nginx.conf 1>/var/log/nginx/nginx-service.log 2>&1 & fi sleep 10 DoneCopy the code

3. The instance runs properly after the deployment

Deploy the TSF Mesh application

1. Import host resources to the container cluster

2. Create Nginx and Node.js applications

3. Create Nginx and Node.js deployment groups

4. Use the deployment group to deploy the image

Node. Js test

1. Access the Node.js application directly

2. Test API interfaces in service governance

3. Enter the container to check whether the interface can be invoked using the service name

Sudo docker ps # find container IDCopy the code

Sudo docker exec -it cfa4343f4a22 /bin/bash #Copy the code

Docker container internal tests are invoked by the service name

Nginx test

1. Directory structure and test in container (WORKDIR can be modified according to its own needs)

2. Access static resources from the Internet

Out of service

3. The service is online

Curl successfully accessing the Nginx service in the Node.js service container

conclusion

Through the combination of data plane and control plane, TSF Mesh acts as the local L7 layer agent in the form of Sidecar. It runs in the same POD with service and shares the network with POD. It is through this combination that the internal governance, operation and maintenance of the overall microservice architecture is completed.

Although the above tests are only basic connectivity tests, the TSF platform is capable of service governance, operation and maintenance monitoring, construction and deployment, configuration changes and other related capabilities. At the same time, it can be seen that when TSF Mesh is connected to non-Java applications, such as Node.js, PHP, Python, etc., the access cost is very low and the code can achieve “zero invasion”, which is very suitable for microservice transformation of heterogeneous languages and old legacy systems. This is why the Microservices team developed TSF Mesh.

portal

  • For more information about micro services: Tencent micro service platform TSF
  • Feedback exchange your valuable opinion: Tencent cloud middleware q&A community
  • How to design elastic microservice architecture in traffic peak