spring boot memo - Nginx, NPM, EasyExcel

Nginx

Nginx is an http server (or web server). The server is divided into web server and application server. The web server (http server) is responsible for processing and responding to user requests, such as Apache, IIS and nginx. The application server is the server that stores and runs the system program and is responsible for processing the business logic in the program, such as Tomcat, Weblogic and Jboss (now most application servers also include the function of web server).

effect

There are many micro services in the back end of the project, so there are many interface addresses. You can use nginx reverse proxy to distribute different api paths to different api servers. The main functions are reverse agent, load balancing, dynamic and static separation

Reverse proxy


Nginx acts as a proxy in the graph. The three client s on the left can't feel the existence of the three server s when they get the content from nginx. The browser or other terminal requests nginx. The browser or other terminal finally gets the content he wants, but it doesn't know where to get it

Forward proxy


Nginx acts as a proxy in the graph. The three clients on the left obtain content from nginx when requesting. The server cannot feel the existence of the three clients.

to configure

http {
    server {
        listen       81;
        ......
    },
    
    ......
	server {

		listen 9001;
		server_name localhost;

		location ~ /eduservice/ {           
			 proxy_pass http://localhost:8001;
		}
		
		location ~ /eduoss/ {   
			 proxy_pass http://localhost:8002;
		}
	}
}


load balancing

In a server cluster, Nginx can distribute the received client requests "evenly" (strictly speaking, it is not necessarily uniform, but by setting weights) to all servers in the cluster. This is called load balancing.

to configure

# Load balancing: setting domain
upstream domain {
    server localhost:8000;
    server localhost:8001;
}
server {  
        listen       8080;        
        server_name  localhost;

        location / {
            # root   html; # Nginx default
            # index  index.html index.htm;
            
            proxy_pass http://domain; #  In load balancing configuration, requests will be evenly distributed to ports 8000 and 8001
            proxy_set_header Host $host:$server_port;
        }
}

Sometimes the page will visit port 801:8000 of local host.

Dynamic and static separation

In fact, the dynamic and static separation is that the Nginx server divides the received requests into dynamic requests and static requests

The static request directly fetches the corresponding resources from the root directory path set by the nginx server, and the dynamic request is forwarded to the real background for processing.

to configure

server {  
        listen       8080;        
        server_name  localhost;

        location / {
            root   html; # Nginx default
            index  index.html index.htm;
        }
        
        # Static configuration. All static requests are forwarded to nginx for processing. The storage directory is my project
        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {
            root /usr/local/var/www/my-project; # The root directory to which the static request is proxied
        }
        
        # If the dynamic request matches the one with path 'node', it will be forwarded to port 8002 for processing
        location /node/ {  
            proxy_pass http://localhost:8002; #  Act as service agent
        }
}

Access static files: such as index HTML returns the static file. If you visit 127.0.0.1:8002 or 127.0.0.1:8080/node /, you will jump to http://localhost:8002
Nginx getting started guide that you can understand even the front end
Take you to understand Nginx in 8 minutes

npm

The full name of npm is Node Package Manager js package management tool, you can easily download js library through npm
Download node JS, after installation, enter npm -v for the command number, and the version number can be viewed, which means the installation is successful

initialization

#Create an empty folder and enter the folder at the command prompt to execute command initialization
npm init
#Enter relevant information according to the prompt. If the default value is used, press enter directly.
#Name: project name
#Version: project version number
#Description: item description
#keywords: {Array} keyword, which is convenient for users to search our project
#Finally, a package. XML file will be generated JSON file, which is the configuration file of the package, is equivalent to maven's POM xml
#We can also modify it as needed later.
#If you want to directly generate package JSON file, you can use the command
npm init -y

Image modification of foreign image: Taobao is slow http://npm.taobao.org/

#After the following configuration, all NPM installations will be downloaded through the image address of Taobao in the future
npm config set registry https://registry.npm.taobao.org 

#View npm configuration information
npm config list

npm install:

#Use npm install to install the latest version of the dependent package,
#Module installation location: project directory \ node_modules
#The installation will automatically add package lock. In the project directory JSON file, which helps lock the version of the installation package
#At the same time, package In the JSON file, the dependency package will be added to the dependencies node, similar to < dependencies > in maven
npm install jquery


#npm managed projects generally do not carry nodes during backup and transmission_ Modules folder
npm install #According to package The configuration download dependency in JSON initializes the project


#If you want to specify a specific version during installation
npm install jquery@2.1.x


#devDependencies node: the dependency package during development, which is not included when the project is packaged into the production environment
#Use the - D parameter to add dependencies to the devDependencies node
npm install --save-dev eslint
#or
npm install -D eslint


#Global installation
#Node.js location of npm packages and tools installed globally: user directory \ AppData\Roaming\npm\node_modules
#Some command-line tools often use global installation
npm install -g webpack

Other commands

#Update package (update to the latest version)
npm update Package name
#Global Update
npm update -g Package name

#Uninstall package
npm uninstall Package name
#Global uninstall
npm uninstall -g Package name

EasyExcel

Reference (for the following contents, please refer to the online education project of Silicon Valley)
alibaba/easyexcel
Famous frameworks for Java parsing and generating excel include Apache poi and jxl. However, they all have a serious problem that they consume very much memory. POI has a set of Sax mode API, which can solve some memory overflow problems to a certain extent, but POI still has some defects. For example, the decompression and post decompression storage of version 07 excel are completed in memory, and the memory consumption is still very large. easyexcel rewrites POI's analysis of the 07 version of Excel. It can use POI sax for the original 3M excel. It still needs about 100M memory to be reduced to a few m, and no memory overflow will occur for the larger excel. The 03 version relies on the Sax mode of POI. The model conversion is encapsulated in the upper layer, which makes the user more simple and convenient.

  • EasyExcel is an open source excel processing framework of Alibaba, which is famous for its simple use and memory saving. The main reason why EasyExcel can greatly reduce the memory consumption is that it does not load all the file data into the memory at one time when parsing excel, but reads the data line by line from the disk and parses it one by one.
  • EasyExcel adopts the line by line parsing mode, and notifies the processing (analysis event listener) of the parsing results of one line in the mode of observer.

write

pom introduces dependency

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>2.1.1</version>
    </dependency>
</dependencies>

Create entity class

//Set header and added data fields
public class DemoData {
    //Set header name
    @ExcelProperty("Student number")
    private int sno;
    
	//Set header name
    @ExcelProperty("Student name")
    private String sname;

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    @Override
    public String toString() {
        return "DemoData{" +
                "sno=" + sno +
                ", sname='" + sname + '\'' +
                '}';
    }
}

Create a method loop to set the data to be added to Excel

//Loop to set the data to be added, and finally encapsulate it into the list collection
private static List<DemoData> data() {
    List<DemoData> list = new ArrayList<DemoData>();
    for (int i = 0; i < 10; i++) {
        DemoData data = new DemoData();
        data.setSno(i);
        data.setSname("Zhang San"+i);
        list.add(data);
    }
    return list;
}

Writing method I

public static void main(String[] args) throws Exception {
    // Writing method 1
    String fileName = "F:\\11.xlsx";
    // Here, you need to specify which class to write, and then write to the first sheet with the name of template, and then the file stream will be closed automatically
    // If you want to use 03 here, you can pass in the excel type parameter
    EasyExcel.write(fileName, DemoData.class).sheet("Write method 1").doWrite(data());
}

(writing method 2)

public static void main(String[] args) throws Exception {
    // Write method 2. Method 2 needs to manually close the flow
    String fileName = "F:\\112.xlsx";
    // Here, you need to specify which class to write with
    ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
    WriteSheet writeSheet = EasyExcel.writerSheet("Write method 2").build();
    excelWriter.write(data(), writeSheet);
    ///Don't forget that finish will help close the flow
    excelWriter.finish();
}

read

Create entity class

public class ReadData {
    //Set the properties corresponding to the column
    @ExcelProperty(index = 0)
    private int sid;
    
    //Set the properties corresponding to the column
    @ExcelProperty(index = 1)
    private String sname;

    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    @Override
    public String toString() {
        return "ReadData{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                '}';
    }
}

Create listener for read operation

//Create and read excel listener
public class ExcelListener extends AnalysisEventListener<ReadData> {

    //Create a list collection to encapsulate the final data
    List<ReadData> list = new ArrayList<ReadData>();

    //Read the excle content line by line
    @Override
    public void invoke(ReadData user, AnalysisContext analysisContext) {
       System.out.println("***"+user);
        list.add(user);
    }

    //Reading excel header information
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("Header information:"+headMap);
    }

    //Execute after reading
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }
}

Call to achieve the final read

public static void main(String[] args) throws Exception {

        // Writing method 1:
        String fileName = "F:\\01.xlsx";
        // Here, you need to specify which class to use to read, and then read the first sheet file. The stream will close automatically
        EasyExcel.read(fileName, ReadData.class, new ExcelListener()).sheet().doRead();

        // Writing method 2:
        InputStream in = new BufferedInputStream(new FileInputStream("F:\\01.xlsx"));
        ExcelReader excelReader = EasyExcel.read(in, ReadData.class, new ExcelListener()).build();
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        excelReader.read(readSheet);
        // Don't forget to close it here. Temporary files will be created when reading, and the disk will collapse
        excelReader.finish();
}

Tags: Nginx Spring Boot

Posted by wendymelon on Thu, 19 May 2022 17:48:35 +0300