springboot集成redission哨兵模式
代码】springboot集成redission哨兵模式。
·
pom引用
<!--springboot中的redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.8.2</version>
</dependency>
RedissonAutoConfiguration
package com.ruijie.returns.config;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author zjl
* @date 2021年12月8日10:44:58
*/
@Configuration
@ConditionalOnClass(Config.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration {
@Autowired
private RedissonProperties redissonProperties;
@Bean
RedissonClient redissonSentinel() {
Config config = new Config();
SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redissonProperties.getSentinelAddresses())
.setMasterName(redissonProperties.getMasterName())
.setTimeout(redissonProperties.getTimeout())
.setMasterConnectionPoolSize(redissonProperties.getMasterConnectionPoolSize())
.setSlaveConnectionPoolSize(redissonProperties.getSlaveConnectionPoolSize());
if(StringUtils.isNotBlank(redissonProperties.getPassword())) {
serverConfig.setPassword(redissonProperties.getPassword());
}
return Redisson.create(config);
}
}
RedissonProperties
package com.ruijie.returns.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author zjl
* @date 2021年12月8日10:40:04
*/
@ConfigurationProperties(prefix = "redisson")
@Data
public class RedissonProperties {
private int timeout = 1000;
private int slaveConnectionPoolSize = 200;
private int masterConnectionPoolSize = 200;
private String[] sentinelAddresses;
private String masterName;
private String password;
}
application.properties
redisson.sentinelAddresses=redis://172.16.2.122:26379,redis://172.16.2.123:26379,redis://172.16.2.124:26379
redisson.masterName=mymaster
redisson.password=CES@_123_$_666
redisson.timout=1000
测试类:
package com.ruijie.returns.service;
import com.ruijie.returns.ReturnsApplicationApp;
import com.ruijie.returns.domain.vo.ReBateLogVO;
import com.ruijie.returns.utils.TestFileUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
@SpringBootTest(classes = ReturnsApplicationApp.class)
@RunWith(SpringRunner.class)
@Slf4j
public class ReRebateLogServiceTest {
@Autowired
private RedissonClient redissonClient;
@Test
public void testRedis() {
RBucket<Object> mytest = redissonClient.getBucket("mytest");
mytest.set("vaule1111");
System.out.println(mytest.get());
}
}
更多推荐
已为社区贡献3条内容
所有评论(0)