当前位置: 首页 > news >正文

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 类中添加列表相关的方法。

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操作的端点。

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,实现更高效和更复杂的地理信息处理系统。

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 响应式Web设计:纯HTML和CSS的实现技巧-1
  • Java 入门指南:注解(Annotation)
  • Linux系统下的容器安全:深入解析与最佳实践
  • 《AI办公类工具PPT系列之三——Gamma APP》
  • appium下载及安装
  • .NET开源纪元:穿越封闭的迷雾,拥抱开放的星辰
  • vue3-基础
  • 从零开始学cv-5: 图像的仿射变换
  • FlinkCEP - Flink的复杂事件处理详解
  • 推荐使用阿贝云免费云服务器、免费虚拟主机
  • docker GBase 8sV8.8使用的常见错误
  • pip出问题了,重装,获取python3.7 get-pip.py
  • python(7) : Windows守护python程序
  • 《Cloud Native Data Center Networking》(云原生数据中心网络设计)读书笔记 -- 07数据中心的边缘
  • 医学装备(即设备全生命周期)管理系统背景及必要性
  • “大数据应用场景”之隔壁老王(连载四)
  • Angular6错误 Service: No provider for Renderer2
  • docker-consul
  • Gradle 5.0 正式版发布
  • Invalidate和postInvalidate的区别
  • Less 日常用法
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • Linux中的硬链接与软链接
  • mac修复ab及siege安装
  • OSS Web直传 (文件图片)
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • Tornado学习笔记(1)
  • V4L2视频输入框架概述
  • 从零开始学习部署
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 猴子数据域名防封接口降低小说被封的风险
  • 检测对象或数组
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 排序算法学习笔记
  • 驱动程序原理
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 手写双向链表LinkedList的几个常用功能
  • 我是如何设计 Upload 上传组件的
  • 云大使推广中的常见热门问题
  • 最近的计划
  • 2017年360最后一道编程题
  • Salesforce和SAP Netweaver里数据库表的元数据设计
  • Spring第一个helloWorld
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • ​你们这样子,耽误我的工作进度怎么办?
  • ​如何使用QGIS制作三维建筑
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • (+4)2.2UML建模图
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (动态规划)5. 最长回文子串 java解决
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (每日一问)计算机网络:浏览器输入一个地址到跳出网页这个过程中发生了哪些事情?(废话少说版)
  • (一)springboot2.7.6集成activit5.23.0之集成引擎
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • (转)一些感悟