Java Cluster Combat: Upgrading Single Architecture to Cluster Architecture (1) Using NGINX to Build a Cluster

Java Cluster Combat: Upgrading Single Architecture to Cluster Architecture (2) Realizing Session Sharing

Java cluster combat: single architecture upgrade to cluster architecture (3) sharing of uploaded files

Java Cluster Combat: Upgrading Single Architecture to Cluster Architecture (4) Using REDIS Distributed Locks

Java Cluster Combat: Upgrading Single Architecture to Cluster Architecture (5) Scheduled Tasks

Java Cluster Combat: Upgrading Single Architecture to Cluster Architecture (6) Distributed Cache REDIS

For the IT technicians of large factories, the microservice architecture is used every day, and they are familiar with it. For rookie programmers in small factories, what we do every day is CRUD. For us, clustering, distributed, and microservices are just a vague concept in our minds. Most of the systems we write are single-architecture systems that run on one server. If the number of users increases and the server cannot support it, we will add configuration. Anyway, Alibaba Cloud and Tencent Cloud are now easy to add configuration. But if one day (hope all rookies have such a day), our number of users increases too fast and too much, and the server with the highest configuration is still unable to support it. Is this a happy trouble for us? ——It is the boss who is happy, but we are the ones who are troubled.

In this case, we thought of using microservices, and we started to learn spring cloud alibaba, but we didn't know how many times our server would crash before we learned it, and we were afraid that the boss would kill us when he got angry. Therefore, it is not enough to cram for a while, and you must learn as early as possible! So at this time, what is the faster solution? I think it may be to use a cluster architecture. The cluster architecture is relatively easy to learn, and the amount of code modification will be less.

Today we will first build a simple cluster, as shown in the following figure:

A total of three computers, one installed NGINX, two installed TOMCAT. These two TOMCATs run our single application, and NGINX accepts requests from the browser, and then forwards the requests to one of the TOMCATs in turn for processing. If you don't understand this passage, it's okay, let's get started, and when we're done, you'll understand. Students who do not have three computers can use virtual machines. Or just operate on one computer, then both IPs must be changed to 127.0.0.1

GitHub:  GitHub -  all the source code of Dengxd/JavaCluster is here, GitHub often cannot be connected, so it needs to be refreshed several times

Install NGINX

Because all programmers will use WINDOWS, and some programmers will not use LINUX, so I chose WINDOWS7 to install.

NGINX download address: nginx: download

Stable version is a stable version, select this version. The download is a file nginx-1.22.1.zip, after decompression, as shown in the figure below:

Open the nginx.conf file in the folder conf and find these lines:

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

Change it to this:

upstream mytomcat {
		server 192.168.1.200:8000;
		server 192.168.1.201:8001;
    }
    server {
        listen       80;
		server_name  localhost;
		
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://mytomcat;
        }

Save it, and NGINX is configured.

In these few lines of code

server 192.168.1.200:8000;

server 192.168.1.201:8001;

These two lines are the IP and ports of our two TOMCATs. If you want to add a few more TOMCATs, just add a few more lines. If you don't have three computers and only operate on one computer, change both IP addresses to 127.0.0.1

Note: In the production environment, server_name localhost; this line should change localhost to your domain name

Mytomcat is actually a name for the cluster, which can be named according to your own habits. This name will be used in this line: proxy_pass http://mytomcat;

After the modification, it is necessary to test whether the configuration file nginx.conf is correct

Use the nginx -t command to test:

As shown in the figure above, if "syntax is ok" and "test is successful" appear, the test is successful. If these two lines do not appear, it means that there is an error in nginx.conf, and the error must be corrected until the test is successful.

Write a simple monolithic program

This program uses the Spring boot framework, which comes with TOMCAT, which saves a lot of trouble. The function of the program is to display the port number on the web page, either displaying 8000 or displaying 8001

The main code is as follows:

static String port;
public static void main(String[] args) throws Exception{
    SpringApplication app = new SpringApplication(MainApplication.class);
    Environment env = app.run(args).getEnvironment();
    port=env.getProperty("server.port");//获取端口号
    System.out.println(port);
}

@RequestMapping("/")
@ResponseBody
public String test(HttpServletRequest request, HttpServletResponse response){
    return port;//返回端口号
}

The main method obtains the port number when starting the program, and the test method returns the port number when the user accesses

Run the program on two business servers

We packaged the program and copied it to two Tomcat business servers.

The server with IP 182.168.1.200 uses the command: java -jar ShowPort-1.0-SNAPSHOT.jar --server.port=8000 to start the program. Then use a browser to access: http://localhost:8000/ on the server   . If the web page displays 8000, it means that the program is normal:

The server whose IP is 182.168.1.201 uses the command: java -jar ShowPort-1.0-SNAPSHOT.jar --server.port=8001 to start the program. Then use a browser to access: http://localhost:8001/ on the server   . If the web page displays 8001, it means that the program is normal.

Start NGINX on the NGINX server

Start the service with the command nginx:

Note: If you want to stop NGINX, you cannot close this CMD window directly. You have to open another CMD window, and then use the command nginx -s stop to shut down NGINX:

Test cluster on NGINX server

Now we start the browser on the NGINX server, enter the URL: http://localhost/    ,

You can see that the web page displays 8001:

We open a few more tabs in the browser, and we will see that some display 8000, and some display 8001

In this way, our cluster is successfully configured. Next time we will implement session sharing.

Guess you like

Origin blog.csdn.net/dengxiaodai/article/details/129707243