The third stage is reading and writing separation, load balancing, high availability and two-stage hot standby redis of Day11 Tomcat MYCAT database

Half bump 2020-11-09 10:50:28
stage reading writing separation load

1. About tomcat Server scripts

1.1 Problem specification

If by order : java -jar xxx.war When starting the server in the way of , If the remote terminal is shut down , that tomcat The server will shut down as well . Affect the use of users . The above order Indicates that the foreground is running .

1.2 Online deployment commands

explain : Generally in Linux Deploy the server in the system Generally, it is run in the back end start-up tomcat The server . And specify the log file output .
command : nohup java -jar 8081.war -> 8081.log &
 Insert picture description here

1.3 Instructions on document viewing

cat Output all the contents of the file
more Output all the contents of the document , Paging output , Space for the next screen ,q sign out
less Usage and more identical , Only by PgUp、PgOn Key to control q sign out
tail Used to display the number after the file , Frequently used
tail -10 nginx.conf see nginx.conf Last 10 That's ok
tail –f nginx.conf Dynamic view log , It is convenient to view the new information in the log
ctrl+c End of view

1.4 Linux Script description

explain : Linux Medium " Script "( plugin - The wilderness act ) The suffix of is .sh
create a file : vim
 Insert picture description here
Execute the script :
 Insert picture description here

2 Database agent description

2.1 Optimization of database structure

explain : Due to the need for users to link at the same time 2 Agents need to be introduced when there are more than one database , So we have the following deployment .
matters needing attention : User link proxy server General port number :8066 port
 Insert picture description here

2.2 Mycat Introduce

 Insert picture description here

2.3 Mycat Deploy

2.3.1 Upload Mycat Installation package

 Insert picture description here
decompression Mycat Compressed package :

 `tar -zxvf Mycat-server-1.7.0-DEV-20170416134921-linux.tar.gz`

2.3.2 Check JDK Whether to install

 Insert picture description here

2.4 About Mycat Profile description

2.4.1 server.xml Profile description

explain : stay server.xml The user name, password and operation database information are defined in the configuration file , Must be with YML Consistent profile .
 Insert picture description here
2). modify YML The configuration file
 Insert picture description here

2.4.2 schemas Profile description

explain :schemas The main function of the file is to configure the database read-write strategy .

`<writeHost host="hostM1" url="" user="root" password="root">
<!-- Read database 1-->
<readHost host="hostS1" url="" user="root" password="root" />
<!-- Read database 2-->
<readHost host="hostS2" url="" user="root" password="root" />

 Insert picture description here

2.5 Mycat test

Start command :
 Insert picture description here
Mycat test :
 Insert picture description here

2.6 Database to achieve high availability

High availability : Make sure the service doesn't go down as much as possible , Ensure the normal use of users .

2.6.1 Hot standby of database

explain : Generally, the configuration of dual hot standby in the company , Configure to be master-slave to each other .
 Insert picture description here
Configuration instructions :
1. Main library
 Insert picture description here
2. Slave Library

`/* Realize the mount of master-slave */
/* Start the master-slave service */
/* Check the master-slave status */

2.6.2 High availability of database configuration

explain : After modifying the database , Upload the configuration file .

`<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<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 the node name / Node host / Data name -->
<dataNode name="jtdb" dataHost="localhost1" database="jtdb" />
<!-- Parameter Introduction UTF-8 Wrong report in Chinese -->
<!--balance 0 Indicates that all read operations are 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 parameter unchanged -->
<!--switchType=-1 Does not automatically switch , The slave node will not be switched automatically after the host goes down -->
<!--switchType=1 It means that it will switch automatically ( The default value is ) If the first primary node goes down ,Mycat Will be carried out in 3 Heartbeat detection , If 3 No response , It will automatically switch to the second master node -->
<!-- And will update /conf/ Master node information of the file localhost1=0 Represents the first node . Don't 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 database , Under default conditions Mycat The main operation of the first host in the first host has achieved read-write separation . Because the default write will be sent to 137 The database of . Read operations are sent to by default 141. If the slave node is busy , Then the main node shares part of the pressure .
<writeHost host="hostM1" url="" user="root" password="root">
<!-- Read database 1-->
<readHost host="hostS1" url="" user="root" password="root" />
<!-- Read database 2-->
<readHost host="hostS2" url="" user="root" password="root" />
<!-- Define a second host Because the database has already realized dual hot standby .-->
<!--Mycat High availability . When the first host 137 After downtime .mycat It will automatically send out heartbeat detection . testing 3 Time .-->
<!-- If the host 137 Not given Mycat In response, the host is judged dead . The second host in Qidong will continue to provide services for users .-->
<!-- If 137 After the host recovers, it is in the waiting state . If 141 Downtime is 137 Once again, continue to provide services to users .-->
<!-- Premise : Realize the hot standby of two computers .-->
<writeHost host="hostM2" url="" user="root" password="root">
<readHost host="hostS1" url="" user="root" password="root" />
<readHost host="hostS2" url="" user="root" password="root" />

2.6.2 Realize database high availability test

testing procedure :
1. Shut down the main database , And then retrieve the data , Check whether the user's access is restricted .
2. Modify database records , Check whether the user's operation is normal . ( The operation is from the library )
3. Restart the main database Then check whether the data from the database is synchronized .

  1. Redis Study


3.1 Restore program

3.1.1 modify YML The configuration file

 Insert picture description here

3.1.2 modify pro Picture path

 Insert picture description here

3.1.3 modify Nginx The configuration file

 Insert picture description here

3.1.4 modify hosts file

 Insert picture description here

3.2 Caching mechanisms

explain : Using cache can effectively reduce the frequency of users accessing physical devices . Fast access to data from memory , And then back to the user , At the same time, we need to ensure that the data in memory is database data .
reflection :
1. The running environment of the cache should be in memory .( fast )
2. Use C Language development cache
3. What data structure should the cache use --------K-V structure It is generally used String Most of them are types key Must be unique . v:JSON Format
4. The memory environment is erased when it is powered down , So the memory data should be persisted ( Write disk operation )
5. If the memory size is not maintained , It is easy to lead to Memory data overflow . use LRU Algorithm optimization !!!
 Insert picture description here

3.3 Redis Introduce

redis It's a key-value The storage system . and Memcached similar , It supports storage value There are more types , Include string( character string )、list( Linked list )、set( aggregate )、zset(sorted set -- Ordered set ) and hash( Hash type ). These data types support push/pop、add/remove And take intersection, union and difference sets and more abundant operations , And these operations are Atomicity Of . On this basis ,redis Support various sorts of sorting . And memcached equally , To ensure efficiency , The data is cached in memory . The difference is redis Periodically, updated data is written to disk or changes are written to an appended log file , And on this basis to achieve master-slave( Master-slave ) Sync .

Redis It's open source (BSD The license ) Of , Data structure storage system in memory , It can be used as a database 、 Caching and message middleware . It supports multiple types of data structures , Such as character string (strings), hash (hashes), list (lists), aggregate (sets), Ordered set (sorted sets) And scope query , bitmaps, hyperloglogs and Geographical space (geospatial) Index radius query . Redis Built in Copy (replication),LUA Script (Lua scripting), LRU Driving events (LRU eviction), Business (transactions) And different levels of Disk persistence (persistence), And pass Redis sentry (Sentinel) And automatic Partition (Cluster) Provide high availability (high availability).

Atomicity States : Redis Is a single process, single thread operation , So there is no thread concurrency security issue . In the way of queue, one operation at a time .
Redis Common use :
1.Redis It can be used as a cache
2.Redis It can be used as a database Verification Code
3.Redis Message middleware can use Bank transfer, etc

3.4 Redis install

1). decompression Redis Installation package

`[root@localhost src]# tar -zxvf redis-5.0.4.tar.gz`

 Insert picture description here
2). install Redis
explain : stay Redis Execute the command in the root directory of
command : 1.make
2.make install
 Insert picture description here

3.5 modify Redis Configuration file for

command 1: Show line number :set nu
 Insert picture description here
Change location 1: notes IP binding
 Insert picture description here
Change location 2: Turn off protection mode
 Insert picture description here
Change location 3: Turn on background start
 Insert picture description here

3.6 redis Server command

1. Start command : redis-server redis.conf
2. Search command : ps -ef | grep redis
3. Enter the client : redis-cli -p 6379
4. close redis: kill -9 PID Number | redis-cli -p 6379 shutdown
 Insert picture description here

  1. Redis Introductory cases


4.1 introduce jar Package file

 `<!--spring Integrate redis -->

4.2 Editing tests API

`package com.jt.test;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
public class TestRedis {
* 1. test redis Program link is normal
* step :
* 1. Instantiation jedis Tools API object (host:port)
* 2. According to the example operation redis The method is to order
* The explanation about the link failure :
* 1. Check Linux A firewall
* 2. Check Redis Configuration file modification item
* 2.1 IP binding
* 2.2 Protected mode
* 2.3 Background start
* 3. Check redis Starting mode redis-server redis.conf
* 4. Check IP port And redis Whether to start ...
* */
public void test01(){
String host = "";
int port = 6379;
Jedis jedis = new Jedis(host,port);
jedis.set("cgb2006"," study hard Day day up ");
//2. Does the exercise exist key
jedis.set("cgb2006", "xxxx");
jedis.expire("cgb2006", 100);


1. Preview other orders Code and commands
2. review AOP Knowledge

本文为[Half bump]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云