大屏監控系統實戰(2)-後臺工程搭建

一、概述

項目的後端技術棧爲Java、SpringBoot、MybatisPlus、爬蟲Jsoup、HttpClient、Maven項目構建。

各軟件版本分別如下:

軟件及環境 版本號
操作系統 Windows10&MacBook Pro
開發工具 IDEA2019.3
數據庫工具 Navicat Premium12.0
MySQL 5.7
JDK 1.8
SpringBoot 2.1.8.RELEASE
Maven 3.6.0
部署服務器 CentOS7.2
httpclient 4.5.8
Jsoup 1.12.1
fastjson 1.2.54
lombok 1.18.10
mybatis-plus 3.2.0
druid 1.1.12
mysql-connector-java 5.1.48
maven插件 3.1.0&3.8.1

二、SpringBoot項目的配置文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.anymk</groupId>
        <artifactId>parent-boot-vue</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>csdn-reader</artifactId>

    <properties>
        <java.version>1.8</java.version>
        <spring.boot.version>2.1.8.RELEASE</spring.boot.version>
        <maven.resource.version>3.1.0</maven.resource.version>
        <maven.clean.version>3.1.0</maven.clean.version>
        <maven.compiler.version>3.8.1</maven.compiler.version>
        <java.source.version>1.8</java.source.version>
        <java.target.version>1.8</java.target.version>
        <file.encoding>UTF-8</file.encoding>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.8</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.54</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.12.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>
        <!-- database -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>csdn</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <!--解決打包後,執行java -jar 命令,找不到主清單屬性-->
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--clean插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>${maven.clean.version}</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>src/main/resources/static</directory>
                        </fileset>
                        <fileset>
                            <directory>src/main/resources/templates</directory>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <!--資源插件,主要爲了從前端項目裏複製打包好的文件到springboot項目-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>${maven.resource.version}</version>
                <executions>
                    <execution>
                        <id>copy static</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>src/main/resources/static</outputDirectory>
                            <overwrite>true</overwrite>
                            <resources>
                                <resource>
                                    <!--因爲vue-cli打包的目錄在項目的根目錄,所以從這裏複製-->
                                    <directory>${project.parent.basedir}/construction-data/dist</directory>
                                    <includes>
                                        <include>css/</include>
                                        <include>img/</include>
                                        <include>js/</include>
                                        <include>favicon.ico</include>
                                        <include>index.html</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy template</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>src/main/resources/templates</outputDirectory>
                            <overwrite>true</overwrite>
                            <resources>
                                <resource>
                                    <!--因爲vue-cli打包的目錄在項目的根目錄,所以從這裏複製-->
                                    <directory>${project.parent.basedir}/construction-data/dist</directory>
                                    <includes>
                                        <include>index.html</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

注意,這裏我們給這個工程提供了一個父工程,父工程主要的目的是爲了讓這個前後端分離的項目最終能打包成一個Jar包去部署,提高運維效率,具體操作見:如何將SpringBoot+Vue前後端分離項目一次打包爲一個Jar包運行?

application.yml

該文件位於resource目錄下,需要大家修改的地方有:項目運行的端口號port,數據庫的連接信息,其他都不用改

server:
  #端口號
  port: 8888
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/csdnreader?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
    username: root
    password: root
    druid:
      initialSize: 5
      minIdle: 5
      maxActive: 20
      maxWait: 60000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1
      testWhileIdle: true
      testOnBorrow: true
      testOnReturn: false
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      filters: stat,wall
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      stat-view-servlet:
        allow: 127.0.0.1
  aop:
    proxy-target-class: true

  messages:
    encoding: utf-8

  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

#mybatis plus 設置
mybatis:
  type-aliases-package: com.csdn.reader.entity
  mapper-locations: classpath*:mapper/*/*.xml
  configuration:
    jdbc-type-for-null: null
  global-config:
    # 關閉 mybatis-plus的 banner
    banner: false

三、數據庫腳本

201個博主的投票信息會每五分鐘去投票網站爬取一次,爬取後寫到數據庫以便於後續的數據分析,特提供數據庫腳本如下:

/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 50553
 Source Host           : localhost:3306
 Source Schema         : csdnreader

 Target Server Type    : MySQL
 Target Server Version : 50553
 File Encoding         : 65001

 Date: 21/01/2020 15:53:35
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_csdn_topn
-- ----------------------------
DROP TABLE IF EXISTS `t_csdn_topn`;
CREATE TABLE `t_csdn_topn`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ranking` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '排名',
  `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名字',
  `nowVotes` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '票數',
  `createDate` datetime NULL DEFAULT NULL COMMENT '採集時間',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 315973 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;

SET FOREIGN_KEY_CHECKS = 1;

四、目錄結構及啓動類

採用後端標準的三層模型進行開發

目錄結構如下

ReaderApplication啓動類

package com.csdn.reader;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
@EnableScheduling//開啓定時任務
@MapperScan("com.csdn.reader.dao")
public class ReaderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ReaderApplication.class, args);
    }

}

註解詳解:

@EnableScheduling//開啓定時任務,去定時爬取投票網站數據
@MapperScan("com.csdn.reader.dao")//設置掃描的數據訪問層包路徑,將數據insert到MySQL數據庫中

五、項目啓動

 在開發環境中運行啓動類的main函數即可啓動。啓動成功後截圖:

over!

發佈了263 篇原創文章 · 獲贊 3167 · 訪問量 87萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章