Browse Source

day11
重建布隆过滤器;Canal客户端解决一致性问题

it_lv 2 weeks ago
parent
commit
2aa87de813

+ 28 - 0
service/canal-client-service/pom.xml

@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>service</artifactId>
+        <groupId>com.atguigu.tingshu</groupId>
+        <version>1.0</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>canal-client-service</artifactId>
+
+    <properties>
+        <maven.compiler.source>17</maven.compiler.source>
+        <maven.compiler.target>17</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>io.github.xizixuejie</groupId>
+            <artifactId>canal-spring-boot-starter</artifactId>
+            <version>0.0.17</version>
+        </dependency>
+    </dependencies>
+
+</project>

+ 19 - 0
service/canal-client-service/src/main/java/com/atguigu/tingshu/CanalClientApp.java

@@ -0,0 +1,19 @@
+package com.atguigu.tingshu;
+
+import io.xzxj.canal.spring.annotation.EnableCanalListener;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+
+/**
+ * @author: atguigu
+ * @create: 2025-03-21 15:46
+ */
+@EnableCanalListener
+@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
+public class CanalClientApp {
+
+    public static void main(String[] args) {
+        SpringApplication.run(CanalClientApp.class, args);
+    }
+}

+ 23 - 0
service/canal-client-service/src/main/java/com/atguigu/tingshu/listener/UserListener.java

@@ -0,0 +1,23 @@
+package com.atguigu.tingshu.listener;
+
+import com.atguigu.tingshu.model.CDCEntity;
+import io.xzxj.canal.core.annotation.CanalListener;
+import io.xzxj.canal.core.listener.EntryListener;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.Set;
+
+/**
+ * @author: atguigu
+ * @create: 2025-03-21 15:47
+ */
+@Slf4j
+@CanalListener(destination = "tingshuTopic", schemaName = "tingshu_user", tableName = "user_info")
+public class UserListener implements EntryListener<CDCEntity> {
+
+    @Override
+    public void update(CDCEntity before, CDCEntity after, Set<String> fields) {
+        log.info("[cdc]监听到变更数据");
+        EntryListener.super.update(before, after, fields);
+    }
+}

+ 21 - 0
service/canal-client-service/src/main/java/com/atguigu/tingshu/model/CDCEntity.java

@@ -0,0 +1,21 @@
+package com.atguigu.tingshu.model;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import javax.persistence.Column;
+import javax.persistence.Table;
+
+/**
+ *
+ * @author: atguigu
+ * @create: 2023-11-01 16:22
+ */
+@Data
+@TableName("user_info")
+@Table(name = "user_info")
+public class CDCEntity {
+
+    @Column(name = "id")
+    private Long id;
+}

+ 8 - 0
service/canal-client-service/src/main/resources/bootstrap.properties

@@ -0,0 +1,8 @@
+spring.application.name=service-canal
+spring.profiles.active=dev
+spring.main.allow-bean-definition-overriding=true
+spring.cloud.nacos.discovery.server-addr=192.168.200.6:8848
+spring.cloud.nacos.config.server-addr=192.168.200.6:8848
+spring.cloud.nacos.config.prefix=${spring.application.name}
+spring.cloud.nacos.config.file-extension=yaml
+spring.cloud.nacos.config.shared-configs[0].data-id=common.yaml

+ 2 - 0
service/pom.xml

@@ -20,6 +20,8 @@
         <module>service-order</module>
         <module>service-payment</module>
         <module>service-user</module>
+        <module>canal-client-service</module>
+        <module>service-cdc</module>
     </modules>
 
     <dependencies>

+ 9 - 31
service/service-cdc/pom.xml

@@ -2,49 +2,27 @@
 <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-    <modelVersion>4.0.0</modelVersion>
-
-    <groupId>com.atguigu</groupId>
-    <artifactId>service-cdc</artifactId>
-    <version>1.0-SNAPSHOT</version>
-
     <parent>
-        <groupId>org.springframework.boot</groupId>
-        <artifactId>spring-boot-starter-parent</artifactId>
-        <version>2.3.6.RELEASE</version>
+        <artifactId>service</artifactId>
+        <groupId>com.atguigu.tingshu</groupId>
+        <version>1.0</version>
     </parent>
+    <modelVersion>4.0.0</modelVersion>
 
+    <artifactId>service-cdc</artifactId>
 
     <properties>
-        <maven.compiler.source>8</maven.compiler.source>
-        <maven.compiler.target>8</maven.compiler.target>
+        <maven.compiler.source>17</maven.compiler.source>
+        <maven.compiler.target>17</maven.compiler.target>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
 
 
     <dependencies>
-        <!--web 需要启动项目-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-web</artifactId>
-        </dependency>
         <dependency>
-            <groupId>top.javatool</groupId>
+            <groupId>io.github.xizixuejie</groupId>
             <artifactId>canal-spring-boot-starter</artifactId>
-            <version>1.2.1-RELEASE</version>
-        </dependency>
-        <dependency>
-            <groupId>javax.persistence</groupId>
-            <artifactId>persistence-api</artifactId>
-            <version>1.0</version>
-        </dependency>
-        <dependency>
-            <groupId>org.projectlombok</groupId>
-            <artifactId>lombok</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-redis</artifactId>
+            <version>0.0.17</version>
         </dependency>
     </dependencies>
 

+ 5 - 2
service/service-cdc/src/main/java/com/atguigu/tingshu/CDCApplicaiton.java

@@ -1,13 +1,16 @@
 package com.atguigu.tingshu;
 
+import io.xzxj.canal.spring.annotation.EnableCanalListener;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 
 /**
  * @author: atguigu
- * @create: 2023-11-01 16:39
+ * @create: 2025-03-21 15:46
  */
-@SpringBootApplication
+@EnableCanalListener
+@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
 public class CDCApplicaiton {
 
     public static void main(String[] args) {

+ 0 - 38
service/service-cdc/src/main/java/com/atguigu/tingshu/config/RedisConfig.java

@@ -1,38 +0,0 @@
-package com.atguigu.tingshu.config;
-
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.Primary;
-import org.springframework.data.redis.connection.RedisConnectionFactory;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
-import org.springframework.data.redis.serializer.StringRedisSerializer;
-
-/**
- * @author: atguigu
- * @create: 2023-12-23 10:59
- */
-@Configuration
-public class RedisConfig {
-
-    @Bean
-    @Primary
-    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
-        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
-        redisTemplate.setConnectionFactory(redisConnectionFactory);
-
-        //String的序列化方式
-        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
-        // 使用GenericJackson2JsonRedisSerializer 替换默认序列化(默认采用的是JDK序列化)
-        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
-
-        //序列号key value
-        redisTemplate.setKeySerializer(stringRedisSerializer);
-        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
-        redisTemplate.setHashKeySerializer(stringRedisSerializer);
-        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
-
-        redisTemplate.afterPropertiesSet();
-        return redisTemplate;
-    }
-}

+ 0 - 38
service/service-cdc/src/main/java/com/atguigu/tingshu/handler/AlbumInfoCdcHandler.java

@@ -1,38 +0,0 @@
-package com.atguigu.tingshu.handler;
-
-import com.atguigu.tingshu.model.CDCEntity;
-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;
-
-/**
- * @author: atguigu
- * @create: 2023-12-23 10:53
- */
-@Slf4j
-@Component
-@CanalTable("album_info") ////监听变更表
-public class AlbumInfoCdcHandler implements EntryHandler<CDCEntity> {
-
-    @Autowired
-    private RedisTemplate redisTemplate;
-
-    /**
-     * 监听用户表更新/逻辑删除,删除Redis缓存用户信息
-     *
-     * @param before
-     * @param after
-     */
-
-    @Override
-    public void update(CDCEntity before, CDCEntity after) {
-        log.info("监听到专辑表更新/逻辑删除,删除Redis缓存专辑信息");
-        Long id = after.getId();
-        String redisKey = "album:info:" + id;
-        redisTemplate.delete(redisKey);
-    }
-
-}

+ 0 - 38
service/service-cdc/src/main/java/com/atguigu/tingshu/handler/UserCdcHandler.java

@@ -1,38 +0,0 @@
-package com.atguigu.tingshu.handler;
-
-import com.atguigu.tingshu.model.CDCEntity;
-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;
-
-/**
- * @author: atguigu
- * @create: 2023-12-23 10:53
- */
-@Slf4j
-@Component
-@CanalTable("user_info") ////监听变更表
-public class UserCdcHandler implements EntryHandler<CDCEntity> {
-
-    @Autowired
-    private RedisTemplate redisTemplate;
-
-    /**
-     * 监听用户表更新/逻辑删除,删除Redis缓存用户信息
-     *
-     * @param before
-     * @param after
-     */
-
-    @Override
-    public void update(CDCEntity before, CDCEntity after) {
-        log.info("监听到用户表更新/逻辑删除,删除Redis缓存用户信息");
-        Long id = after.getId();
-        String redisKey = "user:userinfo:" + id;
-        redisTemplate.delete(redisKey);
-    }
-
-}

+ 35 - 0
service/service-cdc/src/main/java/com/atguigu/tingshu/listener/UserListener.java

@@ -0,0 +1,35 @@
+package com.atguigu.tingshu.listener;
+
+import com.atguigu.tingshu.model.CDCEntity;
+import io.xzxj.canal.core.annotation.CanalListener;
+import io.xzxj.canal.core.listener.EntryListener;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+
+import java.util.Set;
+
+/**
+ * @author: atguigu
+ * @create: 2025-03-21 15:47
+ */
+@Slf4j
+@CanalListener(destination = "tingshuTopic", schemaName = "tingshu_user", tableName = "user_info")
+public class UserListener implements EntryListener<CDCEntity> {
+
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    /**
+     * 监听用户表更新回调方法
+     * @param before
+     * @param after
+     * @param fields
+     */
+    @Override
+    public void update(CDCEntity before, CDCEntity after, Set<String> fields) {
+        log.info("[cdc]监听到变更数据");
+        String redisKey = "user:userinfo:"+after.getId();
+        redisTemplate.delete(redisKey);
+    }
+}

+ 2 - 0
service/service-cdc/src/main/java/com/atguigu/tingshu/model/CDCEntity.java

@@ -3,6 +3,7 @@ package com.atguigu.tingshu.model;
 import lombok.Data;
 
 import javax.persistence.Column;
+import javax.persistence.Table;
 
 /**
  *
@@ -10,6 +11,7 @@ import javax.persistence.Column;
  * @create: 2023-11-01 16:22
  */
 @Data
+@Table(name = "user_info")
 public class CDCEntity {
 
     @Column(name = "id")

+ 0 - 8
service/service-cdc/src/main/resources/application-dev.yaml

@@ -1,8 +0,0 @@
-#canal配置
-canal:
-  server: 192.168.200.6:11111
-  destination: tingshuTopic #Canal服务端发送数据的话题名称跟上面容器里参数destinations的一样
-spring:
-  redis:
-    host: 192.168.200.6
-    port: 6379

+ 0 - 3
service/service-cdc/src/main/resources/application.yml

@@ -1,3 +0,0 @@
-spring:
-  profiles:
-    active: dev

+ 8 - 0
service/service-cdc/src/main/resources/bootstrap.properties

@@ -0,0 +1,8 @@
+spring.application.name=service-canal
+spring.profiles.active=dev
+spring.main.allow-bean-definition-overriding=true
+spring.cloud.nacos.discovery.server-addr=192.168.200.6:8848
+spring.cloud.nacos.config.server-addr=192.168.200.6:8848
+spring.cloud.nacos.config.prefix=${spring.application.name}
+spring.cloud.nacos.config.file-extension=yaml
+spring.cloud.nacos.config.shared-configs[0].data-id=common.yaml

+ 0 - 59
service/service-cdc/src/main/resources/logback-spring.xml

@@ -1,59 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<configuration>
-
-    <contextName>logback</contextName>
-
-    <!-- 日志的输出目录 -->
-    <property name="log.path" value="D://work//tingshu_work//logs" />
-
-    <!--控制台日志格式:彩色日志-->
-    <!-- magenta:洋红 -->
-    <!-- boldMagenta:粗红-->
-    <!-- cyan:青色 -->
-    <!-- white:白色 -->
-    <!-- magenta:洋红 -->
-    <property name="CONSOLE_LOG_PATTERN"
-              value="%yellow(%date{yyyy-MM-dd HH:mm:ss}) %highlight([%-5level]) %green(%logger) %msg%n"/>
-
-    <!--文件日志格式-->
-    <property name="FILE_LOG_PATTERN"
-              value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] %thread %file:%line %logger %msg%n" />
-
-    <!--编码-->
-    <property name="ENCODING" value="UTF-8" />
-
-    <!-- 控制台日志 -->
-    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
-        <!-- 临界值过滤器 -->
-        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
-            <level>INFO</level>
-        </filter>
-        <encoder>
-            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
-            <charset>${ENCODING}</charset>
-        </encoder>
-    </appender>
-
-    <!-- 文件日志 -->
-    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
-        <file>${log.path}//log.log</file>
-        <append>true</append>
-        <encoder>
-            <pattern>%date{yyyy-MM-dd HH:mm:ss} %msg%n</pattern>
-            <charset>${ENCODING}</charset>
-        </encoder>
-    </appender>
-
-
-    <!-- 开发环境 -->
-    <springProfile name="dev">
-        <!-- com.atguigu日志记录器:业务程序INFO级别  -->
-        <logger name="com.atguigu" level="INFO" />
-        <logger name="top.javatool" level="WARN" />
-        <!-- 根日志记录器:INFO级别  -->
-        <root level="INFO">
-            <appender-ref ref="CONSOLE" />
-        </root>
-    </springProfile>
-
-</configuration>