Mysql uses redis+canal to achieve cache consistency

Table of contents

1. Enable binlog logs

1. First check whether binlog is enabled

2. Enable binlog and restart the mysql service

2. Authorize canal to link to MySQL. The account has the authority to serve as a MySQL slave.

3. Download and configure canal

1. Download canal, visit the release page, select the required package to download, take version 1.0.17 as an example

2. Modify the instance.properties configuration file in the conf\example folder

3. Start the canal service (bat under windows, sh in linux)

4. Based on Canal notification principle

5. Project integration

1. pom dependency

2. Write dependencies

3. Modify the Item entity class

4. Write a listener (Redis/jvm cache synchronization) 

6. Test

1. Database table data and redis data

2. Add data

3. Modify data

​Edit 4. Delete data


1. Enable binlog logs

1. First check whether binlog is enabled

show variables like '%log_bin%';

If it is OFF, the instruction bit is turned on

2. Enable binlog and restart the mysql service

Right click on My Computer - Manage - Services - MYSQL - Properties

Here is my.ini address

Add under [mysqld]

log-bin= mysqlbinlog

binlog-format=ROW

Insert image description here

After configuration, you need to restart the mysql service

View status 

show variables like '%log_bin%';

Opened successfully 

2. Authorize canal to link to MySQL. The account has the authority to serve as a MySQL slave.

CREATE USER canal IDENTIFIED BY 'canal';  
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
-- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ;
FLUSH PRIVILEGES;

3. Download and configure canal

1. Download canal, visit  the release  page, select the required package to download, take version 1.0.17 as an example

Or use my files: Alibaba Cloud Disk Sharing

After decompression

2. Modify the instance.properties configuration file in the conf\example folder

final configuration file

#################################################

## mysql serverId , v1.0.26+ will autoGen

# canal.instance.mysql.slaveId=0

# enable gtid use true/false

canal.instance.gtidon=false

# position info

canal.instance.master.address=127.0.0.1:3306

canal.instance.master.journal.name=

canal.instance.master.position=

canal.instance.master.timestamp=

canal.instance.master.gtid=

# rds oss binlog

canal.instance.rds.accesskey=

canal.instance.rds.secretkey=

canal.instance.rds.instanceId=

# table meta tsdb info

canal.instance.tsdb.enable=true

#canal.instance.tsdb.url=jdbc:mysql://127.0.0.1:3306/canal_tsdb

#canal.instance.tsdb.dbUsername=canal

#canal.instance.tsdb.dbPassword=canal

#canal.instance.standby.address =

#canal.instance.standby.journal.name =

#canal.instance.standby.position =

#canal.instance.standby.timestamp =

#canal.instance.standby.gtid=

# username/password

canal.instance.dbUsername=root

canal.instance.dbPassword=msir1234

canal.instance.connectionCharset = UTF-8

# enable druid Decrypt database password

canal.instance.enableDruid=false

#canal.instance.pwdPublicKey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALK4BUxdDltRRE5/zXpVEVPUgunvscYFtEip3pmLlhrWpacX7y7GCMo2/JM6LeHmiiNdH1FWgGCpUfircSwlWKUCAwEAAQ==

# table regex

canal.instance.filter.regex=.*\\..*

# table black regex

canal.instance.filter.black.regex=mysql\\.slave_.*

# table field filter(format: schema1.tableName1:field1/field2,schema2.tableName2:field1/field2)

#canal.instance.filter.field=test1.t_product:id/subject/keywords,test2.t_company:id/name/contact/ch

# table field black filter(format: schema1.tableName1:field1/field2,schema2.tableName2:field1/field2)

#canal.instance.filter.black.field=test1.t_product:subject/product_image,test2.t_company:id/name/contact/ch

# mq config

canal.mq.topic=example

# dynamic topic route by schema or table regex

#canal.mq.dynamicTopic=mytest1.user,topic2:mytest2\\..*,.*\\..*

canal.mq.partition=0

# hash partition config

#canal.mq.enableDynamicQueuePartition=false

#canal.mq.partitionsNum=3

#canal.mq.dynamicTopicPartitionNum=test.*:4,mycanal:6

#canal.mq.partitionHash=test.table:id^name,.*\\..*

#################################################

3. Start the canal service (bat under windows, sh in linux)

 Click startup.bat to start

Check the log logs\canal\canal.log. If the startup is successful, it will be as shown below.

4. Based on Canal notification principle

Insert image description here

Interpretation:

  • After the product service completes the product modification, the business ends directly without any code intrusion.
  • Canal monitors changes in the MySQL database and immediately notifies the cache service when changes are detected.
  • The cache service receives the canal notification and updates the cache.

5. Project integration

1. pom dependency

Use the third-party open source canal-starter client on GitHub. Address: https://github.com/NormanGyllenhaal/canal-client

<dependency>
        <groupId>top.javatool</groupId>
        <artifactId>canal-spring-boot-starter</artifactId>
        <version>1.2.1-RELEASE</version>
</dependency>

2. Write dependencies

canal:
   destination: example
   server: localhost:11111 #canal’s ip address and port

3. Modify the Item entity class

What Canal pushes to canal-client is the modified row of data (row), and the canal-client we introduced will help us encapsulate the row data into the Item entity class. This process requires knowing the mapping relationship between database and entities. Need to use JPA annotations:

Complete the mapping between Item and database table fields through @Id, @Column and other annotations:

Note that if it is different from the database name, @Column must be used

package com.springboot3.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Id;

/**
 * 
 * @TableName course
 */
@TableName(value ="course")
@Data
public class Course implements Serializable {
    /**
     * 
     */
    @Id
    @TableId
    private String id;

    /**
     * 
     */
    private String name;

    /**
     * 
     */
    @Column(name = "teacher_id")
    private String teacherId;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}

4. Write a listener (Redis/jvm cache synchronization) 

EntryHandler<T>Write a listener by implementing the interface to listen to Canal messages. Note two points:

  • The implementation class @CanalTable("course")specifies the table information to be monitored.
  • The generic type of EntryHandler is the entity class corresponding to the table
package com.springboot3.handler;

import com.springboot3.domain.Course;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

@CanalTable(value = "Course")
@Component
@Slf4j
public class CourseHandler implements EntryHandler<Course> {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void insert(Course course) {
        log.info("insert message  {}", course);
        redisTemplate.opsForValue().set(course.getId(),course);
    }

    @Override
    public void update(Course before, Course after) {
        log.info("update before {} ", before);
        log.info("update after {}", after);
        redisTemplate.opsForValue().set(after.getId(),after);

    }

    @Override
    public void delete(Course course) {
        log.info("delete  {}", course);
        redisTemplate.delete(course.getId());

    }
}

6. Test

1. Database table data and redis data

database:

redis:

2. Add data

Add data physics

The console shows:

redis database:

3. Modify data

Change physics to chemistry

The console shows:

redis database:

4. Delete data

Delete chemistry subject

console data

 redis database data:

Guess you like

Origin blog.csdn.net/weixin_55127182/article/details/132280538