Beijing Taobao project Day12

Time:2021-9-16

1. High availability of database

1.1 construction strategy

1.1.1 problem description

If you implement the database agent according to the following configuration, if the database host goes down, it will directly affect the execution of the whole program, so you need to implement the high availability mechanism

High availability implementation issues:
    If the database is highly available, the database can be switched automatically. Because the user directly operates the slave database, when the data is inconsistent when the master database is started, the master-slave synchronization will stop

Beijing Taobao project Day12

1.1.2 dual machine hot standby (dual master mode)

Note: database high availability can be realized through database dual master mode
Dual master mode is essentially a host that backs up each other
Beijing Taobao project Day12

1.1.3 realization of dual machine hot standby

Previous configuration:
Host: 192.68.126.129
Slave Library: 192.168.126.130

Current configuration
Host: 192.168.126.130
Slave Library: 192.168.126.129

1) . check the status of the main library
Beijing Taobao project Day12

2) Build master-slave data
Beijing Taobao project Day12

1.2 database high availability implementation

1) . edit the profile

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
    
    <!-- The name attribute is customized. Datanode represents the node information of the database, and jtdb represents the logical library -- >
    <schema name="jtdb" checkSQLschema="false" sqlMaxLimit="100" dataNode="jtdb"/>

    <!-- Define node name / node host / data name -- >
    <dataNode name="jtdb" dataHost="localhost1" database="jtdb" />
        <!-- Parameter introduction UTF-8 Chinese error report -- >
        <!-- Balance 0 means that all read operations will be sent to the writehost host -- >  
        <!-- 1 means that all read operations are sent to readhost and idle master nodes -- >
        <!-- Writetype = 0. All write operations are sent to the first writehost host -- >    
        <!-- Writetype = 1 all write operations are randomly sent to writehost -- >
        <!-- Dbtype indicates the database type MySQL / Oracle -- >
        <!-- Dbdriver = "native" fixed parameter unchanged -- >
        <!-- Switchtype = - 1 means that the slave node will not be automatically switched after the host goes down -- >
        <!-- Switchtype = 1 means automatic switching (default). If the first primary node goes down, MYCAT will perform three heartbeat tests. If there is no response three times, MYCAT will automatically switch to the second primary node -- >
        <!-- The master node information of the / conf / dnindex.properties file will be updated. Localhost1 = 0 indicates the first node. Do not modify the file at will, otherwise there will be big problems -- >
    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
              writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select 1</heartbeat>

        <!-- The first host is configured to mainly write to the library. Under the default conditions, MYCAT mainly operates the first host. The first host has realized read-write separation in the first host. Because the default write operation will be sent to 137 database. The read operation will be sent to 141 by default. If the slave node is busy, the master node will share part of the pressure
        -->
        <writeHost host="hostM1" url="192.168.126.129:3306" user="root" password="root">
            <!-- Read database 1 -- >
            <readHost host="hostS1" url="192.168.126.130:3306" user="root" password="root" />
            <!-- Read database 2 -- >
            <readHost host="hostS2" url="192.168.126.129:3306" user="root" password="root" />
            
        </writeHost>

            <!-- Define the second host, because the database has implemented dual machine hot standby. -- >
            <!-- MYCAT achieves high availability. When the first host 137 goes down, MYCAT will automatically send out heartbeat detection for 3 times. -- >
            <!-- If the host 137 does not respond to MYCAT, it is judged that the host is dead. Then go back to Qidong and the second host continues to provide services for the user. -- >
            <!-- If the 137 host is restored, it will be in a waiting state. If 141 is down, 137 will continue to provide services to users again. -- >
            <!-- Premise: realize dual machine hot standby. -- >
        
        <writeHost host="hostM2" url="192.168.126.130:3306" user="root" password="root">
            
            <readHost host="hostS1" url="192.168.126.130:3306" user="root" password="root" />
            
            <readHost host="hostS2" url="192.168.126.129:3306" user="root" password="root" />
        </writeHost>
        
    </dataHost>
</mycat:schema>

2) . upload configuration file:
Beijing Taobao project Day12

3) . restart the MYCAT server
Beijing Taobao project Day12

1.3 database high availability test

1. Start the server and test whether the MySQL database is normal
Beijing Taobao project Day12

2. Close the main database and check whether the user’s operation is normal again
Beijing Taobao project Day12
Beijing Taobao project Day12

3) . modify the contents in the database, restart the main database and check whether the data is synchronized
Beijing Taobao project Day12
Beijing Taobao project Day12
Beijing Taobao project Day12

1.4 check port occupancy

Beijing Taobao project Day12
Beijing Taobao project Day12

2. Redis cache learning

2.1 optimize the existing architecture

Note: the cache server can effectively improve the access efficiency of users
matters needing attention:

1. The K-V structure should be selected as the data structure of the cache. As long as the key is unique, the result must be the same
2. The data in the cache cannot be stored all the time. It is necessary to optimize the memory data regularly. LRU algorithm
3. The cache is required to run very fast. The C language implementation... Runs in memory
4. If the data of cache operation is in memory, if power failure / downtime occurs, the memory data will be lost directly. The persistence operation of memory data (disk) is realized

Beijing Taobao project Day12

2.2 redis cache server

Official website

Redis is an open source (BSD licensed) in memory data structure storage system, which can be used as database, cache and message middleware. It supports many types of data structures, such as strings, hashes, lists, sets, sorted sets and range queries, bitmaps, hyperlogs and geospatial index radius queries. Redis has built-in replication, Lua scripting, LRU events, transactions and different levels of disk persistence, and provides high availability through redis sentinel and cluster.

Redis installation

1) . unzip the redis file
Beijing Taobao project Day12

2) . moving files modifying files
Beijing Taobao project Day12

3) . install redis
Command 1: make
Command 2: make install
Beijing Taobao project Day12

2.2.3 modify redis.conf configuration file

1. Modify IP binding
Beijing Taobao project Day12

2. Turn off the protection mode
Beijing Taobao project Day12

3. Enable background startup
Beijing Taobao project Day12

2.2.4 redis command

1. Start redis
redis-server redis.conf
Beijing Taobao project Day12

2. Check redis service items
Beijing Taobao project Day12

3. Enter redis client
redis-cli -p 6379

4. Close redis
1) . ordersredis-cli -p 6379 shutdown
2) . Kill Command kill – 9 PID number
Beijing Taobao project Day12

2.3 spring boot integration redis

2.3.1 importing jar package

<!-- Spring integrates redis -- >
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>

2.3.2 introduction cases

package com.jt;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;
public class TestRedis {
    /**
 *1. Implement redis test
 *Error report check:
 *1. Check the redis.conf configuration file 1. IP binding problem 2. Protection mode problem 3. Background startup problem
 *2. Check the redis startup mode redis server redis.conf
 */
 @Test
 public void test01(){
        Jedis jedis = new Jedis("192.168.126.129",6379);
        Jedis.set ("2019", "Yang Qianhe");
        System.out.println(jedis.get("2019"));
    }
    /**
 *I want to judge whether there is key data. If there is no key data, I will add data. If there is, I will give up adding data
 */
 @Test
 public void test02(){
        Jedis jedis = new Jedis("192.168.126.129",6379);
//If (! Jedis. Exists ("2019") {// judge whether the data exists
//Jedis.set ("2019", "Jacky Cheung");
//        }
//        System.out.println(jedis.get("2019"));
 //Setnx function: if there is data, it will not be processed
 Jedis.setnx ("2019", "Chen Huixian");
        System.out.println(jedis.get("2019"));
    }
    /**
 *Requirements:
 *Add a data. Set key value to redis. The timeout is required to be 100 seconds
 *Hidden bug: if an error is reported during code execution, the deletion may fail
 *Atomicity: success or failure at the same time
 *Solution: set the stock in operation / timeout to setex
 *Deadlock operation
 */
 @Test
 public void test03() throws Exception {
        Jedis jedis = new Jedis("192.168.126.129",6379);
        //Jedis.set ("2019", "Andy Lau");
 //Hidden meaning: the business needs to delete data when it expires
 //jedis.expire("2019", 100);
 Jedis.setex ("2019", 100, "Andy Lau");
        System.out.println (jedis.ttl ("2019") + "second");
    }
    /**
 *1. If the data exists, the data setnx will not be operated
 *2. Set the timeout at the same time and pay attention to the atomicity setex
 *Parameter Description:
 *      1. XX = "xx";  If only the key exists, perform the operation
 *      2. NX = "nx";  Write without key
 *      3. PX = "px";  millisecond
 *      4. EX = "ex";  second
 */
 @Test
 public void test04() {
        Jedis jedis = new Jedis("192.168.126.129",6379);
        SetParams setParams = new SetParams();
        setParams.nx().ex(100);
        jedis.set("2019", "aaa",setParams);
    }
}

2.3.3 construction of project structure

Beijing Taobao project Day12

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]