SpringBoot+Redis哨兵模式的实现_第1页
SpringBoot+Redis哨兵模式的实现_第2页
SpringBoot+Redis哨兵模式的实现_第3页
SpringBoot+Redis哨兵模式的实现_第4页
SpringBoot+Redis哨兵模式的实现_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第SpringBoot+Redis哨兵模式的实现最近学习到了Redis的哨兵模式,光看视频还不行,需要自己动手实现一遍才能加深映像,特此记录。

由于没有真实的服务器可以供我操作,所以在虚拟机上启动了3个redis服务,分别占用7001、7002、7003端口。

Redis下载安装不多赘述,只在这里记录一下配置。

首先在tmp目录下创建3个文件夹:

cd/tmp

mkdir700170027003

然后将redis的配置文件redis.conf拷贝到刚刚创建的3个文件夹下

cpredis-6.2.6/redis.conf/tmp/7001

cpredis-6.2.6/redis.conf/tmp/7002

cpredis-6.2.6/redis.conf/tmp/7003

接着修改这3个配置文件

viredise.conf

找到端口,redis默认端口是6379,这里分别将端口改为7001、7002和7003

然后修改dir,redis持久化文件保存的路径,分别改为对应的路径

接着注释掉bind并且修改protected-mode为no

redis默认不允许远程连接,修改这2项配置允许我们远程连接

最后在配置文件第一行加上replica-announce-ip#{ip}

注意:这里#{ip}填自己的ip地址

由于是在虚拟机安装的redis,会有多个ip,这里写明ip防止找不到

3个配置文件都改完后,cd到对应的目录启动redis

3个服务都启动后,连接7002的redis

redis-cli-p7002

输入命令,搭建主从集群,让7002成为7001的从节点

REPLICAOF#{ip}7001

注意:这里#{ip}填自己的ip地址

同理,7003也这样操作一遍,这样就搭建好了以7001为主节点,7002和7003位从节点的主从集群模式。

需要注意的是,以命令形式搭建的主从集群,重启后就失效了,想要持久保持可以在配置文件里配置,这里从简就不贴了。

上述操作完成后,接着在tmp目录下创建3个新文件夹

mkdirs1s2s3

cd到s1目录,创建文件sentinel.conf,这个文件也可以从redis的目录中拷贝。

编写文件配置

#sentinel端口

port27001

#工作路径

dir"/tmp/s1"

#哨兵监控的master,主从配置一样,在进行主从切换时7001会变成当前的master端口,最后的2为客观判断主节#点下线的节点个数

sentinelmonitormymaster#{ip}70012

#master或slave多长时间不能使用后标记为s_down状态

sentineldown-after-millisecondsmymaster5000

#若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),

#则认为本次failover失败

sentinelfailover-timeoutmymaster60000

注意:这里#{ip}填自己的ip地址

然后将sentinel.conf文件cp到s2和s3路径下,只用修改port和dir为各自的配置

然后分别在各自路径下启动3个哨兵

redis-sentinelsentinel.conf

由于之前测试了7001关闭服务,哨兵自动切换主节点为7002了,若为第一次启动,日志和截图中的会稍有不同。

哨兵模式搭建好后,接着在Java端集成此模式

pom.xml引入最基本的依赖即可

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

exclusions!--去掉springboot默认配置--

exclusion

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-logging/artifactId

/exclusion

/exclusions

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-data-redis/artifactId

/dependency

dependency

groupIdcom.alibaba/groupId

artifactIdfastjson/artifactId

version1.2.73/version

/dependency

application.xml

spring:

redis:

sentinel:

master:mymaster

nodes:

-#{ip}:27001

-#{ip}:27002

-#{ip}:27003

注意:这里#{ip}填自己的ip地址

在一个配置类里注入一个bean,实现redis读写分离,配置从redis读数据时优先从从节点读取

packagecom.wl.demo.config;

importcom.alibaba.fastjson.serializer.SerializerFeature;

importcom.alibaba.fastjson.support.config.FastJsonConfig;

importcom.alibaba.fastjson.support.spring.FastJsonRedisSerializer;

importio.lettuce.core.ReadFrom;

importorg.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;

importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.Configuration;

importorg.springframework.data.redis.connection.RedisConnectionFactory;

importorg.springframework.data.redis.core.RedisTemplate;

importorg.springframework.data.redis.serializer.StringRedisSerializer;

*@authorwl

*@date2025/3/28

@Configuration

publicclassRedisConfig{

@Bean

publicLettuceClientConfigurationBuilderCustomizerlettuceClientConfigurationBuilderCustomizer(){

returnbuilder-builder.readFrom(ReadFrom.REPLICA_PREFERRED);

@Bean

publicRedisTemplateString,ObjectredisTemplate(RedisConnectionFactoryconnectionFactory){

RedisTemplateString,ObjectredisTemplate=newRedisTemplate();

redisTemplate.setConnectionFactory(connectionFactory);

FastJsonRedisSerializerObjectfastJsonRedisSerializer=newFastJsonRedisSerializer(Object.class);

FastJsonConfigfastJsonConfig=fastJsonRedisSerializer.getFastJsonConfig();

SerializerFeature[]serializerFeatures=newSerializerFeature[]{SerializerFeature.WriteDateUseDateFormat,SerializerFeature.WriteMapNullValue};

fastJsonConfig.setSerializerFeatures(serializerFeatures);

StringRedisSerializerstringRedisSerializer=newStringRedisSerializer();

redisTemplate.setKeySerializer(stringRedisSerializer);

redisTemplate.setHashKeySerializer(stringRedisSerializer);

redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);

redisTemplate.setValueSerializer(fastJsonRedisSerializer);

redisTemplate.setEnableTransactionSupport(true);

redisTemplate.afterPropertiesSet();

returnredisTemplate;

}

编写一个测试接口

packagecom.wl.demo.controller;

importmon.result.HttpResult;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.data.redis.core.StringRedisTemplate;

importorg.springframework.web.bind.annotation.GetMapping;

importorg.springframework.web.bind.annotation.PathVariable;

importorg.springframework.web.bind.annotation.RestController;

*@authorwl

*@date2025/4/14

@RestController

publicclassTestController{

privatefinalStringRedisTemplatestringRedisTemplate;

@Autowired

publicTestController(StringRedisTemplatestringRedisTemplate){

this.stringRedisTemplate=stringRedisTemplate;

@GetMapping("/set/{key}/{value}")

publicHttpResultsetValue(@PathVariable("key")Stringkey,@PathVariable("value")Stringvalue){

stringRedisTemplate.opsForValue().set(key,value);

returnHttpResult.success();

@GetMapping("/get/{key}")

publicHttpResultgetValue(@PathVariable("key")Stringkey){

returnHttpResult.success(stringRedisTemplate.opsForValue().get(key));

}

启动springboot,调用set接口

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论