springboot连接redis集群环境

目录

 

 

一、 maven配置

二、yml文件配置

三 、 Config代码

四、测试用例


 

一、 maven配置



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.3
         
    
    com.forecharts
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        11
        7.5.0
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.5.0
        
        
            org.elasticsearch.client
            elasticsearch-rest-client
            7.5.0
        
        
            org.elasticsearch
            elasticsearch
            7.5.0
        


        
            org.springframework.boot
            spring-boot-devtools
            true
        
        
            com.alibaba
            fastjson
            1.2.62
        
        
            org.apache.commons
            commons-lang3
            3.9
        
        
            com.github.pagehelper
            pagehelper
            4.1.6
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.jetbrains
            annotations
            RELEASE
            compile
        
        
            org.projectlombok
            lombok
        
        
            redis.clients
            jedis
        
        
            junit
            junit
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


二、yml文件配置

spring:
    redis:
        cluster:
            node:192.168.109.134:6379,192.168.109.134:6380,192.168.109.134:6381
        password:
        database: 0
        pool:
            max-active: 20   # 最大连接数 20个,如果是负数则没有限制
            max-wait: -1   # 最大阻塞时间 如果是 负数则没有要求
            max-Idle: 20  #最大空闲连接  idle 表示空闲或者虚度的意思
            min-Idle: 0  #最小的空闲连接  
        timeout: 6000 #连接node节点超时时间

三 、 Config代码

package com.example.demo.com.exmaple.Config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

@Configuration
public class RedisClusterConfig {

    @Value("${spring.redis.cluster.nodes}")
    private String clusternodes;

    @Value("${spring.redis.database}")
    private int database;

    @Value("${spring.redis.pool.max-Active}")
    private int maxActive;

    @Value("${spring.redis.pool.max-idle}")
    private int maxidle;

    @Value("${spring.redis.pool.min-idle}")
    private int minidle;

    @Value("${spring.redis.pool.max-wait}")
    private long maxwait;

    @Value("${spring.redis.timeout}")
    private int timeout;

    @Bean
    public JedisCluster getjedisCluster()
    {
        return new JedisCluster(getHostport(),timeout,getjedispoolConfig());

    }

    private Set getHostport()
    {
        String []nodes=clusternodes.split(",");

        Set  hostAndPorts=new HashSet<>();
        for(String node:nodes)
        {
            String[] hp=node.split(":");
            hostAndPorts.add(new HostAndPort(hp[0],Integer.parseInt(hp[1])));
        }
        return hostAndPorts;
    }

    private JedisPoolConfig  getjedispoolConfig()
    {
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxidle);
        jedisPoolConfig.setMinIdle(minidle);
        jedisPoolConfig.setMaxWaitMillis(maxwait);
        jedisPoolConfig.setMaxTotal(maxActive);
        return jedisPoolConfig;


    }


}

四、测试用例

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

@RestController
public class RedisJedistest {

    @Autowired
    private JedisCluster jedisCluster;
    @GetMapping("/test")
    public void test()
    {
        jedisCluster.set("name1","tom1");
        String username=jedisCluster.get("name1");
        System.out.println("username=="+username);
    }

}

 

 

测试包:

 

 

你可能感兴趣的:(redis)