依旧是代驾项目中使用到的功能 特此总结
一个是Redis Geo用于存储和查询地理位置信息(经纬度)
一个是定时任务-分布式 XXL-JOB

Redis Geo

image
https://redis.io/docs/latest/develop/data-types/geospatial/

其实就是存储经纬度key value的东西学习
实际用到计算一个地理位置和另一个地理位置之间的距离

1.GEOADD
添加地理位置信息
语法:GEOADD key longitude latitude member [longitude latitude member ...]
# 添加单个位置
GEOADD cities 116.4074 39.9042 "北京"
# 添加多个位置
GEOADD cities 121.4737 31.2304 "上海" 113.2644 23.1291 "广州" 114.0579 22.5431 "深圳"

2.GEODIST
计算两个位置间的距离
语法:GEODIST key member1 member2 [unit]
GEODIST cities "北京" "上海" (默认单位m)

3.GEOHASH
获取位置的 Geohash 字符串
语法:GEOHASH key member [member ...]
GEOHASH cities "北京"

4.GEOPOS
获取位置的经纬度坐标
语法:GEOPOS key member [member ...]
GEOPOS cities "北京"

5.GEORADIUS
查找指定半径内的位置点
语法:GEORADIUS key longitude latitude radius unit [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
# 查找距离116.4074 39.9042坐标1000公里内的城市
GEORADIUS cities 116.4074 39.9042 1000 km

6.GEORADIUSBYMEMBER
以某个成员为中心查找半径内的位置点
语法:GEORADIUSBYMEMBER key member radius unit [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
GEORADIUSBYMEMBER cities "北京" 1000 km

删除某个集合中的东西
ZREM key 要删的
查看所有创建的
ZRANGE key  0 -1】

我们以redis命令行为测试案例:
GEOADD GEODIST GEOPOS GEORADIUS这几个命令
image
以北京和天津为例 然后计算两者之间的距离

geoadd city 116.4074 39.9042 BeiJing
geoadd city 117.3616 39.3434 TianJin

image

geopos city TianJin

image

geodist city BeiJing TianJin KM

image

georadius city 116 39 150 KM

image


定时任务-分布式 XXL-JOB

https://github.com/xuxueli/xxl-job
image

比如此项目中 顾客呼叫代价 每隔几分钟就搜索一次附近可以接单的司机
每时每刻定时发送信息

微服务集群项目中:使用XXL-JOB
我借助gpt用一个简单案例来验证

首先你需要下载其项目 主要使用admin和core类
image
core类没啥好说的就是工具
主要是调度中心admin类
image

主要是它的配置类:

只研究常用项:
端口号启动调度中心访问:http://localhost:5555/xxl-job-admin
### web
server.port=5555
server.servlet.context-path=/xxl-job-admin

### xxl-job, datasource
spring.datasource.url=jdbc:mysql:/本机数据库/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=用户名
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

你设置的通过token后面执行器需要这个token
### xxl-job, access token
xxl.job.accessToken=default_token 值可更改

日志保存时间
### xxl-job, log retention days
xxl.job.logretentiondays=30

现在开始做:

1.配置SQL文件 启动admin调度中心

你需要配置好admin调度中心的sql文件

image

大致结构如下
image

然后启动admin项目模块 访问调度中心
密码使用算法
image
image

然后这里启动之后显示你注册的执行器
image

接下来开始使用它来配置我们其他分离的模块使用 以下几步都是新的模块

2.导入依赖
        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
            <version>版本号</version>
        </dependency>
3.设置配置类

用xml文件映射或者config配置类都可以
我们简单用config配置类直接配好

@Configuration
public class XxlJobConfig {
	@Bean
	public XxlJobSpringExecutor xxlJobExecutor() {
		XxlJobSpringExecutor executor = new XxlJobSpringExecutor();
		//任务调度中心的位置
		executor.setAdminAddresses("http://127.0.0.1:5555/xxl-job-admin");
		//设置的执行器名字
		executor.setAppname("gaoyuan");
		//任务调度中心的token设置
		executor.setAccessToken("default_token");
		//设置执行器的端口号 其唯一 多个执行器不能重复
		executor.setPort(2222);
		//设置log日志位置
		executor.setLogPath("D:/data/applogs/xxl-job/jobhandler");
		//设置日志保留天数为30天
		executor.setLogRetentionDays(30);
		return executor;
	}
}
4.创建定时任务
@Component
public class SimpleJobHandler {
//GaoJobHandler对应你随后在任务调度中心设置的Handler一致
    @XxlJob("GaoJobHandler")
    public void GaoJobHandler() {
	//控制台显示看一下其多久执行一次
        Date date = new Date();
        System.out.println("XXL-JOB 定时任务执行中... 时间:" + date.toString());
    }
}
5.启动配置项目 任务调度中心管理创建任务

image
corn设置每隔多长时间执行一次 我设置10s/次

执行器要注册成功
image

4

over 测试完毕

posted on 2025-11-03 22:34  蒸饺  阅读(6)  评论(0)    收藏  举报