Spring Boot + Webflux + Redis

本文介绍如何在SpringBoot项目中结合Webflux和Redis进行异步非阻塞操作,包括添加依赖、配置Redis、定义实体类及编写Controller的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<scope>runtime</scope>
</dependency>
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<optional>true</optional>
</dependency>

配置Redis

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=5000

添加实体

package org.luncert.webfluxtest.model;

import org.springframework.data.annotation.Id;

import lombok.Data;

import java.io.Serializable;

/**
 * 城市实体类
 *
 */
@Data
public class City implements Serializable { // 必须实现Serializable,redis才能序列化该实体

    private static final long serialVersionUID = -2081742442561524068L;

    @Id
    private Long id;

    private Long provinceId;

    private String cityName;

    private String description;

}

编写controller

package org.luncert.webfluxtest.contorller;

import org.luncert.webfluxtest.model.City;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/city")
public class CityController {

    private RedisTemplate<String, City> redisTemplate; // 不能直接使用@Autowired,类型参数(泛型)会使spring找不到合适的bean

    @SuppressWarnings("unchecked")
    public CityController(RedisTemplate<?, ?> redisTemplate) {
        this.redisTemplate = (RedisTemplate<String, City>) redisTemplate;
    }

    @GetMapping(value = "/{id}")
    public Mono<City> getById(@PathVariable("id") Long id) {
        String key = "city_" + id;
        ValueOperations<String, City> operations = redisTemplate.opsForValue();
        boolean hasKey = redisTemplate.hasKey(key);
        City city = operations.get(key);

        if (!hasKey) {
            return Mono.create(monoSink -> monoSink.success(null));
        }
        return Mono.create(monoSink -> monoSink.success(city));
    }

    @PostMapping()
    public Mono<City> save(@RequestBody City city) {
        String key = "city_" + city.getId();
        ValueOperations<String, City> operations = redisTemplate.opsForValue();
        operations.set(key, city, 60, TimeUnit.SECONDS);

        return Mono.create(monoSink -> monoSink.success(city));
    }

    @DeleteMapping(value = "/{id}")
    public Mono<Long> delete(@PathVariable("id") Long id) {
        String key = "city_" + id;
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            redisTemplate.delete(key);
        }
        return Mono.create(monoSink -> monoSink.success(id));
    }

}

运行…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值