Summary of the Second Kill Mall Project

Keywords: Redis Spring MySQL Thymeleaf

Technology used

1. Backend: SpringBoot, JSR303, MyBatis

2. Front end: Thymeleaf, BootStrap, Jquery

3. Middleware: RabbitMQ, Redis, Druid

Project building

1. Build Spring Boot
1.Add to maven
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
2. Integrated thymeleaf
    1.Import dependency
        <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

    2.Add configuration items:
      spring.thymeleaf.cache=false
      spring.thymeleaf.content-type=text/html
      spring.thymeleaf.enabled=true
      spring.thymeleaf.encoding=UTF-8
      spring.thymeleaf.mode=HTML5
      spring.thymeleaf.prefix=classpath:/templates/
      spring.thymeleaf.suffix=.html

    3.Add to thymeleaf Template
      <!DOCTYPE HTML>
      <html xmlns:th="http://www.thymeleaf.org">
      <head>
        <title>hello</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      </head>
      <body>
        <p th:text="'hello:'+${name}" ></p>
      </body>
      </html>

3.Spring integrates Mybatis framework

  1.Add dependency
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.0.0</version>
    </dependency>
  2.Add configuration
    # mybatis
    #type-aliases-package: Packet scanning path of data table corresponding entity class
    mybatis.type-aliases-package=com.xintao.testone.domain
    mybatis.configuration.map-underscore-to-camel-case=true
    mybatis.configuration.default-fetch-size=100
    mybatis.configuration.default-statement-timeout=3000
    #type-aliases-package: Packet scanning path for data table corresponding to XML
    mybatis.mapperLocations =classpath:com/xintao/testone/dao/*.xml

It is necessary to configure the entity class corresponding to each table in the database and the package scanning path of XML in the application.properties file.

4. Add MySql and Druid
    1.Add dependency
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.0.5</version>
        </dependency>
    2.Add configuration
        spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
        spring.datasource.username=root
        spring.datasource.password=123456
        spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
        spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
        spring.datasource.filters=stat
        spring.datasource.maxActive=2
        spring.datasource.initialSize=1
        spring.datasource.maxWait=60000
        spring.datasource.minIdle=1
        spring.datasource.timeBetweenEvictionRunsMillis=60000
        spring.datasource.minEvictableIdleTimeMillis=300000
        spring.datasource.validationQuery=select 'x'
        spring.datasource.testWhileIdle=true
        spring.datasource.testOnBorrow=false
        spring.datasource.testOnReturn=false
        spring.datasource.poolPreparedStatements=true
        spring.datasource.maxOpenPreparedStatements=20
        logging.leve.com.chengxiaoxiao.seckillshop=debug

(1)JDBC URL format

jdbc:mysql://<ip>: <port>/database name

(2) The following time zone errors occurred in MySql at runtime:

java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support

Problem resolution: Springboot 2.1.0 has a very high 8.0.15 version without specifying the MySQL driver version (as shown in the figure below), which is due to the time zone difference between the database and the system. The low version of MySQL JDBC driver (5.1.28) does not have a time zone problem.

The solution is to add & server Timezone = GMT% 2B8 to the url back end of jdbc or set MySQL time zone to the current system time zone. Check MySQL's current time zone and modify it to Beijing time zone as follows:

SHOW VARIABLES LIKE '%time_zone%';
SET GLOBAL time_zone='+8:00';

5. Install Redis

Redis is a key-value storage system, data is cached in memory, and access efficiency is very high. The purpose of using redis is to break through the connection abnormality problem that MySQL and other databases are prone to occur in the case of large concurrency, because MySQL's data query is very time-consuming and unable to cope with the huge throughput required by the second kill system. The scheme we adopt is to query and obtain data in redis first, search in the database when redis can not be found, and then put the data in the database into redis.
Because the client needs to interact with the server about users and products, redis needs to be installed on the server side. Because there are no physical servers, only VMware virtual machine simulation servers can be used to complete the interaction. Create a Linux virtual machine in VMware and select NAT mode. Install Redis in the Linux virtual machine.

1. Official website: https://redis.io/download
 2. Decompression installation:
       1. Unzip file: tar-zxvf redis-5.0.3. tar.gz
       2. Move to directory / usr/local/redis: mv redis-5.0.3 /usr/local/redis
          Enter this directory: cd/usr/local/redis
       3. Compile and decompress the file: make
  		  The compilation depends on gcc and is installed as follows:
              1.yum  install  gcc
              2. Verify that the installation was successful: rpm-qa | grep GCC
       4. Execute make install to install redis: make install
       5. Modify the configuration file redis.config
          vi redis.config
              1. Modify Accessible IP: Modify bind 127.0.0.1 to bind 0.0.0.0 (allow access to other computers)
              2. Modify Background Enablation: daemonize yes
              3. Add password: requirepass
        6. Specify the startup service profile to start redis service: redis-server. / redis.conf
        7. Restart: redis-cli shutdown save
        8. Command line login: auth password
        9. Modify redis to serve the system
              1. To the utis directory: cd utils
              2. Execute install_server.sh:. / install_server.sh
                  Select the default port: [6379]
                  Set the location of conf (configuration), log (log), data (data) files
            	  		config:  /usr/local/redis/redis.config
            	 		log:	/usr/local/redis/redis.log
           	     		data:	/usr/local/redis/data
        10. See if the background process exists
              1. View process: ps-ef | grep redis
              2. Is the port listening: netstat-lntp | grep 6379
        11 Check if the installation is successful: chkconfig --list | grep redis.
        12. The most commonly used redis command:
        	  Start redis service: redis-server. / redis.conf
        	  Restart: redis-cli
        	  Exit redis: exit
 3. Adding dependencies
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
         </dependency>
    
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
             <version>1.2.38</version>
         </dependency>
4. Add configuration:
       #redis            
       redis.host=192.168.220.128
       redis.port=6379
       redis.timeout=3
       redis.password=123456
       redis.poolMaxTotal=10
       redis.poolMaxIdle=10
       redis.poolMaxWait=3

Posted by gilreilly on Wed, 15 May 2019 18:15:08 -0700