引入依赖:
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.10.7</version> </dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
springboot 集成:
import cn.hutool.core.util.StrUtil; import com.alibaba.excel.util.StringUtils; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.client.codec.StringCodec; import org.redisson.config.Config; import org.redisson.config.ReadMode; import org.redisson.config.SentinelServersConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; import java.io.IOException; @Configuration @Data @Slf4j public class RedisConfig { @Value("${redis.nodes:127.0.0.1:26379}") private String nodes; @Value("${redis.password:test}") private String password; @Value("${redis.timeout:2000}") private int timeout; @Value("${redis.masterName:mymaster}") private String masterName; private Boolean printConfig = true; private RedissonClient redissonClient; @PostConstruct public void init(){ genRedissionClient(); } private void genRedissionClient(){ String[] nodeArray = nodes.split(","); Config config = new Config(); StringCodec stringCodec = new StringCodec(); config.setCodec(stringCodec); if (nodeArray.length == 1) { //单节点配置 config.useSingleServer() .setAddress(StrUtil.format("redis://{}", nodeArray[0])) .setPassword(password) .setTimeout(timeout); } else { //集群化配置 SentinelServersConfig sentinelServersConfig = config.useSentinelServers(); for (String node : nodeArray) { sentinelServersConfig.addSentinelAddress(StrUtil.format("redis://{}", node)); } sentinelServersConfig.setTimeout(timeout); sentinelServersConfig.setMasterName(masterName); sentinelServersConfig.setReadMode(ReadMode.SLAVE); //设置密码 if (StringUtils.isNotBlank(password)) { sentinelServersConfig.setPassword(password); } } if(printConfig) { try { //打印配置 log.debug(config.toYAML()); } catch (IOException e) { e.printStackTrace(); } printConfig = false; } if(null != redissonClient){ redissonClient.shutdown(); } redissonClient = Redisson.create(config); } }
application.yml:
redis: nodes: 127.0.0.1:6369,127.0.0.1:6368,127.0.0.1:6367 password: 123abc masterName: sss
标签:redisson,springboot,redis,模式,private,org,import,config,redssion From: https://www.cnblogs.com/goPush/p/17639846.html