Springboot Cangqiong takeaway practice: 2. nginx reverse proxy and load balancing configuration + MD5 encryption + Apifox import interface document + Swagger (knife4j version, including common interface annotations)

nginx reverse proxy and load balancing

Observing the project code, we can find
Front-end request address: http://localhost/api/employee/login
Backend interface address: http://localhost:8080/admin/employee/login
Obviously, the two addresses are inconsistent, so how do you request the backend service?
This is a reverse proxy through nginx. The front-end request is forwarded to the back-end through nginx, thereby realizing the mapping between different addresses of the front-end and back-end.

Advantages

  • Improve access speed

    Because nginx itself can cache, if the same interface is accessed and the data is cached, nginx can directly return the data without actually accessing the server, thus improving the access speed.

  • Perform load balancing

    The so-called load balancing means to evenly distribute a large number of requests to each server in the cluster according to the method we specify.

  • Ensure back-end service security

    Use nginx as the entry point for requesting access. After the request reaches nginx, it is forwarded to the specific service, thereby ensuring the security of the back-end service.

nginx reverse proxy configuration

server{
    listen 80;
    server_name localhost;
    
    location /api/{
        proxy_pass http://localhost:8080/admin/; #Reverse proxy
    }
}

The meaning of the above code is: listen to port number 80. When we access an interface such as http://localhost:80/api/…/…, it passes a match such as location /api/ {} and is reflected by nginx. Go to http://localhost:8080/admin/ to proxy.

nginx load balancing configuration

upstream webservers{
    server 192.168.100.128:8080;
    server 192.168.100.129:8080;
}
server{
    listen 80;
    server_name localhost;
    
    location /api/{
        proxy_pass http://webservers/admin;#Load balancing
    }
}

upstream: If the proxy server is a group of servers, we can use the upstream directive to configure the backend server group. The name behind it can be customized, such as webservers here, but it must be consistent from top to bottom.

The meaning of the above code is: listen to port number 80. When we access an interface such as http://localhost:80/api/…/…, it will go to http through a reverse proxy such as location /api/ {}: //webservers/admin, at the same time find a group of servers based on the webservers name, and forward to the specific server according to the set load balancing policy (the default is polling).

nginx load balancing strategy:

Name Description
Polling Default mode
weight Weight mode, the default is 1, the higher the weight, The more client requests are allocated
ip_hash According to the IP allocation method, each visitor can have fixed access to a back-end service
least_conn According to the least connection method, allocate requests to back-end services with fewer connections first
url_hash According to the url allocation method, so that the same url will be allocated to the same backend service
fair According to the response Time mode, services with short response times will be allocated first

Polling:

upstream webservers{
    server 192.168.100.128:8080;
    server 192.168.100.129:8080;
}

weight:

upstream webservers{
    server 192.168.100.128:8080 weight=90;
    server 192.168.100.129:8080 weight=10;
}

ip_hash:

upstream webservers{
    ip_hash;
    server 192.168.100.128:8080;
    server 192.168.100.129:8080;
}

least_conn:

upstream webservers{
    least_conn;
    server 192.168.100.128:8080;
    server 192.168.100.129:8080;
}

url_hash:

upstream webservers{
    hash &request_uri;
    server 192.168.100.128:8080;
    server 192.168.100.129:8080;
}

fair:

upstream webservers{
    server 192.168.100.128:8080;
    server 192.168.100.129:8080;
    fair;
}

MD5 encryption

Modify data in the database

Open the employee table and change the password to 123456

e10adc3949ba59abbe56e057f20f883e

Be careful not to use carriage returns or spaces, just click update.

Use DigestUtils to modify EmployeeServiceImpl

Find the TODO in the project and modify it to the following code:

//TODO md5 encryption is required later and then compared.
        password = DigestUtils.md5DigestAsHex(password.getBytes());

Apifox import interface document

Apifox is a tool used during the design phase to manage and maintain interfaces.

Create two new HTTP projects in Apifox, namely “Cangqiong Takeout Management Terminal” and “Cangqiong Takeaway Client”.
Then ctrl + o opens the import of interface settings. Or open it as follows:

Then select the YApi format, drag and drop the two json documents of the “Project Interface” of day02 in the data to the pages of the two projects for import, and then discover new interfaces in the two projects.

Swagger (knife4j version)

Swagger is a framework used during the development phase to help back-end developers do back-end interface testing.

Import dependencies

<dependency>
   <groupId>com.github.xiaoymin</groupId>
   <artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>

The configuration class adds configuration and sets static resource mapping

com.sky.config.WebMvcConfiguration
Add configuration

/**
     * Generate interface documents through knife4j
     * @return
*/
    @Bean
    public Docket docket() {<!-- -->
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("Cangqiong Takeaway Project Interface Document")
                .version("2.0")
                .description("Cangqiong Takeaway Project Interface Document")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sky.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

com.sky.config.WebMvcConfiguration
Set static resource mapping

/**
     * Set static resource mapping
     * @param registry
*/
protected void addResourceHandlers(ResourceHandlerRegistry registry) {<!-- -->
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}

Test

Enter http://localhost:8080/doc.html in the browser. If you find the following page, the test is successful.

Commonly used annotations

The generated interface document can be controlled through annotations to make the interface document more readable. Commonly used annotations are as follows:

Annotation Explanation
@Api Used on a class, such as Controller, to indicate a description of the class
@ApiModel Used on classes, such as entity, DTO, VO
@ApiModelProperty Used on attributes to describe attribute information
@ApiOperation Used on methods, such as Controller methods, to explain the purpose and function of the method