The final result

What can be gained from this project

  • Set up a Spring Boot project

  • Integrate with Mybatis to implement basic CRUD

  • Front and rear end linkage

  • Caching basics (Redis)

  • Use of Load Balancing (NGINx)

  • Basic understanding of Linux and shell files

  • Small as the sparrow is, I believe it can reap a lot

What is this project about

** Calculates Linux VM indicators. ** Includes memory usage, CPU usage, and real-time network speed. Obtain HDFS health information, including configured capacity, existing capacity, DFS remaining capacity, and non-DFS usage. And visualize it

Involving technology

Spring boot + Echarts + thymeleaf+ Redis+Vue+Bootstrap

How to go to the

Core basic look at part 1-3, beautification of the expansion to see part 4-5. Each module is detachable, take what you need. If it involves something you want to see, it’s worth watching.

The source address

Github.com/bigli97/com…

Program flow chart

Cluster deployment architecture diagram

Project technology stack

Effect of load balancing

directory

The Java connection Linux

Display Linux indicators

Integration of mybatis

Obtaining HDFS Data

Integrate redis

Integration of nginx

Part I [Java connecting to Linux]

This implementation idea

Use Java to read files in Linux, read out the information, and the obtained string to be calculated, through the control layer to the front end, use the front end to display.

Effect display of this article

Implementation steps

  1. Java uses SSH to connect to Linux

  2. Data preparation

  3. Provide a visual model

  4. The front-end and back-end linkage is implemented to asynchronously transfer data to the page

Java uses SSH to connect to Linux

2. Data preparation

(1) Obtaining CPU usage

Command: top – b – 1 n | head – 3

Third behavior CPU status: corresponding to: Us: percentage of CPU usage by user space SY: percentage of CPU usage by system kernel space NI: Niced Percentage of CPU usage by processes whose priorities have changed ID: percentage of idle CPU wa:IO Wait PERCENTAGE of CPU usage by IO waits Hi: indicates the CPU usage of Hardware IRQ hard interrupts. Si: indicates the CPU usage of software soft interrupts. St: indicates the time stolen by the hypervisor

Calculation method: 100- CPU idle percentage

(2) Obtaining memory usage

Command: the cat/proc/meminfo

The red box indicates: Total memory, free memory

Calculation method :(total memory – free memory)/total memory

(3) Real-time network speed acquisition

/proc/net/dev command: cat /proc/net/dev

Receive indicates the number of packets received (downloads) and Transmit indicates the number of packets sent (uploads).

Need a script file (to get real time Internet speed and real time upload rate, I only made the first one)

Awk: grep: grep: echo: echo: grep: echo: echo: echo: echo: echo: echo: echo: echo: echo: echo: echo: echo: echo

Calculation method: The data is based on the difference generated by the network downloads one second before and after

3. Visual templates

We need a visual front-end template, I found an official case, the result is as follows

Echarts.apache.org/examples/zh…

4. Data interaction between the front and back ends

The front part

The backend part

Summary of this part

If you’ve studied Spring Boot or Echarts and want a hands-on program, then this program is for you. Why didn’t I directly use Java to implement native (Windows) performance monitoring, as shown below? Because Java does not directly access some of the native machine data, it should be the operating system level permissions, these are the pits I tread, I also only access the JVM level

Win10 indicator monitoring

Part 2 [Integrating Mybatis]

steps

  1. Add POM dependencies
  2. Modify the Application configuration file
  3. Example Add a mapper configuration file
  4. Create packages for Entity (holding entity classes), DAO (performing data manipulation), and Service (business layer)

1. Increase POM dependency

2. Modify the Application configuration file

The annotated line represents the output SQL statement

3. Add the mapper configuration file

Why use Mapper instead of annotations, such as this code

The annotation approach is suitable only for simple SQL statements, while mapper.xml is universal

@Select("select * from user ")
Copy the code

chart

Create entity (store entity class), DAO (perform data operation), Service (business layer) package

chart

Entity class (lombok, get, set)

The dao class

The class under the service

Class of service. Under the impl

The representation outside the red box is integrated with redis

The controller class

One return page, one asynchronous access

Screenshot of results in this section

Part 3 [Obtaining HDFS Data]

Train of thought

The first part obtains some information about Linux indicators. This article is to obtain HDFS data, which is also aimed at linking with big data. The implementation idea is similar to the first part.

steps

  1. Java uses SSH to connect to Linux

  2. The character information obtained from the command is processed

  3. Provide a visual template

  4. The front-end and back-end linkage is implemented to asynchronously transfer data to the page

I’m not going to write down the repetitive steps, but you can look at part one

implementation

Return a string using Linux commands

To obtain HDFS health information, run the DFS dfsadmin-report command

Note that this command takes about 3 seconds, so you need to process it in one method when you make a tool class method. Otherwise, it will be too much to use more than 3 seconds. The /etc/profile command does not provide it. The following figure

The reason is simple to say, don’t want to know skip

JSCH connection through SSH2. SSH directly executes remote commands and scripts, using Bash’s non-interactive + non-login shell mode. It creates a shell and exits after executing the script, no longer interacting with the user. The non-interactive shell executes the script it creates, and by default this environment variable is not set. No-login shell does not execute the /etc/profile file. Instead, it checks the.bashrc file in the user’s HOME directory and loads it. So what’s the solution? Bashrc: /root/.bashrc: /root/.bashrc: /root/.bashrc: /root/.bashrc The second way is to run the command with an environment variable in front of it, as follows

The export PATH = $PATH: / opt/hadoop - 2.7.1 / bin: / opt/hadoop - 2.7.1 / sbin; hdfs dfsadmin -reportCopy the code

The command results in the following figure, and the data I want is the information in the red box

access

The subString method for strings is obtained by finding the subscript +1 of “(” and -1 of “GB”. By the way, plus one, minus one means minus one, and if you don’t get plus one you get (), not the rest. IndexOf = indexOf; indexOf = index; indexOf = index; To get the indexOf a row using indexOf(“\n”), I’ll just look for this x2 on the second row, x3 on the third. The specific code is as follows

Achieve results

Part iv [Redis Integration]

The project only implemented standalone integration

steps

  1. Add POM dependencies
  2. Modify the application. The properties
  3. Write the PortUtil utility class
  4. Write RedisConfig class
  5. Write redis utility classes
  6. Modify the business layer

1. Increase POM dependency

2. Modify application.properties

3. Write the PortUtil utility class

Function: Check whether the Redis service is started when running the project. Start go redis, not start go database.

4. Write the RedisConfig class

Function: When the Redis dependency is added, SpringBoot automatically generates a RedisTemplate and a StringRedisTemplate in the container, but the RedisTemplate’s generic type is
, This is not safe and cumbersome, and the RedisTemplate does not set serialization, so you need to configure Redis. In a word: prevent garbled code.,object>

5. Write redis utility classes

Redis operation, such as add, delete, change an object, there are too many methods, only write common.

For a representative diagram, I prefer to write the utility class as static so it’s easy to call. The required Redis template class cannot be directly injected with @Resource, so set injection is required. HasKey is one of those methods, and the others are pretty much the same.

6. Modify the service layer

Generally redis is added to the business layer.

The create method

The get method

You can see what’s going on using the Redis visualization tool or by adding it to your Application configuration file

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
Copy the code

This is used to print the SQL statement produced by the program

Screenshot of results in this section

The console

Part 5: Integrating Nginx

For those who have only studied theory, this may be an opportunity to practice.

Any Spring Boot project is ok without my project, and each part of me is independent. The net effect is to access different ports by accessing a single url or IP address. Loads can load ports or IP addresses, depending on how you configure them.

Realize the effect drawing

The first thing is packing

Jar your project

The following is how to package idea. If you can package idea, you can skip it

Packaging steps

  1. Add packaged plug-ins to poM files

  2. Add the way the package is typed, jar writes jar, war writes war

  3. Package with maven plugins (clean is recommended)

1. Add packaged plug-ins to POM files

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>  </plugin> </plugins> </build>Copy the code

Do not add the consequences of this plug-in, are personally tested pit ah

Jar = jar; war = war

<packaging>jar</packaging>
Copy the code

3. Use maven plugin to package (clean is recommended)

4. Location of the JAR package

Finally the way, I used the idea of bringing the packaging tool is that the build, and then run to look not to understand a lot of information, project finally start not, of course, maybe I am wrong way, but I still strongly do not recommend, it is a trouble, 2 it is to need to pay attention to place too much, use me this is ok.

This is the built-in packaging method of IDEA

This is an error message in the packaging mode

Here’s how to pack and skip

  1. Set a method to return the respective port
  2. Define different ports
  3. Separate the packed packages by name
  4. Modify the nginx. Conf
  5. Start the JAR packages in turn
  6. Access path verification effect
  7. Start the nginx

1. To see the effect, set a method in the controller to return the respective port (as shown in my demo).

2. Package the project and define different ports 8080,8081,8082

3. Classify the packed packages by name and put them into Linux

4. Go to the nginx conf directory and modify nginx.conf

5. Start nginx.

./nginx
Copy the code

6. Launch the JAR packages in sequence (you can also use one-click launch scripts, described below)

Nohup java-jar XXX (nohup means background running)Copy the code

7, access path verification effect, keep refreshing will observe the port changes

http://192.168.11.130/port

Start tomcat script with one click

#! APP_NAME1= computer80801.jar APP_NAME2=computer8081.jar APP_NAME3=computer8082.jar # Sh script execution. Sh [start | stop | restart | status] "exit 1} # check whether 8080 running is_exist1 () {pid1 = ` ps - ef | grep $APP_NAME1 | grep -v grep | awk '{print $2}' ` # 1 if there is no return, there returns 0 if [z] "${pid1}"; Fi} then return 1 else return 0 # check whether 8081 running is_exist2 () {pid2 = ` ps - ef | grep $APP_NAME2 | grep -v grep | awk '{print $2}' ` If [-z "${pid2}"]; Fi} then return 1 else return 0 # check whether 8082 running is_exist3 () {pid3 = ` ps - ef | grep $APP_NAME3 | grep -v grep | awk '{print $2}' ` If [-z "${pid3}"]; Then return 1 else return 0 fi} # start(){is_exist1 if [$? -eq "0"]; then echo "${APP_NAME1} is already running. pid=${pid1} ." else nohup java -jar $APP_NAME1 > /dev/null 2>&1 & fi is_exist2 if [ $? -eq "0" ]; then echo "${APP_NAME2} is already running. pid=${pid2} ." else nohup java -jar $APP_NAME2 > /dev/null 2>&1 & fi is_exist3 if [ $? -eq "0" ]; Then echo "${APP_NAME3} is already running. pid=${pid3}." else nohup Java -jar $APP_NAME3 > /dev/null 2>&1 &fi} #  stop(){ is_exist1 if [ $? -eq "0" ]; then kill -9 $pid1 else echo "${APP_NAME1} is not running" fi is_exist2 if [ $? -eq "0" ]; then kill -9 $pid2 else echo "${APP_NAME2} is not running" fi is_exist3 if [ $? -eq "0" ]; Then kill -9 $pid3 else echo "${APP_NAME3} is not running" fi} $is_exist1 if [$? -eq "0"]; then echo "${APP_NAME1} is running. Pid is ${pid1}" else echo "${APP_NAME1} is NOT running." fi is_exist2 if [ $? -eq "0"]; then echo "${APP_NAME2} is running. Pid is ${pid2}" else echo "${APP_NAME2} is NOT running." fi is_exist3 if [ $? -eq "0"]; Then echo "${APP_NAME3} is running. Pid is ${pid3}" else echo "${APP_NAME3} is NOT running Case "$1" in "start") start;; "stop") stop ;; "status") status ;; "restart") restart ;; *) usage ;; esacCopy the code