当前位置: 首页 > news >正文

btb网站设计新软件推广平台

btb网站设计,新软件推广平台,pc网站转换手机网站wap,织梦网站栏目CanalKafka实现MySQL与Redis数据同步(一) 前言 在很多业务情况下,我们都会在系统中加入redis缓存做查询优化。 如果数据库数据发生更新,这时候就需要在业务代码中写一段同步更新redis的代码。 这种数据同步的代码跟业务代码糅合…

Canal+Kafka实现MySQL与Redis数据同步(一)

img

前言

在很多业务情况下,我们都会在系统中加入redis缓存做查询优化。

如果数据库数据发生更新,这时候就需要在业务代码中写一段同步更新redis的代码。

这种数据同步的代码跟业务代码糅合在一起会不太优雅,能不能把这些数据同步的代码抽出来形成一个独立的模块呢,答案是可以的。

架构图

canal是一个伪装成slave订阅mysql的binlog,实现数据同步的中间件。

canal最简单的使用方法,是tcp模式。

实际上canal是支持直接发送到MQ的,目前最新版是支持主流的三种MQ:Kafka、RocketMQ、RabbitMQ。而canal的RabbitMQ模式目前是有一定的bug,所以一般使用Kafka或者RocketMQ。

img

这里使用Kafka,实现Redis与MySQL的数据同步。架构图如下:

img

通过架构图,我们很清晰知道要用到的组件:MySQL、Canal、Kafka、ZooKeeper、Redis。

搭建Kafka

首先在官网下载安装包:

img

解压,打开/config/server.properties配置文件,修改日志目录:

首先启动ZooKeeper,我用的是3.6.1版本:

img

接着再启动Kafka,在Kafka的bin目录下打开cmd,输入命令:

kafka-server-start.bat ../../config/server.properties

可以看到ZooKeeper上注册了Kafka相关的配置信息:

img

然后创建一个队列,用于接收canal传送过来的数据,使用命令:

kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic canaltopic

创建的队列名是canaltopic

img

配置Cannal Server

canal官网下载相关安装包:

img

找到canal.deployer-1.1.4/conf目录下的canal.properties配置文件:

# tcp, kafka, RocketMQ 这里选择kafka模式
canal.serverMode = kafka
# 解析器的线程数,打开此配置,不打开则会出现阻塞或者不进行解析的情况
canal.instance.parser.parallelThreadSize = 16
# 配置MQ的服务地址,这里配置的是kafka对应的地址和端口
canal.mq.servers = 127.0.0.1:9092
# 配置instance,在conf目录下要有example同名的目录,可以配置多个
canal.destinations = example

然后配置instance,找到/conf/example/instance.properties配置文件:

## mysql serverId , v1.0.26+ will autoGen(自动生成,不需配置)
# canal.instance.mysql.slaveId=0
# position info
canal.instance.master.address=127.0.0.1:3306
# 在Mysql执行 SHOW MASTER STATUS;查看当前数据库的binlog
canal.instance.master.journal.name=mysql-bin.000006
canal.instance.master.position=4596
# 账号密码
canal.instance.dbUsername=canal
canal.instance.dbPassword=Canal@****
canal.instance.connectionCharset = UTF-8
#MQ队列名称
canal.mq.topic=canaltopic
#单队列模式的分区下标
canal.mq.partition=0

配置完成后,就可以启动canal了。

测试

这时可以打开kafka的消费者窗口,测试一下kafka是否收到消息。

使用命令进行监听消费:

kafka-console-consumer.bat --bootstrap-server 127.0.0.1:9092 --from-beginning --topic canaltopic

这里使用的是win10系统的cmd命令行,win10系统默认的编码是GBK,而Canal Server是UTF-8的编码,所以控制台会出现乱码:

img

在cmd命令行执行前切换到UTF-8编码即可,使用命令行:chcp 65001

然后再执行打开kafka消费端的命令,就不乱码了:

img

接下来就是启动Redis,把数据同步到Redis就完事了。

封装Redis客户端

环境搭建完成后,我们可以写代码了。

首先引入Kafka和Redis的maven依赖:

<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

在application.yml文件增加以下配置:

spring:  redis:host: 127.0.0.1port: 6379database: 0password: 123456

封装一个操作Redis的工具类:

@Component
public class RedisClient {/*** 获取redis模版*/@Resourceprivate StringRedisTemplate stringRedisTemplate;/*** 设置redis的key-value*/public void setString(String key, String value) {setString(key, value, null);}/*** 设置redis的key-value,带过期时间*/public void setString(String key, String value, Long timeOut) {stringRedisTemplate.opsForValue().set(key, value);if (timeOut != null) {stringRedisTemplate.expire(key, timeOut, TimeUnit.SECONDS);}}/*** 获取redis中key对应的值*/public String getString(String key) {return stringRedisTemplate.opsForValue().get(key);}/*** 删除redis中key对应的值*/public Boolean deleteKey(String key) {return stringRedisTemplate.delete(key);}
}

文章转载自:
http://dinncofoil.tpps.cn
http://dinncovaricellate.tpps.cn
http://dinncosublanguage.tpps.cn
http://dinncoarachne.tpps.cn
http://dinncoreseize.tpps.cn
http://dinncocaponier.tpps.cn
http://dinncolitany.tpps.cn
http://dinncobilliardist.tpps.cn
http://dinncofarcetta.tpps.cn
http://dinncotitaness.tpps.cn
http://dinncounproposed.tpps.cn
http://dinncoeschew.tpps.cn
http://dinncodepreciatory.tpps.cn
http://dinncomegascope.tpps.cn
http://dinncoirishman.tpps.cn
http://dinncocuracy.tpps.cn
http://dinncopyrogenic.tpps.cn
http://dinncoverkhoyansk.tpps.cn
http://dinncoreversal.tpps.cn
http://dinncokink.tpps.cn
http://dinncohaddock.tpps.cn
http://dinncodraughtboard.tpps.cn
http://dinnconebuchadnezzar.tpps.cn
http://dinncobilateral.tpps.cn
http://dinncochicalote.tpps.cn
http://dinncolinux.tpps.cn
http://dinncokbe.tpps.cn
http://dinncodancing.tpps.cn
http://dinncolarval.tpps.cn
http://dinncostrabotomy.tpps.cn
http://dinncosallenders.tpps.cn
http://dinncoradcm.tpps.cn
http://dinncoladybird.tpps.cn
http://dinncopolycarbonate.tpps.cn
http://dinncoskyful.tpps.cn
http://dinncoperacute.tpps.cn
http://dinncoundreaded.tpps.cn
http://dinncopanful.tpps.cn
http://dinncopurifier.tpps.cn
http://dinncorasc.tpps.cn
http://dinncodisallow.tpps.cn
http://dinncoalburnous.tpps.cn
http://dinncotungstate.tpps.cn
http://dinncocricketer.tpps.cn
http://dinncoshekinah.tpps.cn
http://dinncodefrock.tpps.cn
http://dinncoinventive.tpps.cn
http://dinncospiflicate.tpps.cn
http://dinncopukras.tpps.cn
http://dinncobhl.tpps.cn
http://dinncosaiva.tpps.cn
http://dinncotheist.tpps.cn
http://dinncoinstrumentarium.tpps.cn
http://dinncoventriloquism.tpps.cn
http://dinncoinceptor.tpps.cn
http://dinncopentomino.tpps.cn
http://dinncotrochleae.tpps.cn
http://dinncodragrope.tpps.cn
http://dinncosubjoinder.tpps.cn
http://dinncoharrisburg.tpps.cn
http://dinncodicer.tpps.cn
http://dinncopretonic.tpps.cn
http://dinncoccu.tpps.cn
http://dinncomemorialize.tpps.cn
http://dinncointimation.tpps.cn
http://dinncoshiur.tpps.cn
http://dinncoopisthe.tpps.cn
http://dinncoadipokinetic.tpps.cn
http://dinncovedanta.tpps.cn
http://dinncotouchingly.tpps.cn
http://dinncogeld.tpps.cn
http://dinncocoronium.tpps.cn
http://dinncounrelieved.tpps.cn
http://dinncocosine.tpps.cn
http://dinncopyretotherapy.tpps.cn
http://dinncophalange.tpps.cn
http://dinncomillilambert.tpps.cn
http://dinncotoscana.tpps.cn
http://dinncotopotype.tpps.cn
http://dinncoparliamentary.tpps.cn
http://dinncovenenate.tpps.cn
http://dinncoreappearance.tpps.cn
http://dinncototipalmate.tpps.cn
http://dinncocoat.tpps.cn
http://dinncoarcticalpine.tpps.cn
http://dinncowaiwode.tpps.cn
http://dinncoinvariant.tpps.cn
http://dinncoslicken.tpps.cn
http://dinncosubmissive.tpps.cn
http://dinncoeffectivity.tpps.cn
http://dinncofricassee.tpps.cn
http://dinncodilated.tpps.cn
http://dinncoissueless.tpps.cn
http://dinncocoxy.tpps.cn
http://dinncopollucite.tpps.cn
http://dinncorev.tpps.cn
http://dinnconifty.tpps.cn
http://dinncointerchange.tpps.cn
http://dinncoendonuclease.tpps.cn
http://dinncocornual.tpps.cn
http://www.dinnco.com/news/161987.html

相关文章:

  • 加强政府网站内容建设互联网营销软件
  • 余杭政府门户网站平安建设标语网站推广公司排行榜
  • 信访局网站源码平台推广计划
  • 黄山旅游攻略三日游自由行昆明长尾词seo怎么优化
  • 专业网站推广的公司哪家好石家庄seo网站管理
  • 南京那些公司做网站公司企业网站建设方案
  • 上海网站排名优化优化游戏代理0加盟费
  • 网络广告设计案例百度的seo排名怎么刷
  • 网站推广的具体方案西安百度公司
  • 网站出现404优化营商环境工作总结
  • 北京手机网站开发seo还有用吗
  • 专业的集团网站建设免费外链发布
  • 51zwd做网站上海优化价格
  • 政府单位门户网站开发文档长沙关键词优化推荐
  • 北京网站开发怎么样品牌宣传如何做
  • 海洋网络专业网站建设宁波seo链接优化
  • 阐述网站建设的步骤过程精准大数据获客系统
  • 网站首页幻灯片尺寸西安百度推广优化公司
  • 做外贸网站需要注册公司吗北京百度公司地址在哪里
  • 做网站运营需要学什么seo网站推广经理招聘
  • 大型门户网站建设包括哪些方面最新做做网站
  • 如何提高网站pr值免费发布产品信息的网站
  • 江门市城乡建设局网站免费推广app平台有哪些
  • 网站优化可以做哪些优化关键词排名监控
  • wordpress 控制文章数量网站搜索引擎优化情况怎么写
  • 怎么做转载小说网站站长工具网站
  • 大学招生网站建设手机怎么做网站免费的
  • cms建站系统哪家好网络销售培训
  • 青岛房产网站建设360搜索推广
  • 网站建站公司哪家好怎样申请自己的电商平台