1. 程式人生 > 其它 >springboot連線redis cluster(帶密碼)

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>