A scaffold example of a management platform based on a new generation of interface services framework “ApiBoot” (” Vue + ElementUI “)

preface

Forwarding user requests is a mandatory step for the interface service at deployment time.

The steps for requesting forwarding are as follows:

  1. The domain name is resolved to the forwarding server
  2. The forwarding server forwards the packets to the unified gateway based on the weight and backup configurations
  3. If the unified gateway has gray configuration, you need to filter requests based on identity or header information
  4. Forward to a specific business service

At present, there are many kinds of excellent request forwarding on the market, such as: Nginx, F5, Kong, Tengine, etc. Tengine is alibaba’s encapsulation based on Nginx. This chapter is based on Nginx to explain the content, let’s first prepare the test environment of Nginx.

Prepare the environment

If your test environment does not have Nginx installed, I will describe the installation process in two ways.

Install Nginx using Brew

If you are running OSX, you can install it directly using brew management tool. This method is relatively simple. You can automatically download the latest stable version from the remote server to decompress and configure the environment.

# to install nginx
➜  ~ brew install nginx
Copy the code

Wait quietly ~

After the installation is complete, we will first change the port number (brew installation package changed the default listening port number to 8080, generally when using unzip mode listening port 80).

We need to find the location of the nginx.conf file:

➜  ~ sudo find / -name nginx.conf           
/usr/local/etc/nginx/nginx.conf
Copy the code

Find file, we use sudo vi/usr/local/etc/nginx/nginx. Conf command to change the default port number, location is as follows:

server {
        listen       80;
        server_name  localhost;
        #...
}        
Copy the code

Save the modification and exit.

Finally, don’t forget to restart the Nginx service.

➜  ~ brew services restart nginx
Copy the code

Decompress package

First, go to nginx.org/download to download the nginx official address to choose your favorite version, the following version 1.17.7 example:

  • Nginx.org/download/ng…

Click download and decompress to install (pay attention to the compile environment, there may be a lack of some dependency libraries, the local installation of the corresponding dependency is ok)

# extract nginxThe tar - XVF nginx - 1.17.7. Tar. Gz# enter directory
cdNginx - 1.17.7# configuration
./configure --prefix=/usr/local/nginx
# compiler
sudo make
# installation
sudo make install
Go to the nginx execution directory
cd /usr/local/nginx/sbin
# start nginx
./nginx
Copy the code

Welcome to Nginx! “, indicating that we have successfully installed.

The sample project

To demonstrate the painless perception of update service users, let’s create a simple SpringBoot sample project and add a test interface to the project. The project pam.xml dependencies look like this:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>
Copy the code

The sample interface

Create a TestController named TestController as follows:

/** * Test controller **@authorHeng Yu Teenager */
@RestController
@RequestMapping(value = "/test")
public class TestController {
    @Autowired
    private ServerProperties serverProperties;

    @GetMapping
    public String hello(a) {
        return "Request dispatched, port number:" + serverProperties.getPort() + "Service, interface access successful."; }}Copy the code

Configure forward

The request interface for our test is now in place. We need to forward the request to our test interface when accessing Nginx. We need to configure the forwarding using the two nginx keywords upstream and location.

  • Upstream: server group where requests are sent to multiple servers.
  • location: forward the path prefix, such as “/user/”, when we accesshttp://127.0.0.1/user/1Is executedlocationForwarding service of.

Upstream’s forwarding process is shown below:

Configuration of UpStream

Add “upstream” to the nginx.conf file HTTP as follows:

# Load configuration
upstream test{server 127.0.0.1:8080 weight = 1; Server 127.0.0.1:9090 weight = 2; 9000 backup server 127.0.0.1:; }Copy the code

Configure the Location

Having configured the server group above, we need to configure the server group named test as a proxy in location. Add a new location under the server of Location as follows:

# configure "/lb/" to forward all requests to local port 8080
location /lb/ {
	proxy_pass http://test/;
	proxy_set_header Host $host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_connect_timeout       50;
	proxy_read_timeout          50;
	proxy_send_timeout          50;
}
Copy the code

Restart the Nginx

I installed Nginx using BREW, so the command to restart nginx is as follows:

brew services restart nginx
Copy the code

If you are installing the package:

Go to the installation package directory
cd /usr/local/nginx/sbin
# overloading
./nginx -s reload
Copy the code

The weight allocation

In nginx there is a concept of weight, according to the size of the weight value to control the request traffic, when the weight value is larger, more traffic will be distributed.

  • Server 127.0.0.1:8080 weight 1;The weight ratio isA thirdOne out of every three requests will be forwarded to this server.
  • Server 127.0.0.1:9090 weight 2;The weight ratio isTwo-thirds of theTwo out of three requests are forwarded to this server.

Standby configuration

When we add backup to the end of the server in upstream, it indicates that the server is the standby server and is only enabled when all other servers are down, which is what we take advantage of when updating.

Run the test

For the sake of demonstration, we will directly package the test project package in this chapter and use –server.port to specify the port number to run to simulate the scenario of multiple servers.

Start 127.0.0.1:8080 serverJava - jar target/use - nginx - loadbalance - upgrade - service - 0.0.1 - the SNAPSHOT. Jar -- server. The port = 8080# Start 127.0.0.1:9090 serverJava - jar target/use - nginx - loadbalance - upgrade - service - 0.0.1 - the SNAPSHOT. Jar -- server. The port = 9090Start 127.0.0.1:9000 standby serverJava - jar target/use - nginx - loadbalance - upgrade - service - 0.0.1 - the SNAPSHOT. Jar -- server. The port = 9000Copy the code

Note: Use multiple terminal Windows to run the service.

Nginx. conf>server set location to /lb/ path prefix, so we visit http://127.0.0.1/lb/test (nginx listens on port 80, Nginx does not require a port number to be used for forwarding.

Test point: weighted forwarding

curl http://localhost/lb/testPort: 8080. The interface is successfully accessed. Curl http://localhost/lb/testPort: 9090. The interface is successfully accessed. Curl http://localhost/lb/testPort: 9090. The interface is successfully accessed. Curl http://localhost/lb/testPort number: 8080. The interface is accessed successfully.Copy the code

According to the access result, the service of port 8080 is requested once in every three times, while that of port 9090 is requested twice in every three times, which is exactly in line with our configured weight, and the test passes.

Test point: Standby takes effect

Let’s stop both 8080 and 9090 and visit http://127.0.0.1/lb/test again.

curl http://localhost/lb/testPort: 9000. The interface is successfully accessed. Curl http://localhost/lb/testPort: 9000. The interface is successfully accessed. Curl http://localhost/lb/testPort number: 9000. The interface is accessed successfully.Copy the code

It can be seen that our standby server is enabled and has forwarded all the request traffic to the service 9000. The test passed.

Type on the blackboard and underline

Nginx will not distribute traffic to the standby 9000 server as long as either 8080 or 9090 server is running. Nginx will not distribute traffic to the standby 9000 server if either 8080 or 9090 server is running. And so on, all the services are updated.

Code sample

If you like this article please click Star for source repository, thanks!! The source code for this article can be obtained in the use-nginx-loadbalance-upgrade-service directory:

  • Gitee:gitee.com/hengboy/spr…

Author’s personal blog uses the open source framework ApiBoot to help you become an Api service architect