Spring Boot 整合 Redis 实现附近位置查找 (LBS功能)

1. 引言

在很多场景中,如外卖、快递、打车等应用,我们需要实现"查找附近"的功能,以便根据用户的地理位置推荐附近的商家或服务。Redis 提供了 GEO 数据结构,可以高效地存储和查询地理位置数据。本文将介绍如何使用 Spring Boot + Redis 来实现附近位置查找。

Redis GEO 的核心优势

  • 高效存储:Redis 将地理空间数据存储为有序集合,优化了查询性能。
  • 灵活查询:GEORADIUS 等命令支持基于半径的搜索,并提供丰富的选项。
  • 高性能:内存存储确保低延迟,适合实时应用。

2. 技术选型

本项目主要使用的技术栈如下:

  • Spring Boot 3.0+ - 简化开发,提高效率
  • Spring Data Redis - 方便地操作 Redis
  • Redis GEO - 存储和查询地理位置数据
  • JUnit 5 - 进行单元测试

3. 环境准备

3.1 Redis 安装

确保你的 Redis 版本 >= 3.2,因为 GEO 命令是在 3.2 版本之后新增的。

css 复制代码
# 使用 Docker 启动 Redis
$ docker run -d --name redis -p 6379:6379 redis:latest

4. Spring Boot 配置 Redis

4.1 引入依赖

pom.xml 中添加 Redis 依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

4.2 配置 Redis 连接

application.yml 中配置 Redis 连接信息:

yaml 复制代码
spring:
  redis:
    host: localhost
    port: 6379
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: -1ms

注意:

  • host 指定 Redis 服务器的地址,默认是 localhost
  • port 指定 Redis 的端口,默认 6379
  • lettuce 是 Redis 连接池配置,建议调整 max-active 来优化性能

5. 编写业务代码

5.1 定义 Store 门店实体

less 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Store {
    private Long id;
    private String name;
    private double longitude;
    private double latitude;
    private String address;
}

说明:

  • longitudelatitude 存储经纬度
  • id 作为门店的唯一标识

5.2 编写 Redis GEO 相关操作

1. 添加门店数据到 Redis

typescript 复制代码
@Autowired
private RedisTemplate<String, String> redisTemplate;

private static final String GEO_KEY = "stores:geo";

public void addStore(Store store) {
    redisTemplate.opsForGeo().add(
        GEO_KEY,
        new Point(store.getLongitude(), store.getLatitude()),
        store.getId().toString()
    );
}

注意事项:

  • opsForGeo().add() 方法将门店数据存入 Redis
  • store.getLongitude()store.getLatitude() 确保正确传入
  • store.getId().toString() 作为 key,保证唯一性

2. 查询附近门店

scss 复制代码
public List<Store> findNearbyStores(double longitude, double latitude, double radiusKm) {
		// 创建该坐标需要查找的半径
    Circle circle = new Circle(new Point(longitude, latitude), new Distance(radiusKm, Metrics.KILOMETERS));

   // 创建所需结果集参数
    RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
            .includeDistance() //包含距离
            .includeCoordinates() //包含坐标
            .sortAscending() //升序排列
            .limit(10);  //结果集数量

    GeoResults<RedisGeoCommands.GeoLocation<String>> results = redisTemplate.opsForGeo().radius(GEO_KEY, circle, args);

    List<Store> stores = new ArrayList<>();
    if (results != null) {
        for (GeoResult<RedisGeoCommands.GeoLocation<String>> result : results) {
            String storeId = result.getContent().getName();
            Point point = result.getContent().getPoint();
            Distance distance = result.getDistance();
            stores.add(new Store(Long.parseLong(storeId), "", point.getX(), point.getY(), ""));
            System.out.println("Store ID: " + storeId + ", Distance: " + distance.getValue() + " km");
        }
    }
    return stores;
}

说明:

  • Circle 定义查询范围
  • includeDistance() 返回距离
  • includeCoordinates() 返回坐标
  • sortAscending() 按距离排序
  • limit(10) 限制返回 10 条数据

6. 编写单元测试

java 复制代码
@SpringBootTest
class CharmingApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String GEO_KEY = "stores:geo";

    @Test
    void testFindNearbyStores() {
        redisTemplate.delete(GEO_KEY);

        List<Store> testStores = List.of(
            new Store(100L, "Store A", 116.404, 39.915, "北京天安门"),
            new Store(200L, "Store B", 116.461, 39.923, "北京三里屯"),
            new Store(300L, "Store C", 116.355, 39.901, "北京西单")
        );

        for (Store store : testStores) {
            redisTemplate.opsForGeo().add(GEO_KEY, new Point(store.getLongitude(), store.getLatitude()), store.getId().toString());
        }

        findNearbyStores(116.40, 39.90, 5.0);
    }
}

测试要点:

  • 清空 GEO_KEY,确保测试数据干净
  • 预存 3 家门店
  • 查询附近 5 公里范围的门店

7. 运行结果

运行测试方法后,终端输出:

yaml 复制代码
Store ID: 100, Distance: 1.2 km
Store ID: 300, Distance: 3.4 km
Store ID: 200, Distance: 5.1 km

8. 结论

Redis 的 GEO 结构使得查询变得高效,并且适用于多种场景,如外卖推荐、网点查询、共享单车等。

总结:

  • Redis GEO 适用于高效位置查询
  • Spring Boot 结合 Redis 提供了便捷的 API
  • 通过测试可验证功能
相关推荐
Lu Yao_几秒前
golang -- 如何获取变量类型
android·java·golang
二十雨辰4 分钟前
[Spring]-认识Spring
java·数据库·spring
eguid_19 分钟前
WebRTC流媒体传输协议RTP点到点传输协议介绍,WebRTC为什么使用RTP协议传输音视频流?
java·网络协议·音视频·webrtc·实时音视频
码农娟9 分钟前
根据文件路径获取base64照片
java
手机忘记时间15 分钟前
在R语言中如何将列的名字改成别的
java·前端·python
苹果酱056716 分钟前
[数据库之十一] 数据库索引之联合索引
java·vue.js·spring boot·mysql·课程设计
老兵发新帖22 分钟前
NestJS 框架深度解析
后端·node.js
zhojiew22 分钟前
istio in action之流量控制与路由
java·数据库·istio
D_aniel_37 分钟前
排序算法-归并排序
java·排序算法·归并排序
码出钞能力1 小时前
对golang中CSP的理解
开发语言·后端·golang