一、redis集群配置
redis集群配置
二、集成集群使用步骤
1.配置文件
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=11111
spring.datasource.url=jdbc:mysql://localhost:3306/xxx?serverTimezone=GMT%2B8
spring.redis.cluster.max-redirects=8
spring.redis.cluster.nodes=192.168.182.130:7001,192.168.182.130:7002,192.168.182.130:7003,192.168.182.130:7004,192.168.182.130:7005,192.168.182.130:7006
spring.redis.cluster.timeout=6000
#serverTimezone=UTC
server.servlet.context-path=/
..
导入相关坐标
实体类
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisProperties {
private int expireSeconds;
private String nodes;
private int timeout;
......
setter,getter...
}
配置类
@Configuration
public class JedisClusterConfig {
@Autowired
private RedisProperties redisProperties;
public JedisCluster getJedisCluster() {
String[] serverArray = redisProperties.getNodes().split(",");
Set<HostAndPort> nodes = new HashSet<>();
for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
}
return new JedisCluster(nodes, redisProperties.getTimeout());
}
}
### 配置模板类
@Service
public class RedisClientTemplate {
private static final Logger log = LoggerFactory.getLogger(RedisClientTemplate.class);
@Autowired
private JedisClusterConfig jedisClusterConfig;
public boolean setToRedis(String key, Object value) {
try {
String str = jedisClusterConfig.getJedisCluster().set(key, String.valueOf(value));
if ("OK".equals(str)) return true;
} catch (Exception ex) {
log.error("setToRedis:{Key:" + key + ",value" + value + "}", ex);
}
return false;
}
public Object getRedis(String key) {
String str = null;
try {
str = jedisClusterConfig.getJedisCluster().get(key);
} catch (Exception ex) {
log.error("getRedis:{Key:" + key + "}", ex);
}
return str;
}
}
github案例redis集群环境
使用
生成了模板类,加入到容器里,可以直接调用
测试环境的话,
springboot
在测试类的启动类上加上注解
@RunWith(SpringRunner.class)
@SpringBootTest
注入redis集群依赖,就可以使用了
注意,使用集群,要注意的点:
查看在linux环境下有没有给对应的端口配置能被外网访问的端口
配置端口文件位置: /etc/sysconfig/iptables
以及是否关闭了防火墙:
centos6:
查看防火墙状态(service iptables status或者/etc/init.d/iptables status)
service iptables stop 命令关闭防火墙,但是系统重启后会开始
chkconfig iptables off--关闭防火墙开机自启动
service iptables start 开启防火墙
centos7:
查看防火墙
systemctl status firewalld.service
active(running) 表示已经开启
systemctl stop firewalld.service 关闭,但重启会失效
非集群环境
配置文件
spring.datasource.url=jdbc:mysql://localhost:3306?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.password=11111
spring.datasource.name=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.redis.host=192.168.182.130
spring.redis.port=6380
导入相关坐标
就完事了
还可以这样玩,不需要配置文件的
Jedis jedis=new Jedis(host,port);
但是不建议,可以拿来测试,
说到测试
其实可以在测试类上
@SpringBootTest
@RunWith(SpringRunner.class)
@ConfigurationProperties("spring.redis") 指定配置文件的该属性下的所有属性被加载到该类
由于我们只配了这两个参数,所以只写两个
String port="";
String host="";
当然需要提供getter,setter方法,否则无法获得属性值
然后对redis的一些操作,五种数据类型的操作等
github非集群环境redis