前提:

按照 https://www.cnblogs.com/luffystory/p/12081074.html 配置好Redis Cluster in Ubuntu

按照如下结构搭建项目结构:

POM

<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.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.study</groupId>
<artifactId>SpringBootTest_RedisCluster</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootTest-2</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-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</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-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }
package com.app;

import java.io.Serializable;

public class Book implements Serializable {

    private String name;
private String author; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
} public String toString(){
return String.format("Book: { name: %s, author: %s }", name,author);
} }
package com.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class BookController { @Autowired
RedisTemplate<String, Book> redisTemplate; @Autowired
StringRedisTemplate stringRedisTemplate; @GetMapping("/test1")
public void test() {
ValueOperations<String,Book> ops = redisTemplate.opsForValue();
Book book = new Book();
book.setName("水浒传");
book.setAuthor("施耐庵");
ops.set("b1", book);
System.out.println(ops.get("b1")); ValueOperations<String,String> ops2 = stringRedisTemplate.opsForValue();
ops2.set("Hello", "World");
System.out.println(ops2.get("Hello"));
}
}
package com.app;

import java.util.ArrayList;
import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration
@ConfigurationProperties("spring.redis.cluster")
public class RedisConfig { List<Integer> ports;
String host;
JedisPoolConfig poolConfig; @Bean
RedisClusterConfiguration redisClusterConfiguarion() {
RedisClusterConfiguration configuration = new RedisClusterConfiguration();
List<RedisNode> nodes = new ArrayList<>();
for(Integer port : ports) {
nodes.add(new RedisNode(host,port));
}
configuration.setClusterNodes(nodes);
return configuration;
} @Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory(redisClusterConfiguarion(), poolConfig);
return factory;
} @Bean
RedisTemplate redisTempalte() {
RedisTemplate redisTempalte = new RedisTemplate();
redisTempalte.setConnectionFactory(jedisConnectionFactory());
redisTempalte.setKeySerializer(new StringRedisSerializer());
redisTempalte.setValueSerializer(new JdkSerializationRedisSerializer()); return redisTempalte;
} @Bean
StringRedisTemplate stringRedisTemplate() {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(jedisConnectionFactory());
stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
stringRedisTemplate.setValueSerializer(new StringRedisSerializer()); return stringRedisTemplate;
} public List<Integer> getPorts() {
return ports;
} public void setPorts(List<Integer> ports) {
this.ports = ports;
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public JedisPoolConfig getPoolConfig() {
return poolConfig;
} public void setPoolConfig(JedisPoolConfig poolConfig) {
this.poolConfig = poolConfig;
} }

application.yml

server:
port: 8091
spring:
redis:
cluster:
ports:
- 8001
- 8002
- 8003
- 8004
- 8005
- 8006
- 8007
- 8008
host: 192.168.157.131 #此处所有的node都在同一台机器上,所以用了一个 host
poolConfig:
max-total: 8
max-idle: 8
max-wait-millis: -1
min-idle: 0

启动SpringBoot Application ,并在浏览器中输入: http://localhost:8091/test1

连接任意一个Redis 实例,验证结果:

Redis Cluster with SpringBoot的更多相关文章

  1. Redis Cluster Cache with SpringBoot

    前提: 根据  https://www.cnblogs.com/luffystory/p/12081074.html 创建好Redis集群 <project xmlns="http:/ ...

  2. Redis Cluster的搭建与部署,实现redis的分布式方案

    前言 上篇Redis Sentinel安装与部署,实现redis的高可用实现了redis的高可用,针对的主要是master宕机的情况,我们发现所有节点的数据都是一样的,那么一旦数据量过大,redi也会 ...

  3. Redis Cluster(集群)的搭建

    一.Redis的下载.安装.启动(单实例) 我们统一将Redis安装在/opt目录下,执行命令如下: $ cd /opt $ wget http://download.redis.io/release ...

  4. Redis Cluster 集群搭建与扩容、缩容

    说明:仍然是伪集群,所有的Redis节点,都在一个服务器上,采用不同配置文件,不同端口的形式实现 前提:已经安装好了Redis,本文的redis的版本是redis-6.2.3 Redis的下载.安装参 ...

  5. window下使用Redis Cluster部署Redis集群

    日常的项目很多时候都需要用到缓存.redis算是一个比较好的选择.一般情况下做一个主从就可以满足一些比较小的项目需要.在一些并发量比较大的项目可能就需要用到集群了,redis在Windows下做集群可 ...

  6. Redis Cluster 分区实现原理

    Redis Cluster本身提供了自动将数据分散到Redis Cluster不同节点的能力,分区实现的关键点问题包括:如何将数据自动地打散到不同的节点,使得不同节点的存储数据相对均匀:如何保证客户端 ...

  7. Redis Cluster

    使用 Redis Cluster Redis 3.0 在2015年出了Stable版本,3.0版本相对于2.8版本带来的主要新特性包括: 实现了Redis Cluster,从而做到了对集群的支持: 引 ...

  8. Redis Cluster 介绍与使用

    Redis Cluster 功能特性 Redis 集群是分布式的redis 实现,具有以下特性: 1. 高可用性与可线性扩张到1000个节点 2. 数据自动路由到多个节点 3. 节点间数据共享 4. ...

  9. Redis Cluster原理

    Redis Cluster 是Redis的集群实现,内置数据自动分片机制,集群内部将所有的key映射到16384个Slot中,集群中的每个Redis Instance负责其中的一部分的Slot的读写. ...

随机推荐

  1. P3586 [POI2015]LOG

    传送门 对于询问,首先如果正数数量不到 $c$ 个显然无解 然后如果大于等于 $s$ 的数大于等于 $c$ 个,那么显然有解 否则,考虑贪心地取数,首先初始大于等于 $s$ 的哪些数我们每次取都可以取 ...

  2. oracle 重置密码(一般电网,国企需要的二级等保)

    oracle 10g,11g 可以查找以下,12c没有测试 一.查看profile配置并记录;二.在重置密码之前修改profile(PASSWORD_REUSE_MAX.PASSWORD_REUSE_ ...

  3. 客户端相关知识学习(二)之h5与原生app交互的原理

    前言 现在移动端 web 应用,很多时候都需要与原生 app 进行交互.沟通(运行在 webview中),比如微信的 jssdk,通过 window.wx 对象调用一些原生 app 的功能.所以,这次 ...

  4. O001、写在最前面

    参考https://www.cnblogs.com/CloudMan6/p/5224114.html   <每天5分钟玩转 OpenStack>       1.系统讲解 OpenStac ...

  5. 解决postgresql数据库localhost可以连接,ip连接不了的问题

    解决:windows环境下,postgresql数据库,localhost可以连接,ip地址连接不了. 解决办法: 1.打开postgresql安装目录下的配置文件 pg_hba.conf       ...

  6. Clang调试deadcode思路

    首先描述下我的环境:Ubuntu16.04 llvm4.0 clang4.0全部使用源码安装方式 Clang的根目录,位于llvm-src下边的tools目录下. 因为需要找到真正的开关,下边我描述下 ...

  7. less 经典范例 bootstrap 的 less 版本 常用 less 代码

    1. bootstrap 的 less 版本 2.less 文件分布 /*! * Bootstrap v3.3.7 (http://getbootstrap.com) * Copyright 2011 ...

  8. MySQL面试题(二)

    ● 请你说一说mysql的四种隔离状态 参考回答: Mysql主要包含四种隔离状态: 事务隔离级别 脏读 不可重复读 幻读 读未提交(read-uncommitted) 是 是 是 不可重复读(rea ...

  9. java8学习之比较器深入

    继续接着上次[http://www.cnblogs.com/webor2006/p/8331498.html]的比较器进行探究,上次排序采用是的利用Collections.sort()进行的,下面采用 ...

  10. 页面内置函数${fn:}

    引入头文件<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions " %&g ...