Merge pull request 'server_2024_3_26_liyuan' (#4) from server_2024_3_26_liyuan into server_2024_3_26
Reviewed-on: #4server_2024_3_26
commit
b69f010915
8
pom.xml
8
pom.xml
|
@ -36,6 +36,7 @@
|
|||
<poi.version>4.1.2</poi.version>
|
||||
<transmittable-thread-local.version>2.14.3</transmittable-thread-local.version>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<mqttv3.version>1.2.5</mqttv3.version>
|
||||
</properties>
|
||||
|
||||
<!-- 依赖声明 -->
|
||||
|
@ -236,6 +237,13 @@
|
|||
<version>${zhilian.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--mqtt版本管理-->
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>${mqttv3.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
package com.zhilian.common.redis.service;
|
||||
|
||||
import io.lettuce.core.output.DoubleListOutput;
|
||||
import org.apache.poi.ss.formula.functions.T;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.BoundSetOperations;
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* spring redis 工具类
|
||||
|
@ -27,7 +27,7 @@ public class RedisService {
|
|||
* @param key 缓存的键值
|
||||
* @param value 缓存的值
|
||||
*/
|
||||
public <T> void setCacheObject (final String key, final T value) {
|
||||
public <T> void setCacheObject(final String key, final T value) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
|
@ -39,7 +39,7 @@ public class RedisService {
|
|||
* @param timeout 时间
|
||||
* @param timeUnit 时间颗粒度
|
||||
*/
|
||||
public <T> void setCacheObject (final String key, final T value, final Long timeout, final TimeUnit timeUnit) {
|
||||
public <T> void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) {
|
||||
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
|
||||
}
|
||||
|
||||
|
@ -48,10 +48,9 @@ public class RedisService {
|
|||
*
|
||||
* @param key Redis键
|
||||
* @param timeout 超时时间
|
||||
*
|
||||
* @return true=设置成功;false=设置失败
|
||||
*/
|
||||
public boolean expire (final String key, final long timeout) {
|
||||
public boolean expire(final String key, final long timeout) {
|
||||
return expire(key, timeout, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
@ -61,10 +60,9 @@ public class RedisService {
|
|||
* @param key Redis键
|
||||
* @param timeout 超时时间
|
||||
* @param unit 时间单位
|
||||
*
|
||||
* @return true=设置成功;false=设置失败
|
||||
*/
|
||||
public boolean expire (final String key, final long timeout, final TimeUnit unit) {
|
||||
public boolean expire(final String key, final long timeout, final TimeUnit unit) {
|
||||
return redisTemplate.expire(key, timeout, unit);
|
||||
}
|
||||
|
||||
|
@ -72,10 +70,9 @@ public class RedisService {
|
|||
* 获取有效时间
|
||||
*
|
||||
* @param key Redis键
|
||||
*
|
||||
* @return 有效时间
|
||||
*/
|
||||
public long getExpire (final String key) {
|
||||
public long getExpire(final String key) {
|
||||
return redisTemplate.getExpire(key);
|
||||
}
|
||||
|
||||
|
@ -83,10 +80,9 @@ public class RedisService {
|
|||
* 判断 key是否存在
|
||||
*
|
||||
* @param key 键
|
||||
*
|
||||
* @return true 存在 false不存在
|
||||
*/
|
||||
public Boolean hasKey (String key) {
|
||||
public Boolean hasKey(String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
|
@ -94,10 +90,9 @@ public class RedisService {
|
|||
* 获得缓存的基本对象。
|
||||
*
|
||||
* @param key 缓存键值
|
||||
*
|
||||
* @return 缓存键值对应的数据
|
||||
*/
|
||||
public <T> T getCacheObject (final String key) {
|
||||
public <T> T getCacheObject(final String key) {
|
||||
ValueOperations<String, T> operation = redisTemplate.opsForValue();
|
||||
return operation.get(key);
|
||||
}
|
||||
|
@ -107,7 +102,7 @@ public class RedisService {
|
|||
*
|
||||
* @param key
|
||||
*/
|
||||
public boolean deleteObject (final String key) {
|
||||
public boolean deleteObject(final String key) {
|
||||
return redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
|
@ -115,10 +110,9 @@ public class RedisService {
|
|||
* 删除集合对象
|
||||
*
|
||||
* @param collection 多个对象
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean deleteObject (final Collection collection) {
|
||||
public boolean deleteObject(final Collection collection) {
|
||||
return redisTemplate.delete(collection) > 0;
|
||||
}
|
||||
|
||||
|
@ -127,10 +121,9 @@ public class RedisService {
|
|||
*
|
||||
* @param key 缓存的键值
|
||||
* @param dataList 待缓存的List数据
|
||||
*
|
||||
* @return 缓存的对象
|
||||
*/
|
||||
public <T> long setCacheList (final String key, final List<T> dataList) {
|
||||
public <T> long setCacheList(final String key, final List<T> dataList) {
|
||||
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
|
||||
return count == null ? 0 : count;
|
||||
}
|
||||
|
@ -139,10 +132,9 @@ public class RedisService {
|
|||
* 获得缓存的list对象
|
||||
*
|
||||
* @param key 缓存的键值
|
||||
*
|
||||
* @return 缓存键值对应的数据
|
||||
*/
|
||||
public <T> List<T> getCacheList (final String key) {
|
||||
public <T> List<T> getCacheList(final String key) {
|
||||
return redisTemplate.opsForList().range(key, 0, -1);
|
||||
}
|
||||
|
||||
|
@ -151,10 +143,9 @@ public class RedisService {
|
|||
*
|
||||
* @param key 缓存键值
|
||||
* @param dataSet 缓存的数据
|
||||
*
|
||||
* @return 缓存数据的对象
|
||||
*/
|
||||
public <T> BoundSetOperations<String, T> setCacheSet (final String key, final Set<T> dataSet) {
|
||||
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet) {
|
||||
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
|
||||
Iterator<T> it = dataSet.iterator();
|
||||
while (it.hasNext()) {
|
||||
|
@ -167,10 +158,9 @@ public class RedisService {
|
|||
* 获得缓存的set
|
||||
*
|
||||
* @param key
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public <T> Set<T> getCacheSet (final String key) {
|
||||
public <T> Set<T> getCacheSet(final String key) {
|
||||
return redisTemplate.opsForSet().members(key);
|
||||
}
|
||||
|
||||
|
@ -180,7 +170,7 @@ public class RedisService {
|
|||
* @param key
|
||||
* @param dataMap
|
||||
*/
|
||||
public <T> void setCacheMap (final String key, final Map<String, T> dataMap) {
|
||||
public <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
|
||||
if (dataMap != null) {
|
||||
redisTemplate.opsForHash().putAll(key, dataMap);
|
||||
}
|
||||
|
@ -190,10 +180,9 @@ public class RedisService {
|
|||
* 获得缓存的Map
|
||||
*
|
||||
* @param key
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public <T> Map<String, T> getCacheMap (final String key) {
|
||||
public <T> Map<String, T> getCacheMap(final String key) {
|
||||
return redisTemplate.opsForHash().entries(key);
|
||||
}
|
||||
|
||||
|
@ -204,7 +193,7 @@ public class RedisService {
|
|||
* @param hKey Hash键
|
||||
* @param value 值
|
||||
*/
|
||||
public <T> void setCacheMapValue (final String key, final String hKey, final T value) {
|
||||
public <T> void setCacheMapValue(final String key, final String hKey, final T value) {
|
||||
redisTemplate.opsForHash().put(key, hKey, value);
|
||||
}
|
||||
|
||||
|
@ -213,10 +202,9 @@ public class RedisService {
|
|||
*
|
||||
* @param key Redis键
|
||||
* @param hKey Hash键
|
||||
*
|
||||
* @return Hash中的对象
|
||||
*/
|
||||
public <T> T getCacheMapValue (final String key, final String hKey) {
|
||||
public <T> T getCacheMapValue(final String key, final String hKey) {
|
||||
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
|
||||
return opsForHash.get(key, hKey);
|
||||
}
|
||||
|
@ -226,10 +214,9 @@ public class RedisService {
|
|||
*
|
||||
* @param key Redis键
|
||||
* @param hKeys Hash键集合
|
||||
*
|
||||
* @return Hash对象集合
|
||||
*/
|
||||
public <T> List<T> getMultiCacheMapValue (final String key, final Collection<Object> hKeys) {
|
||||
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) {
|
||||
return redisTemplate.opsForHash().multiGet(key, hKeys);
|
||||
}
|
||||
|
||||
|
@ -238,21 +225,155 @@ public class RedisService {
|
|||
*
|
||||
* @param key Redis键
|
||||
* @param hKey Hash键
|
||||
*
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean deleteCacheMapValue (final String key, final String hKey) {
|
||||
public boolean deleteCacheMapValue(final String key, final String hKey) {
|
||||
return redisTemplate.opsForHash().delete(key, hKey) > 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 向Zset中存入单条数据
|
||||
*
|
||||
* @param zkey ZSet键
|
||||
* @param value 值
|
||||
* @param score 分数
|
||||
*/
|
||||
public <T> void setCacheZsetValue(final String zkey, final T value, final Double score) {
|
||||
redisTemplate.opsForZSet().add(zkey, value, score);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向Zset中存入多条数据
|
||||
*
|
||||
* @param zkey zSet键
|
||||
* @param map => 键值对 键T 值Double分数
|
||||
*/
|
||||
public <T> void setCacheZsetBatchValue(final String zkey, final Map<T, Double> map) {
|
||||
Set<DefaultTypedTuple> collect = map.entrySet().stream()
|
||||
.map(item -> {
|
||||
return new DefaultTypedTuple(item.getKey(), item.getValue());
|
||||
})
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
redisTemplate.opsForZSet().add(zkey, collect);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Zset集合大小
|
||||
*
|
||||
* @param zkey
|
||||
* @return size
|
||||
*/
|
||||
public Long getCacheZsetSize(String zkey) {
|
||||
return redisTemplate.opsForZSet().size(zkey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询成员分数
|
||||
*
|
||||
* @param zkey
|
||||
* @param value
|
||||
* @param <T>
|
||||
* @return score
|
||||
*/
|
||||
public <T> Double getCacheZsetScore(final String zkey, final T value) {
|
||||
return redisTemplate.opsForZSet().score(zkey, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 范围查询分数
|
||||
*
|
||||
* @param zkey
|
||||
* @param start
|
||||
* @param end
|
||||
* @return
|
||||
*/
|
||||
public <T> Set<Double> getCacheZsetScoreBatch(final String zkey, final Long start, final Long end) {
|
||||
return redisTemplate.opsForZSet().range(zkey, start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* 范围查询集合携带分数
|
||||
* @param zkey
|
||||
* @param start
|
||||
* @param end
|
||||
* @return
|
||||
*/
|
||||
public <T> Set<HashMap<Object, Double>> getCacheZsetScoreWithDataBatch(final String zkey, final Long start, final Long end) {
|
||||
Set<ZSetOperations.TypedTuple<Object>> rangeSet = redisTemplate.opsForZSet().range(zkey, start, end);
|
||||
Set<HashMap<Object, Double>> collect = rangeSet.stream().
|
||||
map(item -> {
|
||||
HashMap<Object, Double> objectDoubleHashMap = new HashMap<Object, Double>();
|
||||
objectDoubleHashMap.put(item.getValue(), item.getScore());
|
||||
return objectDoubleHashMap;
|
||||
})
|
||||
.collect(Collectors.toSet());
|
||||
return collect;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加分数
|
||||
* @param zkey
|
||||
* @param value
|
||||
* @param delta
|
||||
* @param <T>
|
||||
*/
|
||||
public <T> Double incrementScore(final String zkey, final T value, final Double delta){
|
||||
return redisTemplate.opsForZSet().incrementScore(zkey,value,delta);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除元素
|
||||
* @param zkey
|
||||
* @param values
|
||||
* @return
|
||||
* @param <T>
|
||||
*/
|
||||
public <T> Long removeCacheZsetBatch(final String zkey,final T... values){
|
||||
return redisTemplate.opsForZSet().remove(zkey,values);
|
||||
}
|
||||
|
||||
/**
|
||||
* 排序并限制结果集大小,按降序返回有序集合的部分元素,支持指定索引范围。
|
||||
* @param zkey
|
||||
* @param start
|
||||
* @param end
|
||||
* @return
|
||||
*/
|
||||
public Set<Double> reverseRange(final String zkey,final Long start,final Long end){
|
||||
return redisTemplate.opsForZSet().reverseRange(zkey,start,end);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按分数范围查询
|
||||
* @param zkey
|
||||
* @param min
|
||||
* @param max
|
||||
* @return
|
||||
*/
|
||||
public Set<Double> rangeByScore(final String zkey,final Double min,final Double max){
|
||||
return redisTemplate.opsForZSet().rangeByScore(zkey,min,max);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最小值
|
||||
* @param zkey
|
||||
* @return
|
||||
*/
|
||||
public Map<Object, Double> getCacheZsetMin(final String zkey){
|
||||
ZSetOperations.TypedTuple typedTuple = redisTemplate.opsForZSet().popMin(zkey);
|
||||
return new HashMap<Object,Double>(){{put(typedTuple.getValue(),typedTuple.getScore());}};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获得缓存的基本对象列表
|
||||
*
|
||||
* @param pattern 字符串前缀
|
||||
*
|
||||
* @return 对象列表
|
||||
*/
|
||||
public Collection<String> keys (final String pattern) {
|
||||
public Collection<String> keys(final String pattern) {
|
||||
return redisTemplate.keys(pattern);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package com.zhilian.business;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @BelongsProject: smart-cloud-server
|
||||
* @BelongsPackage: com.zhilian.business
|
||||
* @Author: LiYuan
|
||||
* @CreateTime: 2024-03-31 10:13
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class ZhilianBusinessApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ZhilianBusinessApplication.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.zhilian.manager;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @BelongsProject: smart-cloud-server
|
||||
* @BelongsPackage: com.zhilian.manager
|
||||
* @Author: LiYuan
|
||||
* @CreateTime: 2024-03-31 10:17
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class ZhilianMagagerApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ZhilianMagagerApplication.class,args);
|
||||
}
|
||||
}
|
|
@ -78,6 +78,18 @@
|
|||
<artifactId>zhilian-common-swagger</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--rabbitMQ服务-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.amqp</groupId>
|
||||
<artifactId>spring-rabbit-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mqttx依赖-->
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.zhilian.online;
|
||||
|
||||
import com.zhilian.common.security.annotation.EnableCustomConfig;
|
||||
import com.zhilian.common.security.annotation.EnableMyFeignClients;
|
||||
import com.zhilian.common.swagger.annotation.EnableCustomSwagger2;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description: 智联车辆上线服务启动类
|
||||
* @date: 2024/3/29 20:36
|
||||
*/
|
||||
@EnableCustomConfig
|
||||
@EnableCustomSwagger2
|
||||
@EnableMyFeignClients
|
||||
@SpringBootApplication
|
||||
public class ZhiLianOnlineApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ZhiLianOnlineApplication.class,args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
package com.zhilian.online.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* @BelongsProject: smart-cloud-server
|
||||
* @BelongsPackage: com.zhilian.online.config
|
||||
* @Author: LiYuan
|
||||
* @CreateTime: 2024-03-31 09:45
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class MqttxConfig {
|
||||
|
||||
/**
|
||||
* 路径
|
||||
*/
|
||||
@Value("${mqtt.server.broker}")
|
||||
private String broker;
|
||||
|
||||
/**
|
||||
* 主题
|
||||
*/
|
||||
@Value("${mqtt.server.topic}")
|
||||
private String topic;
|
||||
|
||||
/**
|
||||
* 客户端ID
|
||||
*/
|
||||
@Value("${mqtt.server.clientId}")
|
||||
private String clientId;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
@Value("${mqtt.server.username}")
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
@Value("${mqtt.server.password}")
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* qos
|
||||
*/
|
||||
@Value("${mqtt.server.qos}")
|
||||
private Integer qos;
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void initMqtt() {
|
||||
log.info("mqttx连接中......");
|
||||
|
||||
try {
|
||||
MqttClient mqttClient = new MqttClient(broker, clientId, new MemoryPersistence());
|
||||
|
||||
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
|
||||
|
||||
mqttConnectOptions.setUserName(username);
|
||||
mqttConnectOptions.setPassword(password.toCharArray());
|
||||
|
||||
//链接超时
|
||||
mqttConnectOptions.setConnectionTimeout(60);
|
||||
//心跳检测
|
||||
mqttConnectOptions.setKeepAliveInterval(60);
|
||||
mqttClient.connect(mqttConnectOptions);
|
||||
log.info("mqtt{}服务连接成功", broker);
|
||||
|
||||
//回调
|
||||
mqttClient.setCallback(new MqttCallback() {
|
||||
@Override
|
||||
public void connectionLost(Throwable throwable) {
|
||||
log.error("连接断开{}", throwable.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
||||
log.info("消息到达,接受消息主题{},接受消息Qos{},接受消息内容{}", topic, mqttMessage.getQos(), new String(mqttMessage.getPayload()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||
log.info("消息{}发送成功" + iMqttDeliveryToken.isComplete());
|
||||
}
|
||||
});
|
||||
|
||||
mqttClient.subscribe(topic,qos);
|
||||
|
||||
} catch (MqttException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,164 @@
|
|||
package com.zhilian.online.config;/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description:
|
||||
* @date: 2024/3/30 19:33
|
||||
*/
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.core.Binding;
|
||||
import org.springframework.amqp.core.BindingBuilder;
|
||||
import org.springframework.amqp.core.DirectExchange;
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @BelongsProject: smart-cloud-server
|
||||
* @BelongsPackage: com.zhilian.online.config
|
||||
* @Author: LiYuan
|
||||
* @CreateTime: 2024-03-30 19:33
|
||||
* @Description: 负载中心RabbitMQ配置
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class RabbitConfig {
|
||||
|
||||
/**
|
||||
* 延迟队列名称
|
||||
*/
|
||||
public static final String DELAY_QUEUE_NAME = "delay_queue";
|
||||
|
||||
/**
|
||||
* 延迟交换机名称
|
||||
*/
|
||||
public static final String DELAY_EXCHANGE_NAME = "delay_exchange";
|
||||
|
||||
/**
|
||||
* 延迟路由键
|
||||
*/
|
||||
public static final String DELAY_ROUTING_KEY = "delay_routing";
|
||||
|
||||
/**
|
||||
* 死信队列名称
|
||||
*/
|
||||
public static final String DEAD_QUEUE_NAME = "dead_queue";
|
||||
|
||||
/**
|
||||
* 死信交换机名称
|
||||
*/
|
||||
public static final String DEAD_EXCHANGE_NAME = "dead_exchange";
|
||||
|
||||
/**
|
||||
* 死信路由键
|
||||
*/
|
||||
public static final String DEAD_ROUTING_KEY = "dead_routing";
|
||||
|
||||
/**
|
||||
* 注入死信队列实例
|
||||
*
|
||||
* @return Queue
|
||||
*/
|
||||
@Bean
|
||||
Queue deadQueue() {
|
||||
log.info("死信队列创建成功");
|
||||
return new Queue(DEAD_QUEUE_NAME, true, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入死信交换机实例
|
||||
*
|
||||
* @return DirectExchange
|
||||
*/
|
||||
@Bean
|
||||
DirectExchange deadExchange() {
|
||||
log.info("死信交换机创建成功");
|
||||
return new DirectExchange(DEAD_EXCHANGE_NAME, true, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将死信队列与死信交换机绑定
|
||||
*
|
||||
* @return Binding
|
||||
*/
|
||||
@Bean
|
||||
Binding deadBinding() {
|
||||
log.info("死信通道建立成功");
|
||||
return BindingBuilder.bind(deadQueue())
|
||||
.to(deadExchange())
|
||||
.with(DEAD_ROUTING_KEY);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 注入延迟队列实例
|
||||
*
|
||||
* @return Queue
|
||||
*/
|
||||
@Bean
|
||||
Queue delayQueue() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
//设置消息过期时间为30秒
|
||||
map.put("x-message-ttl", 1000 * 30);
|
||||
//设置消息过期后存储的死信交换机
|
||||
map.put("x-dead-letter-exchange", DEAD_EXCHANGE_NAME);
|
||||
//设置死信路由键
|
||||
map.put("x-dead-letter-routing-key", DEAD_ROUTING_KEY);
|
||||
log.info("延迟队列创建成功");
|
||||
return new Queue(DELAY_QUEUE_NAME, true, false, false, map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入延迟交换机实例
|
||||
*
|
||||
* @return DirectExchange
|
||||
*/
|
||||
@Bean
|
||||
DirectExchange delayExchange() {
|
||||
log.info("延迟交换机创建成功");
|
||||
return new DirectExchange(DELAY_EXCHANGE_NAME, true, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将延迟队列与延迟交换机绑定
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
Binding delayBinding() {
|
||||
log.info("延迟通道创建成功");
|
||||
return BindingBuilder.bind(deadQueue())
|
||||
.to(delayExchange())
|
||||
.with(DELAY_ROUTING_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置RabbitMq序列化器
|
||||
* @param connectionFactory
|
||||
* @return
|
||||
*/
|
||||
// @Bean
|
||||
// public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
|
||||
// RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
|
||||
// rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
|
||||
// return rabbitTemplate;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// @Bean
|
||||
// public MessageConverter jsonMessageConverter(){
|
||||
// return new Jackson2JsonMessageConverter();
|
||||
// }
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.zhilian.online.constans;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @BelongsProject: smart-cloud-server
|
||||
* @BelongsPackage: com.zhilian.online.constans
|
||||
* @Author: LiYuan
|
||||
* @CreateTime: 2024-03-31 09:35
|
||||
* @Description: 车辆网关上线服务常量
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Data
|
||||
public class OnlineConstants {
|
||||
|
||||
/**
|
||||
* 申请注册令牌缓存前缀
|
||||
*/
|
||||
public static final String ONLINE_TOKEN_PREFIX = "online_token:";
|
||||
|
||||
/**
|
||||
* 申请令牌过期时间
|
||||
*/
|
||||
public static final Long ONLINE_TOKEN_EXPIRE = 60L;
|
||||
|
||||
/**
|
||||
* 节点负载前缀
|
||||
*/
|
||||
public static final String NODE_LOAD_PREFIX = "node_load";
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.zhilian.online.consumer;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.zhilian.online.config.RabbitConfig;
|
||||
import com.zhilian.online.domain.req.GatherRegReq;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @BelongsProject: smart-cloud-server
|
||||
* @BelongsPackage: com.zhilian.online.consumer
|
||||
* @Author: LiYuan
|
||||
* @CreateTime: 2024-03-30 19:57
|
||||
* @Description: 监听死信队列消费者
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class DeadQueueConsumer {
|
||||
|
||||
|
||||
/**
|
||||
* 消费死信队列中的消息,确保车辆上线成功
|
||||
*/
|
||||
@RabbitListener(queues = RabbitConfig.DEAD_QUEUE_NAME)
|
||||
public void SecureOnline(String gatherRegReqMsg) {
|
||||
GatherRegReq gatherRegReq = JSON.parseObject(gatherRegReqMsg, GatherRegReq.class);
|
||||
log.info("开始检查节点{}的上线状态......",
|
||||
gatherRegReq.getClientId());
|
||||
|
||||
|
||||
log.info("节点上线成功");
|
||||
|
||||
log.info("节点上线失败");
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.zhilian.online.controller;
|
||||
|
||||
import com.zhilian.common.core.domain.Result;
|
||||
|
||||
import com.zhilian.online.service.OnlineGatherService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description: 车辆收集节点控制层
|
||||
* @date: 2024/3/29 20:46
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/gather")
|
||||
public class OnlineGatherController {
|
||||
|
||||
/**
|
||||
* 车辆收集节点
|
||||
*/
|
||||
@Autowired
|
||||
private OnlineGatherService onlineGatherService;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
package com.zhilian.online.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.zhilian.common.core.domain.Result;
|
||||
import com.zhilian.common.core.utils.ip.IpUtils;
|
||||
import com.zhilian.common.core.web.controller.BaseController;
|
||||
|
||||
import com.zhilian.online.domain.Gather;
|
||||
import com.zhilian.online.domain.req.GatherRegReq;
|
||||
import com.zhilian.online.service.OnlineLoadCenterService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description: 负载中心Controller
|
||||
* @date: 2024/3/29 20:40
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/load/center")
|
||||
@Slf4j
|
||||
public class OnlineLoadCenterController extends BaseController {
|
||||
|
||||
/**
|
||||
* 请求对象
|
||||
*/
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
/**
|
||||
* 车辆收集节点服务层
|
||||
*/
|
||||
@Autowired
|
||||
private OnlineLoadCenterService onlineLoadCenterService;
|
||||
|
||||
|
||||
/**
|
||||
* @description: 节点申请注解令牌接口,仅限内网访问
|
||||
* @author: LiYuan
|
||||
* @param: vehicle
|
||||
* @return: Result<OnlineAccount>
|
||||
**/
|
||||
@GetMapping("/applyForReg")
|
||||
public Result<String> applyForReg(){
|
||||
log.info("申请注册令牌");
|
||||
return onlineLoadCenterService.applyForReg();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description: 收集节点注册
|
||||
* @author: LiYuan
|
||||
* @param: Gather
|
||||
* @return: Result
|
||||
**/
|
||||
@PostMapping("/regGather")
|
||||
public Result regGather(@Validated Gather gather){
|
||||
String ipAddr = IpUtils.getIpAddr(request);
|
||||
gather.setIpAddress(ipAddr);
|
||||
log.info("节点{}正在上线",gather);
|
||||
return onlineLoadCenterService.regGather(gather);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 车辆上线至收集节点
|
||||
* @return null
|
||||
*/
|
||||
public Result getConnectionOption(){
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.zhilian.online.controller;/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description:
|
||||
* @date: 2024/3/30 11:18
|
||||
*/
|
||||
|
||||
import com.zhilian.common.core.domain.Result;
|
||||
import com.zhilian.online.domain.model.MqttServerModel;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @BelongsProject: smart-cloud-server
|
||||
* @BelongsPackage: com.zhilian.online.controller
|
||||
* @Author: LiYuan
|
||||
* @CreateTime: 2024-03-30 11:18
|
||||
* @Description: 新增车辆上线验证控制器
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/verify")
|
||||
public class OnlineVerifyController {
|
||||
|
||||
/**
|
||||
* 读取配置文件中的mqtt服务地址
|
||||
*/
|
||||
@Value("${mqtt.server.broker}")
|
||||
private String broker;
|
||||
|
||||
/**
|
||||
* 读取配置文件中的订阅主题
|
||||
*/
|
||||
@Value("${mqtt.server.topic}")
|
||||
private String topic;
|
||||
|
||||
@PostMapping("/vehicleConnection")
|
||||
public Result<MqttServerModel> vehicleConnection() {
|
||||
return Result.success(
|
||||
MqttServerModel.builder()
|
||||
.broker(broker)
|
||||
.topic(topic)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.zhilian.online.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.zhilian.common.core.web.domain.BaseEntity;
|
||||
import lombok.*;
|
||||
import org.bouncycastle.util.IPAddress;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* @BelongsProject: smart-cloud-server
|
||||
* @BelongsPackage: com.zhilian.online.domain
|
||||
* @Author: LiYuan
|
||||
* @CreateTime: 2024-03-31 08:58
|
||||
* @Description: 收集节点实体类
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
public class Gather extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 收集节点ID
|
||||
*/
|
||||
@TableId(type = IdType.INPUT)
|
||||
private String clientId;
|
||||
|
||||
// /**
|
||||
// * 收集节点登录令牌
|
||||
// */
|
||||
// @NotBlank
|
||||
// private String token;
|
||||
|
||||
/**
|
||||
* broker
|
||||
*/
|
||||
@NotBlank
|
||||
private String broker;
|
||||
|
||||
/**
|
||||
* username
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* password
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* qos
|
||||
*/
|
||||
private Integer qos;
|
||||
|
||||
/**
|
||||
* topic
|
||||
*/
|
||||
@NotBlank
|
||||
private String topic;
|
||||
|
||||
/**
|
||||
* ip地址
|
||||
*/
|
||||
private String ipAddress;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
//package com.zhilian.online.domain;/**
|
||||
// * @version:
|
||||
// * @Author: LiYuan
|
||||
// * @description:
|
||||
// * @date: 2024/3/29 21:44
|
||||
// */
|
||||
//
|
||||
//import com.baomidou.mybatisplus.annotation.IdType;
|
||||
//import com.baomidou.mybatisplus.annotation.TableId;
|
||||
//import com.baomidou.mybatisplus.annotation.TableName;
|
||||
//import com.zhilian.common.core.web.domain.BaseEntity;
|
||||
//import lombok.*;
|
||||
//
|
||||
//import javax.validation.constraints.NotBlank;
|
||||
//import javax.validation.constraints.NotNull;
|
||||
//import java.util.Date;
|
||||
//
|
||||
///**
|
||||
// *@BelongsProject: smart-cloud-server
|
||||
// *@BelongsPackage: com.zhilian.online.domain
|
||||
// *@Author: LiYuan
|
||||
// *@CreateTime: 2024-03-29 21:44
|
||||
// *@Description: 汽车类,测试使用
|
||||
// *@Version: 1.0
|
||||
// */
|
||||
//@Data
|
||||
////@AllArgsConstructor
|
||||
////@NoArgsConstructor
|
||||
//@Builder
|
||||
//@ToString
|
||||
//@EqualsAndHashCode(callSuper = true)
|
||||
//@TableName("vehicle")
|
||||
//public class Vehicle extends BaseEntity {
|
||||
//
|
||||
//
|
||||
//
|
||||
//}
|
|
@ -0,0 +1,70 @@
|
|||
package com.zhilian.online.domain;/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description:
|
||||
* @date: 2024/3/29 21:49
|
||||
*/
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.zhilian.common.core.web.domain.BaseEntity;
|
||||
import lombok.*;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
*@BelongsProject: smart-cloud-server
|
||||
*@BelongsPackage: com.zhilian.online.domain
|
||||
*@Author: LiYuan
|
||||
*@CreateTime: 2024-03-29 21:49
|
||||
*@Description: 车辆申请注册成功后返回的一次性账户数据
|
||||
*@Version: 1.0
|
||||
*/
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NotBlank
|
||||
@ToString
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("vehicle_account")
|
||||
public class VehicleAccount extends BaseEntity{
|
||||
|
||||
|
||||
/**
|
||||
* 车辆VIN
|
||||
*/
|
||||
@TableId(type = IdType.INPUT)
|
||||
private String vin;
|
||||
|
||||
|
||||
/**
|
||||
* 申请时间
|
||||
*/
|
||||
@NotNull
|
||||
private Date applyTime;
|
||||
|
||||
/**
|
||||
* 申请唯一标识
|
||||
*/
|
||||
@NotBlank
|
||||
private String applyCode;
|
||||
|
||||
/**
|
||||
* 一次性用户名
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 一次性密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 可以进行连接的收集节点id
|
||||
*/
|
||||
private String clientId;
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.zhilian.online.domain.model;/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description:
|
||||
* @date: 2024/3/30 11:19
|
||||
*/
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
*@BelongsProject: smart-cloud-server
|
||||
*@BelongsPackage: com.zhilian.online.domain.model
|
||||
*@Author: LiYuan
|
||||
*@CreateTime: 2024-03-30 11:19
|
||||
*@Description: Mqtt服务模型
|
||||
*@Version: 1.0
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MqttServerModel {
|
||||
|
||||
/**
|
||||
* Mqtt服务节点
|
||||
*/
|
||||
private String broker;
|
||||
|
||||
/**
|
||||
* Mqtt订阅主题
|
||||
*/
|
||||
private String topic;
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.zhilian.online.domain.req;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* @BelongsProject: smart-cloud-server
|
||||
* @BelongsPackage: com.zhilian.online.domain.req
|
||||
* @Author: LiYuan
|
||||
* @CreateTime: 2024-03-31 09:03
|
||||
* @Description: 节点申请注册请求体
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
@Builder
|
||||
public class GatherRegReq {
|
||||
|
||||
/**
|
||||
* 客户端ID
|
||||
*/
|
||||
@NotBlank
|
||||
private String clientId;
|
||||
|
||||
/**
|
||||
* 令牌
|
||||
*/
|
||||
private String token;
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.zhilian.online.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description: 车辆收集节点数据层
|
||||
* @date: 2024/3/29 20:48
|
||||
*/
|
||||
@Mapper
|
||||
public interface OnlineGatherMapper{
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.zhilian.online.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.zhilian.online.domain.VehicleAccount;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description: 车辆负载中心数据层
|
||||
* @date: 2024/3/29 20:45
|
||||
*/
|
||||
@Mapper
|
||||
public interface OnlineLoadCenterMapper extends BaseMapper<VehicleAccount> {
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.zhilian.online.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.zhilian.common.core.domain.Result;
|
||||
|
||||
|
||||
/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description: 车辆收集节点Service接口
|
||||
* @date: 2024/3/29 20:48
|
||||
*/
|
||||
public interface OnlineGatherService{
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.zhilian.online.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.zhilian.common.core.domain.Result;
|
||||
import com.zhilian.online.domain.Gather;
|
||||
import com.zhilian.online.domain.VehicleAccount;
|
||||
import com.zhilian.online.domain.req.GatherRegReq;
|
||||
|
||||
/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description: 车辆负载中心服务接口
|
||||
* @date: 2024/3/29 20:42
|
||||
*/
|
||||
public interface OnlineLoadCenterService extends IService<VehicleAccount> {
|
||||
|
||||
/**
|
||||
* @description: 车辆申请注解令牌接口,仅限内网访问
|
||||
* @author: LiYuan
|
||||
* @param: vehicle
|
||||
* @return: Result<OnlineAccount>
|
||||
**/
|
||||
Result<String> applyForReg();
|
||||
|
||||
/**
|
||||
* @description: 车辆使用申请的一次性账户连接收集节点
|
||||
* @author: LiYuan
|
||||
* @param:
|
||||
* @return: Result
|
||||
**/
|
||||
Result regGather(Gather gather);
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.zhilian.online.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.zhilian.common.core.domain.Result;
|
||||
import com.zhilian.online.mapper.OnlineGatherMapper;
|
||||
import com.zhilian.online.service.OnlineGatherService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @version:
|
||||
* @Author: LiYuan
|
||||
* @description: 车辆收集节点服务层
|
||||
* @date: 2024/3/29 20:48
|
||||
*/
|
||||
@Service
|
||||
public class OnlineGatherServiceImpl implements OnlineGatherService {
|
||||
|
||||
@Autowired
|
||||
private OnlineGatherMapper onlineGatherMapper;
|
||||
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
package com.zhilian.online.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.zhilian.common.core.domain.Result;
|
||||
import com.zhilian.common.core.utils.uuid.IdUtils;
|
||||
import com.zhilian.common.redis.service.RedisService;
|
||||
import com.zhilian.online.config.RabbitConfig;
|
||||
import com.zhilian.online.constans.OnlineConstants;
|
||||
import com.zhilian.online.domain.Gather;
|
||||
import com.zhilian.online.domain.VehicleAccount;
|
||||
import com.zhilian.online.domain.req.GatherRegReq;
|
||||
import com.zhilian.online.mapper.OnlineLoadCenterMapper;
|
||||
import com.zhilian.online.service.OnlineLoadCenterService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author LiYuan
|
||||
* @description 车辆负载中心服务层
|
||||
* @date 2024/3/29 20:42
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class OnlineLoadCenterServiceImpl extends ServiceImpl<OnlineLoadCenterMapper, VehicleAccount> implements OnlineLoadCenterService {
|
||||
|
||||
/**
|
||||
* 车辆负载中心数据层
|
||||
*/
|
||||
@Autowired
|
||||
private OnlineLoadCenterMapper onlineLoadCenterMapper;
|
||||
|
||||
/**
|
||||
* Redis服务
|
||||
*/
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* RabbitMQ服务
|
||||
*/
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
/**
|
||||
* @description: 车辆申请注解令牌接口, 仅限内网访问
|
||||
* @author: LiYuan
|
||||
* @param: vehicle
|
||||
* @return: Result<OnlineAccount>
|
||||
**/
|
||||
@Override
|
||||
public Result<String> applyForReg() {
|
||||
|
||||
//生成一次性令牌
|
||||
String token = IdUtils.fastSimpleUUID();
|
||||
|
||||
//将令牌信息缓存到Redis中
|
||||
redisService.setCacheObject(OnlineConstants.ONLINE_TOKEN_PREFIX , token, OnlineConstants.ONLINE_TOKEN_EXPIRE, TimeUnit.MINUTES);
|
||||
|
||||
//将账户信息返回客户端
|
||||
return Result.success(token);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description: 节点上线成功
|
||||
* @author: LiYuan
|
||||
* @param: Gather
|
||||
* @return: Result
|
||||
**/
|
||||
@Override
|
||||
public Result regGather(Gather gather) {
|
||||
// //判断登录令牌是否过期,一致
|
||||
// if (redisService.hasKey(OnlineConstants.ONLINE_TOKEN_PREFIX + gather.getClientId())) {
|
||||
// return Result.error("令牌已过期");
|
||||
// }
|
||||
|
||||
//为该节点创建负载均衡缓存
|
||||
if (redisService.hasKey(OnlineConstants.ONLINE_TOKEN_PREFIX + gather.getClientId())) {
|
||||
redisService.deleteObject(OnlineConstants.ONLINE_TOKEN_PREFIX + gather.getClientId());
|
||||
}
|
||||
redisService.setCacheZsetValue(OnlineConstants.ONLINE_TOKEN_PREFIX + gather.getClientId(), gather, 0.0);
|
||||
|
||||
// //向RabbitMQ||RocketMQ发送30s延迟消息,确保后续节点上线
|
||||
// rabbitTemplate.convertAndSend(RabbitConfig.DELAY_EXCHANGE_NAME, RabbitConfig.DELAY_ROUTING_KEY, JSON.toJSONString(gather));
|
||||
|
||||
return Result.success("节点上线");
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,2 +1,25 @@
|
|||
Spring Boot Version: ${spring-boot.version}
|
||||
Spring Application Name: ${spring.application.name}
|
||||
/*
|
||||
* God Bless No Bugs!
|
||||
*
|
||||
* _ooOoo_
|
||||
* o8888888o
|
||||
* 88" . "88
|
||||
* (| -_- |)
|
||||
* O\ = /O
|
||||
* ____/`---'\____
|
||||
* .' \\| |// `.
|
||||
* / \\||| : |||// \
|
||||
* / _||||| -:- |||||- \
|
||||
* | | \\\ - /// | |
|
||||
* | \_| ''\---/'' | |
|
||||
* \ .-\__ `-` ___/-. /
|
||||
* ___`. .' /--.--\ `. . __
|
||||
* ."" '< `.___\_<|>_/___.' >'"".
|
||||
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
|
||||
* \ \ `-. \_ __\ /__ _/ .-` / /
|
||||
* ======`-.____`-.___\_____/___.-`____.-'======
|
||||
* `=---='
|
||||
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.zhilian.online.mapper.OnlineGatherMapper">
|
||||
|
||||
</mapper>
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.zhilian.online.mapper.OnlineLoadCenterMapper">
|
||||
|
||||
</mapper>
|
|
@ -0,0 +1,19 @@
|
|||
package com.zhilian.resolver;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @BelongsProject: smart-cloud-server
|
||||
* @BelongsPackage: com.zhilian.resolver
|
||||
* @Author: LiYuan
|
||||
* @CreateTime: 2024-03-31 10:11
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class ZhilianResolverApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ZhilianResolverApplication.class,args);
|
||||
}
|
||||
}
|
|
@ -30,25 +30,27 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
|
|||
private RedisService redisService;
|
||||
|
||||
@Override
|
||||
public List<SysConfig> pageQuery (SysConfig config) {
|
||||
public List<SysConfig> pageQuery(SysConfig config) {
|
||||
LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
|
||||
if (StringUtils.isNotEmpty(config.getConfigName())){
|
||||
queryWrapper.like(SysConfig::getConfigName, config.getConfigName());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(config.getConfigType())){
|
||||
queryWrapper.like(SysConfig::getConfigType, config.getConfigType());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(config.getConfigKey())){
|
||||
queryWrapper.like(SysConfig::getConfigKey, config.getConfigKey());
|
||||
}
|
||||
//模糊条件 查询配置名称
|
||||
queryWrapper.like(StringUtils.isNotEmpty(config.getConfigName()), SysConfig::getConfigName, config.getConfigName());
|
||||
|
||||
//模块条件 通过配置进行模糊查询
|
||||
queryWrapper.like(StringUtils.isNotEmpty(config.getConfigType()), SysConfig::getConfigType, config.getConfigType());
|
||||
|
||||
//模糊条件 通过配置键 进行查询
|
||||
queryWrapper.like(StringUtils.isNotEmpty(config.getConfigKey()), SysConfig::getConfigKey, config.getConfigKey());
|
||||
|
||||
Object beginTime = config.getParams().get("beginTime");
|
||||
if (Objects.nonNull(beginTime) && beginTime instanceof Date beginDate){
|
||||
queryWrapper.gt(SysConfig::getCreateTime, beginDate);
|
||||
}
|
||||
|
||||
//区间查询,查询大于配置创建时间的配置
|
||||
queryWrapper.gt(Objects.nonNull(beginTime) && (beginTime instanceof Date beginDate), SysConfig::getCreateTime, beginTime);
|
||||
|
||||
Object endTime = config.getParams().get("endTime");
|
||||
if (Objects.nonNull(endTime) && endTime instanceof Date endDate){
|
||||
queryWrapper.lt(SysConfig::getCreateTime, endDate);
|
||||
}
|
||||
|
||||
//区间查询,查询小于配置创建时间的配置
|
||||
queryWrapper.lt((Objects.nonNull(endTime) && endTime instanceof Date endDate), SysConfig::getCreateTime, endTime);
|
||||
|
||||
return this.list(queryWrapper);
|
||||
}
|
||||
|
||||
|
@ -56,11 +58,10 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
|
|||
* 通过Key进行查询值
|
||||
*
|
||||
* @param configKey
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String selectConfigByKey (String configKey) {
|
||||
public String selectConfigByKey(String configKey) {
|
||||
LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
|
||||
Assert.notNull(configKey, "key不可为空");
|
||||
queryWrapper.eq(SysConfig::getConfigKey, configKey);
|
||||
|
@ -69,14 +70,14 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean checkConfigKeyUnique (SysConfig config) {
|
||||
public boolean checkConfigKeyUnique(SysConfig config) {
|
||||
LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(SysConfig::getConfigKey, config.getConfigKey());
|
||||
return this.count(queryWrapper) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetConfigCache () {
|
||||
public void resetConfigCache() {
|
||||
this.clearConfigCache();
|
||||
this.loadingConfigCache();
|
||||
}
|
||||
|
@ -85,7 +86,7 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
|
|||
* 清空参数缓存数据
|
||||
*/
|
||||
@Override
|
||||
public void clearConfigCache () {
|
||||
public void clearConfigCache() {
|
||||
Collection<String> keys = redisService.keys(CacheConstants.SYS_CONFIG_KEY + "*");
|
||||
redisService.deleteObject(keys);
|
||||
}
|
||||
|
@ -94,7 +95,7 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
|
|||
* 加载参数缓存数据
|
||||
*/
|
||||
@Override
|
||||
public void loadingConfigCache () {
|
||||
public void loadingConfigCache() {
|
||||
List<SysConfig> configsList = this.list();
|
||||
for (SysConfig config : configsList) {
|
||||
redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
|
||||
|
@ -105,10 +106,9 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
|
|||
* 设置cache key
|
||||
*
|
||||
* @param configKey 参数键
|
||||
*
|
||||
* @return 缓存键key
|
||||
*/
|
||||
private String getCacheKey (String configKey) {
|
||||
private String getCacheKey(String configKey) {
|
||||
return CacheConstants.SYS_CONFIG_KEY + configKey;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue