cgb2007 Jingtao day12

1. High availability of database
1.1 construction strategy
1.1.1 problem description
Note: if the agent of the database is implemented according to the following configuration, if the main database of the database goes down, the execution of the whole program will be directly affected Therefore, we need to implement 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 state will be terminated

1.1.2 dual machine hot standby (dual master mode)
Note: the high availability of database can be realized through the dual main mode of database
Dual master mode is essentially a host that backs up each other

1.1.3 realization of dual machine hot standby
Previous configuration:
Host: 192.168.126.129
Slave Library: 192.168.126.130
Current configuration:
Host: 192.168.126.130
Slave Library: 192.168.126.129

1). Check main library status

2). Realize the construction of master-slave database

1.2 database high availability implementation
1). Edit profile

<?xml version="1.0"?>

<mycat:schema xmlns:mycat="http://io.mycat/">

<!--name Properties are custom  dataNode Represents the node information of the database  jtdb Represents a 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 reporting-->
	<!--balance 0 Indicates that all read operations will be sent to writeHost host -->  
	<!--1 Indicates that all read operations are sent to readHost And idle master nodes-->
	<!--writeType=0 All writes are sent to the first writeHost host-->	
	<!--writeType=1 All writes are sent randomly to writeHost in-->
	<!--dbType Represents the database type mysql/oracle-->
	<!--dbDriver="native"  Fixed parameters unchanged-->
	<!--switchType=-1 Indicates no automatic switching, The slave node will not be automatically switched after the host goes down-->
	<!--switchType=1  Indicates automatic switching(Default value)If the first primary node goes down,Mycat Three heartbeat tests will be performed,If there is no response three times,It will automatically switch to the second master node-->
	<!--And will be updated/conf/dnindex.properties Master node information of the file localhost1=0 Represents 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>

	<!--Configure the first host to write to the library,Under default conditions Mycat It mainly operates the first host, in which the read-write separation has been realized.Because the default write operation will be sent to the database of 137.The read operation is sent to 141 by default.If the slave node is busy,Then the main node shares 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 realized dual machine hot standby.-->
		<!--Mycat Achieve high availability.When the first host 137 goes down.mycat It will automatically send out heartbeat detection.Test 3 times.-->
		<!--If the host 137 does not give Mycat In response, the host is judged dead.The second host back to Qidong will continue to provide services for users.-->
		<!--If the host is restored, it is in a waiting state.If 141 goes down, 137 continues 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 profile:

3). Restart mycat server

1.3 database high availability test
1. Start the server and test whether the mysql database is normal

2. Close the main database and check whether the user's operation is normal again


3). Modify the contents in the database, restart the main database and check whether the data is synchronized



1.4 check port occupancy


2. Redis cache learning
2.1 optimize the existing architecture
Note: the cache server can effectively improve the efficiency of user access
matters needing attention:
1. The cached data structure should be K-V structure. 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. Cache needs to run very fast. C language... 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 Realize the persistent operation of memory data (disk)

2.2 Redis cache server
website: http://www.redis.cn/

2.2.1 introduction to redis
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.
Nginx: 30000-50000 / S
redis: read: 112000 / s, write: 86000 / s, average 100000 / S
Throughput: 500000 / S

2.2.2 Redis installation
1). Extract the redis file

2). Move / modify files

3). Install Redis
Command 1: make
Command 2: make install

2.2.3 modify redis Conf configuration file

2. Turn off the protection mode

3. Enable background startup

2.2.4 Redis command
1. Start redis redis server conf

2. Check redis service items

3. Enter the redis client
redis-cli -p 6379
4. Turn off redis
1). Command redis cli - P 6379 shutdown
2).kill Command - 9 PID number

2.3 spring boot integration Redis
2.3.1 import jar package

    <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 org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;

public class TestRedis {
/**
* 1. Implement redis test
*Error report check:
* 1. Check 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
* 3. Check firewall
* */
@Test
public void test01(){
Jedis jedis = new Jedis("192.168.126.129",6379);
jedis.set("2007", "redis introduction case");
System.out.println(jedis.get("2007"));
}

/**
 * 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("2007") {/ / judge whether the data exists
// jedis.set("2007", "test case 2222");
// }
//setnx function: if there is data, it will not be processed
jedis.setnx("2007", "test advanced usage");
System.out.println(jedis.get("2007"));

}

/**
 * Requirements:
 *      Add a data to redis Set key value: it is required to add a timeout of 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 warehousing operation / timeout together setex
 */
@Test
public void test03() throws InterruptedException {
    Jedis jedis = new Jedis("192.168.126.129",6379);
    //jedis.set("2007", "test time");
    //Meaning: hidden data needs to be deleted
    //jedis.expire("2007", 100);
    jedis.setex("2007", 100, "Test time");
    System.out.println(jedis.ttl("2007")+"second");

}

/**
 * 1.If the data exists, the data setnx will not be operated
 * 2.At the same time, set the timeout and pay attention to atomic 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() throws InterruptedException {
    Jedis jedis = new Jedis("192.168.126.129",6379);
    SetParams setParams = new SetParams();
    setParams.xx().ex(100);
    jedis.set("2007", "bbbbb",setParams);
    System.out.println(jedis.get("2007"));
}

}
2.3.3 construction of project structure

Tags: Java

Posted by ccjob2 on Wed, 04 May 2022 13:17:57 +0300