springboot基础-redis集群
一.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.freeht</groupId>
<artifactId>springbootredis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootredis</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
二.application-redis.yml
spring:
redis:
cluster:
#设置key的生存时间,当key过期时,它会被自动删除;
expire-seconds: 120
#设置命令的执行时间,如果超过这个时间,则报错;
command-timeout: 5000
#设置redis集群的节点信息,其中namenode为域名解析,通过解析域名来获取相应的地址;
nodes: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
三.application.properties
spring.profiles.active=redis
四.RedisProperties.java(获取application-redis.yml里面的内容)
package com.freeht.springbootredis.config.reids; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /**
* 获取redis.properties内容
*/
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisProperties { private Integer expireSeconds; private String nodes; private Integer commandTimeout; public Integer getExpireSeconds() {
return expireSeconds;
} public void setExpireSeconds(Integer expireSeconds) {
this.expireSeconds = expireSeconds;
} public String getNodes() {
return nodes;
} public void setNodes(String nodes) {
this.nodes = nodes;
} public Integer getCommandTimeout() {
return commandTimeout;
} public void setCommandTimeout(Integer commandTimeout) {
this.commandTimeout = commandTimeout;
} @Override
public String toString() {
return "RedisProperties{" +
"expireSeconds=" + expireSeconds +
", nodes='" + nodes + '\'' +
", commandTimeout=" + commandTimeout +
'}';
}
}
五.RedisConfig.java(生成redis集群)
package com.freeht.springbootredis.config.reids; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster; import java.util.HashSet;
import java.util.Set; /**
* 生成redis集群
*/
@Configuration
public class RedisConfig { @Autowired
private RedisProperties redisProperties; @Bean
public JedisCluster getJedisCluster(){
//获取redis集群的ip及端口号等相关信息;
String[] serverArray = redisProperties.getNodes().split(",");
Set<HostAndPort> nodes = new HashSet<>();
//遍历add到HostAndPort中;
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.getCommandTimeout());
}
}
六.JedisClient.java
package com.freeht.springbootredis.config.reids; /**
* 调用redis方法接口
*/
public interface JedisClient {
String set(String key, String value); String get(String key); Boolean exists(String key); Long expire(String key, int seconds); Long ttl(String key); Long incr(String key); Long hset(String key, String field, String value); String hget(String key, String field); Long hdel(String key, String... field);
}
七.JedisClientCluster.java
package com.freeht.springbootredis.config.reids; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster; /**
* 实现redis方法
*/
@Component
public class JedisClientCluster implements JedisClient {
@Autowired
private JedisCluster jedisCluster; @Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
} @Override
public String get(String key) {
return jedisCluster.get(key);
} @Override
public Boolean exists(String key) {
return jedisCluster.exists(key);
} @Override
public Long expire(String key, int seconds) {
return jedisCluster.expire(key, seconds);
} @Override
public Long ttl(String key) {
return jedisCluster.ttl(key);
} @Override
public Long incr(String key) {
return jedisCluster.incr(key);
} @Override
public Long hset(String key, String field, String value) {
return jedisCluster.hset(key, field, value);
} @Override
public String hget(String key, String field) {
return jedisCluster.hget(key, field);
} @Override
public Long hdel(String key, String... field) {
return jedisCluster.hdel(key, field);
}
}
八.TestControl.java
package com.freeht.springbootredis.control; import com.freeht.springbootredis.config.reids.JedisClientCluster;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class TestControl { @Autowired
private JedisClientCluster jedisClientCluster; @RequestMapping(value = "set")
public void set(){
jedisClientCluster.set("12","12");
} @RequestMapping(value = "get")
public String get() {
return jedisClientCluster.get("12");
}
}
九.SpringbootredisApplication.java
package com.freeht.springbootredis; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication
@EnableCaching
public class SpringbootredisApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootredisApplication.class, args);
} }
springboot基础-redis集群的更多相关文章
- springboot+shiro+redis(集群redis版)整合教程
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3.springboot+shiro+redis(单机red ...
- SpringBoot整合Redis集群
一.环境搭建 Redis集群环境搭建:https://www.cnblogs.com/zwcry/p/9174233.html 二.Spring整合Redis集群 1.pom.xml <proj ...
- springboot和Redis集群版的整合
此篇接上一个文章springboot和Redis单机版的整合 https://www.cnblogs.com/lin530/p/12019023.html 下面接着介绍和Redis集群版的整合. 1. ...
- springboot集成redis集群
1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- (七)整合 Redis集群 ,实现消息队列场景
整合 Redis集群 ,实现消息队列场景 1.Redis集群简介 1.1 RedisCluster概念 2.SpringBoot整合Redis集群 2.1 核心依赖 2.2 核心配置 2.3 参数渲染 ...
- SpringBoot系列教程之Redis集群环境配置
之前介绍的几篇redis的博文都是基于单机的redis基础上进行演示说明的,然而在实际的生产环境中,使用redis集群的可能性应该是大于单机版的redis的,那么集群的redis如何操作呢?它的配置和 ...
- Redis集群的搭建及与SpringBoot的整合
1.概述 之前聊了Redis的哨兵模式,哨兵模式解决了读的并发问题,也解决了Master节点单点的问题. 但随着系统越来越庞大,缓存的数据越来越多,服务器的内存容量又成了问题,需要水平扩容,此时哨兵模 ...
- Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置
Redis简介 Redis是一个基于C语言开发的开源(BSD许可),开源高性能的高级内存数据结构存储,用作数据库.缓存和消息代理.它支持数据结构,如 字符串.散列.列表.集合,带有范围查询的排序集,位 ...
- SpringBoot整合NoSql--(三)Redis集群
(1)集群原理 在Redis集群中,所有的Redis节点彼此互联,节点内部使用二进制协议优化传输速度和带宽. 当一个节点挂掉后,集群中超过半数的节点检测失效时才认为该节点已失效.不同于Tomcat集群 ...
随机推荐
- maven工程根项目运行ok但是子项目就报错的解决办法
正常启动没错 项目出现问题 maven工程根项目运行ok但是子项目就报错 报错信息是xxxx没有创建 解决办法 原来是子项目的依赖少了 没有配置1.8 所以会出现莫明其妙的bug
- onbeforeunload事件兼容性操作
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 引入 JPEGCodec;JPEGImageEncoder; 图片处理;MyEclipse编译时报错处理
在Eclipse中处理图片,需要引入两个包: import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JP ...
- python-pymysql学习
python 操作mysql:有两种方式python-mysqldb(python3.0之后不能安装)和pymysql,下面是pymysql的学习. 参照表: python代码实现的mysql查询功能 ...
- 两种HTTP请求方法:GET和POST的区别
之前在一些开发者平台使用网页调用API时,一再提到两种请求方法GET和POST,所以就去了解了下.那么这又不得不提到HTTP了! 一.什么是 HTTP? 超文本传输协议(HTTP)的设计目的是保证客户 ...
- Matlib’s lsqnonlin 和 scipy.optimize’s least_square
matlib's lsqnonlin 和 scipy.optimize's least_square 问题 有三个点 $A,B,C$ , 经过一个线性变换 $T$ , 变为了 $A',B',C'$ 三 ...
- resourcequota分析(一)-evaluator-v1.5.2
什么是evaluator 大家都知道,Kubernetes中使用resourcequota对配额进行管理.配额的管理涉及两个步骤:1.计算请求所需要的资源:2.比较并更新配额.所以解读resource ...
- C++扬帆远航——12(抓小偷)
/* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:zhaoxiaotou.cpp * 作者:常轩 * 微信公众号: ...
- 达拉草201771010105《面向对象程序设计(java)》第十一周学习总结
达拉草201771010105<面向对象程序设计(java)>第十一周学习总结 实验十一 集合 实验时间 2018-11-8 第一部分:理论知识 1.集合(Collection或称为容 ...
- Windows 10 右键 在此处打开 CMD
1. 打开注册表 # 1. 使用快捷键打开 “运行” # win + r # 2. 在 “运行” 中输入 # regedit # 3. 回车 2. 创建与设置 OpenCMDHere # 1. 切换到 ...