利用Canal實(shí)現(xiàn)數(shù)據(jù)庫改動(dòng)就更新redis功能

此功能設(shè)計(jì)到三個(gè)微服務(wù):
service_business: 負(fù)責(zé)查詢數(shù)據(jù)庫數(shù)據(jù)。
service_business_api:存feigin遠(yuǎn)程調(diào)用接口。
service_canal:負(fù)責(zé)canal功能。

service_business

controller

@RestController
@CrossOrigin
@RequestMapping("/ad")
public class AdController {


    @Autowired
    private AdService adService;
    @GetMapping("/findAdByPosition")
   public List<Ad> findAdByPosition(String position){
       List<Ad> adList=adService.findAdByPosition(position);
       return adList;
    }

這里注意返回類型,這里一般返回Resoult,但這里是集合。

接口及實(shí)現(xiàn)類省略。。。

service_business_api

feign接口:

/**
 * @author :gzy
 * @date :Created in 2019/8/19
 * @description :
 * @version: 1.0
 */
@FeignClient(name = "business")
@RequestMapping("/ad")
public interface AdFeign {
    @GetMapping("/findAdByPosition")
    List<Ad> findAdByPosition(@RequestParam(name = "position") String position);
}

service_canal

首先:啟動(dòng)類添加feign客戶端

@SpringBootApplication
@EnableCanalClient
//找到feign的接口
@EnableFeignClients(basePackages = "com.changgou.business.feign")
public class CanaTestApplication {

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

然后引入service_business_api

<!--用于feign-->
        <dependency>
            <groupId>com.gzy</groupId>
            <artifactId>service_business_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

因?yàn)楹竺嬉怯肦edis,所以在yml配置文件加入redis的IP配置。

最后使用:

package com.example.canatest.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.business.feign.AdFeign;
import com.changgou.pojo.Ad;
import com.xpand.starter.canal.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author gzy
 * @date 2019/8/19
 */
@CanalEventListener
public class MyEventListener {
    @Autowired
    private AdFeign  adFeign;
    @Autowired
    private StringRedisTemplate redisTemplate;
//    @InsertListenPoint
//    public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
//        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
//    }
//
//    @UpdateListenPoint
//    public void onEvent1(CanalEntry.RowData rowData) {
//        System.err.println("UpdateListenPoint");
//        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
//    }
//
//    @DeleteListenPoint
//    public void onEvent3(CanalEntry.EventType eventType) {
//        System.err.println("DeleteListenPoint");
//    }

    @ListenPoint(destination = "example", schema = "changgou_business", table = {"tb_ad"}, eventType = {CanalEntry.EventType.UPDATE,CanalEntry.EventType.DELETE,CanalEntry.EventType.INSERT})
    public void onEvent4(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.err.println("廣告跟新了");
//        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
        String position = rowData
                .getAfterColumnsList()
                .stream()
                .filter(column -> column.getName().equals("positon"))
                .limit(1)
                .collect(toList())
                .get(0)
                .getValue();

        System.out.println("position:" + position);

        List<Ad> adByPosition = adFeign.findAdByPosition(position);
        System.out.println(adByPosition);
        String string = JSON.toJSONString(adByPosition);
        redisTemplate.boundValueOps("ad_"+position).set(string);
    }
}

只要數(shù)據(jù)庫有變動(dòng)canal就會(huì)在bin-log日志中監(jiān)控到,然后就會(huì)自動(dòng)更新redis了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • NOSQL類型簡介鍵值對(duì):會(huì)使用到一個(gè)哈希表,表中有一個(gè)特定的鍵和一個(gè)指針指向特定的數(shù)據(jù),如redis,volde...
    MicoCube閱讀 4,156評(píng)論 2 27
  • redis是一個(gè)以key-value存儲(chǔ)的非關(guān)系型數(shù)據(jù)庫。有五種數(shù)據(jù)類型,string、hashes、list、s...
    林ze宏閱讀 1,106評(píng)論 0 0
  • 去年有段時(shí)間得空,就把谷歌GAE的API權(quán)威指南看了一遍,收獲頗豐,特別是在自己幾乎獨(dú)立開發(fā)了公司的云數(shù)據(jù)中心之后...
    騎單車的勛爵閱讀 21,095評(píng)論 0 41
  • 紅色性格,有好東西一定會(huì)分享。我覺得自己也是這樣的,我總喜歡把自己用的東西恨不得全部買下來給家人,我看到的好看的衣...
    小笨魚王月閱讀 108評(píng)論 0 0
  • 今天是我的生日,我的好朋友彭佳樂、劉志恒都來參加我的生日party,我們?cè)诖缶频瓿粤松盏案?,然后在樓下開開心...
    格格的歌閱讀 99評(píng)論 0 0

友情鏈接更多精彩內(nèi)容