• springboot连接redis cluster(带密码)


    RedisConfig配置内容如下:

    package com.example.demo5.config;
    import org.springframework.beans.factory.annotation.Value;
    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.serializer.JdkSerializationRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import org.springframework.stereotype.Component;
    import redis.clients.jedis.JedisPoolConfig;
    import java.util.HashSet;
    import java.util.Set;
    
    @Configuration
    @Component
    public class RedisConfig {
        @Value("${spring.redis.cluster.nodes}")
        private String nodes;
    
        @Value("${spring.redis.password}")
        private String password;
    
        @Value("${spring.redis.jedis.pool.min-idle}")
        private int minIdle;
    
        @Value("${spring.redis.jedis.pool.max-active}")
        private int maxActive;
    
        @Value("${spring.redis.jedis.pool.max-wait}")
        private long maxWait;
    
        @Value("${spring.redis.jedis.pool.max-idle}")
        private int maxIdle;
    
    
        //redis配置
        public JedisPoolConfig poolConfig(){
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMinIdle(minIdle);
            jedisPoolConfig.setMaxWaitMillis(maxWait);
            jedisPoolConfig.setMaxTotal(maxActive);
            jedisPoolConfig.setMaxIdle(maxIdle);
            return jedisPoolConfig;
        }
    
    
        //redis集群配置,6个ip以,分割,然后再以:分割
        public RedisClusterConfiguration redisClusterConfiguration(){
            RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
            String[] cNodes = nodes.split(",");
            Set<RedisNode> hp = new HashSet<>();
            for (String node : cNodes) {
                String[] split = node.split(":");
                hp.add(new RedisNode(split[0].trim(),Integer.valueOf(split[1])));
            }
            redisClusterConfiguration.setPassword(password);
            redisClusterConfiguration.setClusterNodes(hp);
            return redisClusterConfiguration;
        }
    
        //创建redis连接工厂
        public JedisConnectionFactory jedisConnectionFactory() {
            //集群模式
            JedisConnectionFactory  factory = new JedisConnectionFactory(redisClusterConfiguration(),poolConfig());
            return factory;
        }
    
    
        public RedisTemplate<String, Object> redisTemplate() {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            initDomainRedisTemplate(redisTemplate);
            return redisTemplate;
        }
    
        //初始化redis序列化
        private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
            //如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            //这个地方有一个问题,这种序列化器会将value序列化成对象存储进redis中,如果
            //你想取出value,然后进行自增的话,这种序列化器是不可以的,因为对象不能自增;
            //需要改成StringRedisSerializer序列化器。
            redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
            redisTemplate.setEnableTransactionSupport(true);
            redisTemplate.setConnectionFactory(jedisConnectionFactory());
        }
    
    }

    application.yaml文件内容为:

    spring:
      redis:
        cluster:
          nodes: 192.168.81.130:7001,192.168.81.130:7002,192.168.81.130:7003,192.168.81.130:7004,192.168.81.130:7005,192.168.81.130:7006
        password: 123
        jedis:
          pool:
            min-idle: 0
            max-active: 8
            max-wait: -1
            max-idle: 8

    controller内容为:

    package com.example.demo5.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    public class RedisController {
        @Autowired
        private RedisTemplate redisTemplate;
    
        @GetMapping("/test")
        public String test(){
            redisTemplate.opsForValue().set("name", "lizhao");
            String name = (String) redisTemplate.opsForValue().get("name");
            System.out.println(name);
            return name;
    
        }
    }

    pom文件为:

    <?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.5.5</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo5</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo5</name>
        <description>demo5</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-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>RELEASE</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
  • 相关阅读:
    DNS-PreFetch 技术及其他前端优化加载技术
    Windows10 睡死
    Dns Over Https(DoH) --- Windows10 预览版 19645
    原生JavaScript(JS)修改添加CSS样式表 (更好的性能)
    技术的本质
    工程设计
    文件分配表 FAT -WIKI
    博客皮肤修复: 行号和搜索引擎快照(基于 Cnblogs-Theme-SimpleMemory v1.3.3)
    java-知识点学习和补充
    红黑树01--[红黑树简介&添加修复]
  • 原文地址:https://www.cnblogs.com/fengzi7314/p/15427669.html
Copyright © 2020-2023  润新知