Springboot整合Canal解决数据缓存一致性问题

pom.xml


    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
        
            com.xpand
            starter-canal
            0.0.1-SNAPSHOT
        
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
    

application.yml

server:
  port: 18083
spring:
  application:
    name: canal
  redis:
    host: 192.168.1.11
    port: 6379
    password: 123456

#canal配置
canal:
  client:
    instances:
      example:
        host: 192.168.1.11
        port: 11111

canal监听类

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

@CanalEventListener
public class CanalDataEventListener {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @InsertListenPoint
    public void onEventInsert(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        for(CanalEntry.Column c:rowData.getAfterColumnsList()){
            System.out.println("添加后:" + c.getName() + "--------" + c.getValue());
        }
        String id = getData(eventType, rowData);

        stringRedisTemplate.boundValueOps("content_"+id).set(JSON.toJSONString("data"));
    }
    @UpdateListenPoint
    public void onEventUpdate(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        for(CanalEntry.Column c:rowData.getBeforeColumnsList()){
            System.out.println("修改前:" + c.getName() + "--------" + c.getValue());
        }
        for(CanalEntry.Column c:rowData.getAfterColumnsList()){
            System.out.println("修改后:" + c.getName() + "--------" + c.getValue());
        }

        String id = getData(eventType, rowData);
        stringRedisTemplate.boundValueOps("content_"+id).set(JSON.toJSONString("onEventUpdate -data"));
    }
    @DeleteListenPoint
    public void onEventDelete(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        for(CanalEntry.Column c:rowData.getBeforeColumnsList()){
            System.out.println("删除前:" + c.getName() + "--------" + c.getValue());
        }
    }
    @ListenPoint(
            destination="example",schema="changgou_content",table="tb_content",eventType = CanalEntry.EventType.DELETE
    )
    public void onEventCustomUpdate(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        for(CanalEntry.Column c:rowData.getBeforeColumnsList()){
            System.out.println("-----自定义删除前:" + c.getName() + "--------" + c.getValue());
        }
    }

    public String getData(CanalEntry.EventType entryType,CanalEntry.RowData rowData){
        String id="";
        if(entryType.equals(CanalEntry.EventType.DELETE)){
            for(CanalEntry.Column c:rowData.getBeforeColumnsList()){
                if(c.getName().equalsIgnoreCase("category_id")){
                    String value = c.getValue();

                    id= value;
                    //stringRedisTemplate.delete(value);
                }
            }
        }else{
            for(CanalEntry.Column c:rowData.getAfterColumnsList()){
                if(c.getName().equalsIgnoreCase("category_id")){
                    String value = c.getValue();
                    id= value;
                    //stringRedisTemplate.delete(value);
                }
            }
        }

        return id;
    }
}

启动类

import com.xpand.starter.canal.annotation.EnableCanalClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableCanalClient
public class CanalApplication {

    public static void main(String[] args) {
        SpringApplication.run(CanalApplication.class,args);
    }
}

启动后,即可在数据库修改,控制台可以看见相关日志。

你可能感兴趣的:(java,spring,boot,redis,数据库,mysql)