在我们的上一篇文章中我们搭建了环境一台服务器上使用docker安装redis6.0.10一主两从以及哨兵模式。
现在我们在这里使用到项目中
创建springboot项目,整合web,swagger2,redis,lombok即可
引入pom(使用Jedis)
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
<!--spirngboot版本为2.x-->
<!-- 加载spring boot redis包,springboot2.0中直接使用jedis或者lettuce配置连接池,默认为lettuce连接池,这里使用jedis连接池 -->
<!-- 加载spring boot redis包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<!-- 排除lettuce包,使用jedis代替-->
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
先以test运行,看看我们的哨兵节点是否可正常连接
发现抱错了,看见圈红框中的内容,想到了上篇文章中我们配置的sentinel1,sentinel2,sentinel3目录下的conf的sentinel.conf有这么一段:
当时这么配置想着容器之间相互连通就可以,但是这样配置我们在外网又访问不到,那么我们是否可以配置为外网的ip,在容器之间也可以相互访问,在外网也可以提供给我们访问
于是我便修改为:
sentinel monitor mymaster 服务器外网ip 6379 2(注意:如果是阿里云需要把设置的6个端口全部放行)
然后重新启动sentinel-01,sentinel-02,sentinel-03
重新运行程序:
可以说明连接成功!
接下来就用swagger api写个简单的赋值删除程序,熟练一下配置redis哨兵模式!
代码不贴出了,上传到百度网盘了!
链接:https://pan.baidu.com/s/11dYk42L0jR0_bmh8Eo88Bg
提取码:iwjq
springboot-redis: