spring-data-redis jedis

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
<type>jar</type>
</dependency>

redis standalone config

  • jedis- redis_standalone
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    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.RedisStandaloneConfiguration;
    import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;

    import lombok.Getter;
    import lombok.Setter;
    import lombok.extern.slf4j.Slf4j;
    import redis.clients.jedis.JedisPoolConfig;

    @Getter
    @Setter
    @Slf4j
    @Configuration
    @ConfigurationProperties(prefix = "summer.redis")
    public class RedisConfig {

    private String seedAddress;
    private String password;

    JedisClientConfiguration clientConfiguration() {
    JedisPoolConfig poolConfig = getJedisPoolConfig();
    JedisClientConfiguration.JedisClientConfigurationBuilder builder = JedisClientConfiguration.builder();
    return builder
    .usePooling()
    .poolConfig(poolConfig)
    .build();
    }

    public JedisPoolConfig getJedisPoolConfig() {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(100);
    poolConfig.setMaxIdle(100);
    poolConfig.setMinIdle(10);
    poolConfig.setTestOnBorrow(false);
    poolConfig.setTestOnReturn(false);
    poolConfig.setTestOnCreate(true);
    poolConfig.setBlockWhenExhausted(true);
    poolConfig.setMaxWaitMillis(1000);
    return poolConfig;
    }

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {

    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setHostName("wsl");
    redisStandaloneConfiguration.setPort(6379);
    redisStandaloneConfiguration.setPassword("summer123!@#");

    JedisClientConfiguration clientConfiguration = clientConfiguration();
    JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration, clientConfiguration);
    factory.afterPropertiesSet();
    return factory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory());
    return template;
    }
    }

    jedis redis-cluster config

  • code
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    package com.example.summermetrics.config;

    import java.util.Arrays;
    import java.util.List;
    import java.util.Objects;
    import java.util.Optional;
    import java.util.stream.Collectors;

    import org.apache.commons.collections4.CollectionUtils;
    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.JedisClientConfiguration;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;

    import lombok.Getter;
    import lombok.Setter;
    import lombok.extern.slf4j.Slf4j;
    import redis.clients.jedis.JedisPoolConfig;

    @Getter
    @Setter
    @Slf4j
    @Configuration
    public class RedisConfig {

    public JedisClientConfiguration clientConfiguration() {
    JedisPoolConfig poolConfig = getJedisPoolConfig();
    JedisClientConfiguration.JedisClientConfigurationBuilder builder = JedisClientConfiguration.builder();
    return builder
    .usePooling()
    .poolConfig(poolConfig)
    .build();
    }

    public JedisPoolConfig getJedisPoolConfig() {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(100);
    poolConfig.setMaxIdle(100);
    poolConfig.setMinIdle(10);
    poolConfig.setTestOnBorrow(false);
    poolConfig.setTestOnReturn(false);
    poolConfig.setTestOnCreate(true);
    poolConfig.setBlockWhenExhausted(true);
    poolConfig.setMaxWaitMillis(1000);
    return poolConfig;
    }

    public static final String SEED_ADDRESS = "192.168.50.28:6379,192.168.50.28:6381,192.168.50.28:6383";

    public List<RedisNode> getRedisNodes(String seedAddress) {
    List<RedisNode> redisNodes = Arrays.stream(seedAddress.split(","))
    .map(s -> {
    String[] seed = Optional.ofNullable(s).orElse("").split(":");
    if (seed.length < 2) {
    log.error("[RedisConnectionFactory] Add Cluster Seed. seed: {}", s);
    return null;
    }
    String host = seed[0];
    int port = Integer.parseInt(seed[1]);
    log.info("[RedisConnectionFactory] Add Cluster Seed. host: {}, port: {}", host, port);
    return RedisNode.newRedisNode().listeningAt(host, port).build();
    }).filter(Objects::nonNull).collect(Collectors.toList());

    if (CollectionUtils.isEmpty(redisNodes)) {
    throw new RuntimeException("[RedisConnectionFactory] Add Cluster Seed failed.");
    }
    return redisNodes;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {

    RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
    getRedisNodes(SEED_ADDRESS).forEach(redisClusterConfiguration::addClusterNode);
    // redisClusterConfiguration.setPassword("");

    JedisClientConfiguration clientConfiguration = clientConfiguration();
    JedisConnectionFactory factory = new JedisConnectionFactory(redisClusterConfiguration, clientConfiguration);
    factory.afterPropertiesSet();
    return factory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory());
    return template;
    }
    }

    test

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54

    import java.util.UUID;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseStatus;
    import org.springframework.web.bind.annotation.RestController;

    import com.example.summermetrics.domain.SummerMetricsHeader;
    import com.example.summermetrics.domain.redis.Student;
    import com.example.summermetrics.repository.StudentRepository;

    import io.swagger.v3.oas.annotations.Operation;
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;

    @RestController
    @RequestMapping(value = "/summer")
    @RequiredArgsConstructor
    @Slf4j
    public class RedisController {

    @Autowired
    private
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    StudentRepository studentRepository;

    @Operation
    @GetMapping("/redis/cluster")
    @ResponseStatus(HttpStatus.OK)
    public void redisTest(SummerMetricsHeader summerMetricsHeader) {
    String id = UUID.randomUUID().toString();
    Student student = new Student(id, summerMetricsHeader.getMemberNo(), Student.Gender.MALE, 1);
    studentRepository.save(student);
    Student retrievedStudent = studentRepository.findById(id).get();
    log.info(student.toString());

    retrievedStudent.setName("Richard Watson");
    studentRepository.save(student);
    log.info(student.toString());
    //添加key
    stringRedisTemplate.opsForValue().set("user", "张三");
    //获取key
    log.info("从redis中获取key=user的值为:{}", stringRedisTemplate.opsForValue().get("user"));
    ((JedisConnectionFactory)stringRedisTemplate.getConnectionFactory()).destroy();

    }

    }

参考文章

评论