SpringBoot集成Redis集群

@SpringBoot集成Redis集群

SpringBoot集成Redis集群

工程结构图

在这里插入图片描述

一、新建一个 SpringBoot 工程

二、引入 Redis 相关依赖

1、pom.xml 中添加以下内容
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.9.0</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、完整的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.zx</groupId>
	<artifactId>testRedis</artifactId>
	<version>1.0</version>aa
	<packaging>jar</packaging>

	<name>testRedis</name>
	<description>testRedis project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
		<relativePath/>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<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-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- redis start -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<!-- redis end -->

		<dependency>
			<groupId>com.zx</groupId>
			<artifactId>testRedis</artifactId>
			<version>1.0</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

三、配置 Redis 参数

application.properties 中添加以下内容
## ################ windows ##############################
## Redis服务器地址
##spring.redis.host=10.100.50.23
## Redis服务器连接端口
##spring.redis.port=6379
## Redis服务器连接密码(默认为空)
#spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
#spring.redis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
#spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
#spring.redis.pool.max-idle=8
## 连接池中的最小空闲连接
#spring.redis.pool.min-idle=0
## 连接超时时间(毫秒)
#spring.redis.timeout=0
#spring.redis.commandTimeout=5000
## 读取数据超时时间
#spring.redis.soTimeout=5000
##超时重连最大次数
#spring.redis.maxAttempts=3
## redis.cluster
#spring.redis.cluster.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

# ################ linux ##############################
# Redis服务器地址
#spring.redis.host=10.100.50.23
# Redis服务器连接端口
#spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=zx123456
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=50
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=3
# 连接超时时间(毫秒)
spring.redis.timeout=0
spring.redis.commandTimeout=5000
# 读取数据超时时间
spring.redis.soTimeout=5000
#超时重连最大次数
spring.redis.maxAttempts=3
# redis.cluster
spring.redis.cluster.nodes=172.16.1.12:7000,172.16.1.12:7001,172.16.1.12:7002,172.16.1.12:7003,172.16.1.12:7004,172.16.1.12:7005

四、新建 Redis 配置解析类

新建类 RedisConfig.java
package com.zx.utils;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Set;

/**
 * @Description Redis集群配置
 * @Author zhengxiao
 * @Date 2018/12/4
 */
@Configuration
@ConditionalOnClass({JedisCluster.class})
public class RedisConfig {
    /**
     * redis.cluster
     */
    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes;

    /**
     * 连接池中的最大空闲连接
     */
    @Value("${spring.redis.pool.max-idle}")
    private int maxIdle;

    /**
     * 连接池最大阻塞等待时间(使用负值表示没有限制)
     */
    @Value("${spring.redis.pool.max-wait}")
    private long maxWaitMillis;

    /**
     * 连接超时时间
     */
    @Value("${spring.redis.timeout}")
    private int timeout;

    /**
     * 命令超时时间
     */
    @Value("${spring.redis.commandTimeout}")
    private int commandTimeout;

    /**
     * 集群密码
     */
    @Value("${spring.redis.password}")
    private String password;

    /**
     * 读取数据超时时间
     */
    @Value("${spring.redis.soTimeout}")
    private int soTimeout;

    /**
     * 超时重连最大次数
     */
    @Value("${spring.redis.maxAttempts}")
    private int maxAttempts;

    /**
     * 最大连接数
     */
    @Value("${spring.redis.pool.max-active}")
    private int maxActive;

    @Bean
    public JedisCluster getJedisCluster() {
        String[] cNodes = clusterNodes.split(",");
        Set<HostAndPort> nodes = new HashSet<>();
        //分割出集群节点
        for(String node : cNodes) {
            String[] hp = node.split(":");
            nodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
        }
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setMaxTotal(maxActive);
        //创建集群对象
//        return new JedisCluster(nodes, commandTimeout, soTimeout, maxAttempts, password, jedisPoolConfig);
        // windows
//        JedisCluster jedisCluster = new JedisCluster(nodes, commandTimeout, soTimeout, maxAttempts, null, jedisPoolConfig);

        // linux
        JedisCluster jedisCluster = new JedisCluster(nodes, commandTimeout, soTimeout, maxAttempts, password, jedisPoolConfig);
        System.out.println(jedisCluster.get("a"));

        return jedisCluster;
    }

    /**
     * 设置数据存入redis 的序列化方式
     * redisTemplate序列化默认使用的jdkSerializeable,存储二进制字节码,导致key会出现乱码,所以自定义
     * 序列化类
     *
     * @paramredisConnectionFactory
     */
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }
}

五、启动类添加注解@EnableCaching,加入缓存

package com.zx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

/**
 * @Description 启动
 * @Author zhengxiao
 * @Date 2018/12/4
 */
@SpringBootApplication
@EnableCaching
public class RedisApplication {

	public static void main(String[] args) {
		SpringApplication.run(RedisApplication.class, args);
	}
}

六、新建测试服务类

package com.zx.service;/**
 * Created by 1 on 2018/12/4.
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;

/**
 * @Description
 * @Author zhengxiao
 * @Date 2018/12/4
 */
@Service
public class RedisService {
    @Autowired
    private JedisCluster jedisCluster;

    /**
     * 根据 key 查找
     * @param key
     * @return
     */
    public String findByKey(String key) {
        return jedisCluster.get(key);
    }

    /**
     * 保存
     * @param key
     * @param value
     * @return
     */
    public String saveRedis(String key, String value) {
        return jedisCluster.set(key, value);
    }

}

七、新建控制层接口

package com.zx.controller;

import com.zx.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

/**
 * @Description 接口
 * @Author zhengxiao
 * @Date 2018/12/4
 */
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    RedisService redisService;

    /**
     * http://localhost:8080/redis/find?key=a
     * 查询redis集群
     *
     * @param key 键
     * @return 值
     */
    @GetMapping("/find")
    public String findRedis(@RequestParam String key) {
        return redisService.findByKey(key);
    }

    /**
     * http://localhost:8080/redis/save?key=a&value=1
     * @param key
     * @param value
     * @return
     */
    @GetMapping("/save")
    public String saveRedis(@RequestParam String key, @RequestParam String value){
        return redisService.saveRedis(key, value);
    }

}

八、测试

1、启动 Redis 集群
2、启动 SpringBoot
3、浏览器访问

http://localhost:8080/redis/save?key=a&value=1

浏览器显示:ok

http://localhost:8080/redis/find?key=a

浏览器显示:1

九、问题

1、通过jedis连接虚拟机redis单机成功,使用redis客户端可以连接虚拟机redis集群,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool

原因:错误的启动集群命令

/home/redis/redis-cluster/7000/bin/redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 -a zx123456 --cluster-replicas 1

参考文章

https://www.cnblogs.com/mengjinluohua/p/6193962.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值