webflux lettuce redis

1
2
3
4
5
6
7
8
9
 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
1
2
# Connect redis cluster nodes with – C parameter: redis cli – C – H 172.17.0.1 – P 6391
redis-cli –c -h 172.17.0.1 –p 6391

code

1
2
3
4
summer:
redis:
password:
seed-address: 192.168.50.28:6379,192.168.50.28:6380,192.168.50.28:6381
  • RedisConfig
    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
    @ConfigurationProperties(prefix = "summer.redis")
    public class RedisConfig {

    private String seedAddress;
    private String password;

    private LettuceClientConfiguration getLettuceClientConfiguration() {
    ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
    .dynamicRefreshSources(true)
    .enablePeriodicRefresh(Duration.ofSeconds(30))
    .enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.UNKNOWN_NODE)
    .adaptiveRefreshTriggersTimeout(Duration.ofSeconds(25))
    .build();

    return LettucePoolingClientConfiguration.builder()
    .clientOptions(ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build())
    .build();
    }

    private RedisClusterConfiguration getRedisClusterConfiguration(String seedAddress) {
    RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration();
    getRedisNodes(seedAddress).forEach(clusterConfig::addClusterNode);
    clusterConfig.setPassword(RedisPassword.of(password));
    return clusterConfig;
    }

    private List<RedisNode> getRedisNodes(String seedAddress) {
    return Arrays.stream(seedAddress.split(","))
    .map(s -> {
    String[] seed = s.split(":");
    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();
    }).collect(Collectors.toList());
    }

    @Bean
    public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
    return new LettuceConnectionFactory(getRedisClusterConfiguration(seedAddress), getLettuceClientConfiguration());
    }

    @Bean
    ReactiveRedisTemplate<String, String> stringReactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
    return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, RedisSerializationContext.string());
    }
    }
  • 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
    @RestController
    @Tag(name = "Redis APIs", description = "Redis APIs ")
    public class StrignRedisController {

    @Autowired
    @Qualifier("stringReactiveRedisTemplate")
    private ReactiveRedisTemplate<String, String> stringRedisTemplate;

    @Operation(description = "Get data from redis", parameters = {
    @Parameter(name = "key", in = ParameterIn.QUERY, required = true, description = "redis key")
    })
    @GetMapping("redis")
    public Mono<String> getRedisData(final @RequestParam String key) {

    return stringRedisTemplate.opsForValue().get(key);
    }

    @PostMapping("redis")
    @Operation(description = "save data to redis", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody())
    public Mono<Boolean> postRedisData(@Valid @RequestBody PostRedisRequest redisRequest) {
    final Mono<Boolean> set = stringRedisTemplate.opsForValue().set(redisRequest.getKey(), redisRequest.getValue());
    return set;
    }

    }

demo

参考文章

评论