首页天道酬勤rediscluster集群原理,redis-cli连接redis数据库

rediscluster集群原理,redis-cli连接redis数据库

张世龙 05-03 20:46 79次浏览

文章目录1 .依赖2 .参数配置3 .代码实现部署

1.dependencygroupidredis.clients/groupidartifactidjedis/artifactidversion2.9.0/version/dependency2.参数配置# Redis群集服务器地址redis.host1=192.168.000.11 redis.host2=192.168.000.12 redis.host3=192.168.000.13 # redis服务器连接redis服务器连接密码(默认为空) redis.password=xxxx#连接超时时间redis.connection-timeout=2000#读取数据超时打开连接超时或读取超时重试对象验证,以确保可以在redis.testOnBorrow=true 3.代码中实现import lombok.extern.slf4j.slf4j import redis.clients.jedis.hostandport; import redis.clients.jedis.jedis cluster; import redis.clients.jedis.jedis poolconfig; import java.io.IOException; import java.util.HashSet; import java.util.Properties; import java.util.Set; /** * Redis Cluster配置* * @ author wangbo * @ date 2021/6/15 */@ sl F4 jpublicclassjedisclustermanager {私有jedisclusterer try { props.load (jedisclustermanager.class.getresourceasstream (properties constants.properties _ file _ redis ) ) } stringredishost1=props.getproperty (' redis.host1); stringredishost2=props.getproperty (redis.host2); stringredishost3=props.getproperty (redis.host3); intmasterport=integer.parseint (props.getproperty (' redis.master.port ' ); intslaveport=integer.parseint (props.getproperty (' redis.slave.port ' ); SetHostAndPort nodes=new HashSet (; nodes.add (newhostandport (redis host 1,masterPort ); nodes.add (newhostandport (redis host 2,masterPort ); nodes.add (newhostandport (redis host 3,masterPort ); nodes.add (newhostandport (redis host 1,slavePort ) ); nodes.add (newhostandport (redis host 2,slavePort ) ); nodes.add (newhostandport (redis host 3,slavePort ) ); string password=props.getproperty (redis.password ); intconnectiontimeout=integer.parseint (props.getproperty (redis.connection-time out ) ); intsotimeout=integer.parseint (props.getproperty (' redis.so-time out ' ); intmax attempts=integer.parseint (props.getproperty (' redis.max-attempts ' ); booleantestonborrow=boolean.parse boolean (props.getproperty (redis.testonborrow ) ); jedispoolconfigjedispoolconfig=newjedispoolconfig (; jedis poolconfig.settestonborrow (testonborrow; jdis _ cluster=newjediscluster (nodes,connectionTimeout,soTimeout,maxAttempts,password,jedisPoolConfig ); }/***Jediscluster对象*/publicstaticjedisclustergetjedis ({ return jedis _ cluster; }

然后,要检索JedisCluster对象,请使用Jedis提供的各种Redis操作方法。

jedisclusterjediscluster=jedisclustermanager.get jedis (;

jedis连接redis,redis5集群