SpringBoot依赖之Spring Data Redis 实现地理坐标(Geospatial)

Spring Boot 项目中使用 Spring Data Redis 实现地理坐标(Geospatial)

概念

Spring Data Redis (Access+Driver)
  • 依赖名称: Spring Data Redis (Access+Driver)
  • 功能描述: Advanced and thread-safe Java Redis client for synchronous, asynchronous, and reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs and much more.
  • 中文释义:用于同步、异步和反应式使用的高级且线程安全的 Java Redis 客户端。支持集群、哨兵、管道、自动重新连接、编解码器等。

项目学习代码地址

操作演示:

Spring Boot 项目中使用 Spring Data Redis 实现地理坐标(Geospatial)

接下来我们演示在 Spring Boot 项目中使用 Spring Data Redis 实现(Geospatial)操作,我们可以在之前的项目代码基础上扩展 Redis 服务类和控制器类,以支持对 Redis 列表的常见操作。以下是具体的实现步骤。

以下是具体的实现步骤。

1. 更新 Redis 服务类

RedisService 类中添加列表相关的方法。

java 复制代码
package com.dependencies.springdataredis;

import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RedisService {

    private final RedisTemplate<String, Object> redisTemplate;
    private final GeoOperations<String, Object> geoOperations;
  
    public RedisService(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
        this.geoOperations = redisTemplate.opsForGeo();
    }

   private static final String GEO_KEY = "locations";
    
    // 添加地理坐标
    public void addLocation(String member, double longitude, double latitude) {
        geoOperations.add(GEO_KEY, new Point(longitude, latitude), member);
    }
    
    // 根据坐标查询附近的地点
    public List<GeoResult<RedisGeoCommands.GeoLocation<Object>>> getNearbyLocations(double longitude, double latitude, double radius) {
        Circle within = new Circle(new Point(longitude, latitude), new Distance(radius, RedisGeoCommands.DistanceUnit.KILOMETERS));
        return geoOperations.radius(GEO_KEY, within).getContent();
    }
    
    // 获取两地之间的距离
    public Distance getDistance(String member1, String member2) {
        return geoOperations.distance(GEO_KEY, member1, member2, RedisGeoCommands.DistanceUnit.KILOMETERS);
    }
}
2. 更新控制器类

RedisController 中添加处理HyperLogLog操作的端点。

java 复制代码
package com.dependencies.springdataredis;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhizhou   2024/8/20 12:02
 */
@RestController
public class RedisController {

    private final RedisService redisService;

    public RedisController(RedisService redisService) {
        this.redisService = redisService;
    }

      
    @GetMapping("/add-location")
    public String addLocation(@RequestParam String member, @RequestParam double longitude, @RequestParam double latitude) {
        redisService.addLocation(member, longitude, latitude);
        return "Location added.";
    }
    
    @GetMapping("/nearby")
    public List<GeoResult<RedisGeoCommands.GeoLocation<Object>>> getNearbyLocations(@RequestParam double longitude, @RequestParam double latitude, @RequestParam double radius) {
        return redisService.getNearbyLocations(longitude, latitude, radius);
    }
    
    @GetMapping("/distance")
    public Distance getDistance(@RequestParam String member1, @RequestParam String member2) {
        return redisService.getDistance(member1, member2);
    }
}
3. 验证测试:

我们启动项目以后,就通过以下的额 URL 测试 Redis 列表的功能:

Geospatial 操作:

120.209000,30.247100

  • 添加地理位置
    http://localhost:8080/add-location?member=location1&longitude=120.209000&latitude=30.247100
  • 查询附近的地点
    http://localhost:8080/nearby?longitude=120.209000&latitude=30.247100&radius=10
  • 获取两地之间的距离
    http://localhost:8080/distance?member1=location1&member2=location2

操作结果图:


4. 总结

Geospatial(地理空间)

Redis 提供了专门的命令来处理地理位置和半径查询,可以存储地理坐标,并计算给定半径内的元素。

使用场景: 地理位置服务,可以用于实现地理位置相关的功能,比如查询附近的商户、外卖、用户位置服务等。

2023年面试某电气公司,一家准备做充电桩的公司,被问到------设计一个服务,帮用户找到最近的空闲的充电桩,当时回答的就是这个。研究研究还是蛮有意思的。复杂的业务场景还是需要具体场景具体分析,目前这个类型使用场景还是挺多的,如果需要更复杂的操作,可以进一步扩展这些功能,结合其他技术如 Elasticsearch 或 Cassandra,实现更高效和更复杂的地理信息处理系统。

可以关注我,一起学习,一起为程序员职业生涯蓄能。

相关推荐
独自破碎E1 天前
Spring Boot支持哪些嵌入Web容器?
前端·spring boot·后端
疯狂成瘾者1 天前
后端Spring Boot 核心知识点
java·spring boot·后端
IT 行者1 天前
Spring Boot 4.x 安全监控新篇章:基于 ObservationFilterChainDecorator 的可观测性实践
java·spring boot·后端
pyniu1 天前
Spring Boot租房管理系统
java·spring boot·后端
雨中飘荡的记忆1 天前
Spring AI 1.0实战:构建基于DeepSeek的智能客服系统
spring·ai编程
野生技术架构师1 天前
TokenRetryHelper 详解与 Spring Boot 迁移方案
java·spring boot·后端
IT 行者1 天前
告别硬编码!Spring Boot 优雅实现 Controller 路径前缀统一管理
数据库·spring boot·python
蓝程序1 天前
Spring AI学习 程序接入大模型(框架接入)
人工智能·学习·spring
win x1 天前
Redis 持久化
数据库·redis·缓存