|
@@ -1,12 +1,19 @@
|
|
|
package com.xunmei.host.server.service.impl;
|
|
package com.xunmei.host.server.service.impl;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
+import com.alibaba.nacos.api.exception.NacosException;
|
|
|
|
|
+import com.alibaba.nacos.api.naming.NamingService;
|
|
|
|
|
+import com.alibaba.nacos.api.naming.pojo.Instance;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.xunmei.common.core.constant.Constants;
|
|
import com.xunmei.common.core.constant.Constants;
|
|
|
|
|
+import com.xunmei.common.core.constant.ServiceNameConstants;
|
|
|
import com.xunmei.common.core.domain.iot.domain.IotServerInfo;
|
|
import com.xunmei.common.core.domain.iot.domain.IotServerInfo;
|
|
|
import com.xunmei.common.core.enums.iot.IotServerConnectStatus;
|
|
import com.xunmei.common.core.enums.iot.IotServerConnectStatus;
|
|
|
|
|
+import com.xunmei.common.core.utils.JacksonUtils;
|
|
|
import com.xunmei.common.core.utils.uuid.UUID;
|
|
import com.xunmei.common.core.utils.uuid.UUID;
|
|
|
import com.xunmei.common.redis.utils.RedisUtils;
|
|
import com.xunmei.common.redis.utils.RedisUtils;
|
|
|
import com.xunmei.host.server.mapper.IotServerInfoMapper;
|
|
import com.xunmei.host.server.mapper.IotServerInfoMapper;
|
|
@@ -17,10 +24,15 @@ import org.redisson.api.RLock;
|
|
|
import org.redisson.api.RedissonClient;
|
|
import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+import org.springframework.web.client.RestClientException;
|
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* <p>
|
|
* <p>
|
|
@@ -33,6 +45,11 @@ import java.util.concurrent.TimeUnit;
|
|
|
@Service
|
|
@Service
|
|
|
public class IotServerInfoServiceImpl extends ServiceImpl<IotServerInfoMapper, IotServerInfo> implements IotServerInfoService {
|
|
public class IotServerInfoServiceImpl extends ServiceImpl<IotServerInfoMapper, IotServerInfo> implements IotServerInfoService {
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private NamingService namingService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private RestTemplate restTemplate;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public IotServerInfo getToken(String registerCode) {
|
|
public IotServerInfo getToken(String registerCode) {
|
|
@@ -58,7 +75,7 @@ public class IotServerInfoServiceImpl extends ServiceImpl<IotServerInfoMapper, I
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
updateById(serverInfo);
|
|
updateById(serverInfo);
|
|
|
- LogUtils.WEBSOCKET_MSG.error("收到链接请求,已生成token,registerCode:{},token:{}", registerCode,serverInfo.getIotCode());
|
|
|
|
|
|
|
+ LogUtils.WEBSOCKET_MSG.error("收到链接请求,已生成token,registerCode:{},token:{}", registerCode, serverInfo.getIotCode());
|
|
|
return serverInfo;
|
|
return serverInfo;
|
|
|
}
|
|
}
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -113,13 +130,93 @@ public class IotServerInfoServiceImpl extends ServiceImpl<IotServerInfoMapper, I
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public List<IotServerInfo> selectOnlineServer(List<Long> orgIds) {
|
|
public List<IotServerInfo> selectOnlineServer(List<Long> orgIds) {
|
|
|
- LambdaQueryWrapper<IotServerInfo> wrapper=new LambdaQueryWrapper<IotServerInfo>();
|
|
|
|
|
|
|
+ LambdaQueryWrapper<IotServerInfo> wrapper = new LambdaQueryWrapper<IotServerInfo>();
|
|
|
|
|
|
|
|
- wrapper.eq(IotServerInfo::getEnable,0);
|
|
|
|
|
- wrapper.eq(IotServerInfo::getRegisterCodeStatus,1);
|
|
|
|
|
|
|
+ wrapper.eq(IotServerInfo::getEnable, 0);
|
|
|
|
|
+ wrapper.eq(IotServerInfo::getRegisterCodeStatus, 1);
|
|
|
wrapper.eq(IotServerInfo::getIotStatus, IotServerConnectStatus.CONNECTED.getIdx());
|
|
wrapper.eq(IotServerInfo::getIotStatus, IotServerConnectStatus.CONNECTED.getIdx());
|
|
|
- wrapper.in(ObjectUtil.isNotEmpty(orgIds),IotServerInfo::getOrgId);
|
|
|
|
|
- wrapper.select(IotServerInfo::getIotCode,IotServerInfo::getOrgId);
|
|
|
|
|
|
|
+ wrapper.in(ObjectUtil.isNotEmpty(orgIds), IotServerInfo::getOrgId);
|
|
|
|
|
+ wrapper.select(IotServerInfo::getIotCode, IotServerInfo::getOrgId);
|
|
|
return baseMapper.selectList(wrapper);
|
|
return baseMapper.selectList(wrapper);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public void checkWsOnLine() {
|
|
|
|
|
+ List<Instance> instanceList = new ArrayList<>();
|
|
|
|
|
+ try {
|
|
|
|
|
+ instanceList = namingService.getAllInstances(ServiceNameConstants.HOST);
|
|
|
|
|
+ } catch (NacosException e) {
|
|
|
|
|
+ LogUtils.SOCKET_CONNECT_STATUS_CHECK.error("获取实例信息时,nacos服务异常", e);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (CollectionUtil.isEmpty(instanceList)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ final List<String> onlineWs = getOnlineWs(instanceList);
|
|
|
|
|
+ updateWsOnlineStatus(onlineWs);
|
|
|
|
|
+ } catch (RuntimeException e) {
|
|
|
|
|
+ LogUtils.SOCKET_CONNECT_STATUS_CHECK.error("获取服务上WS链接状态时异常", e);
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<String> getOnlineWs(List<Instance> instanceList) throws RuntimeException {
|
|
|
|
|
+ //多线程远程调用获取服务状态
|
|
|
|
|
+ List<String> onlineWS = new ArrayList<>();
|
|
|
|
|
+ for (Instance instance : instanceList) {
|
|
|
|
|
+ List<String> list = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ list = restTemplate.getForObject(instance.getIp() + ":" + instance.getPort() + "/getOnlineWs", List.class);
|
|
|
|
|
+ LogUtils.SOCKET_CONNECT_STATUS_CHECK.error("获取当前系统在线的WS链接:{}", JacksonUtils.toJSONString(list));
|
|
|
|
|
+ } catch (RestClientException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (list == null) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ onlineWS.addAll(list);
|
|
|
|
|
+ }
|
|
|
|
|
+ return onlineWS;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private void updateWsOnlineStatus(List<String> onlineIotCode) {
|
|
|
|
|
+ if (ObjectUtil.isEmpty(onlineIotCode)) {
|
|
|
|
|
+ final LambdaUpdateWrapper<IotServerInfo> wrapper = new LambdaUpdateWrapper<IotServerInfo>();
|
|
|
|
|
+ wrapper.set(IotServerInfo::getIotStatus, IotServerConnectStatus.OFFLINE.getIdx());
|
|
|
|
|
+ wrapper.set(IotServerInfo::getUpdateTime, new Date());
|
|
|
|
|
+ wrapper.set(IotServerInfo::getLastConnectTime, new Date());
|
|
|
|
|
+ baseMapper.update(null, wrapper);
|
|
|
|
|
+ LogUtils.SOCKET_CONNECT_STATUS_CHECK.info("检测WS链接状态时,发现没有在线的链接,修改所有链接为离线");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ //获取当前在线的WS
|
|
|
|
|
+ final List<IotServerInfo> infoList = selectOnlineServer(null);
|
|
|
|
|
+ //获取唯一标识
|
|
|
|
|
+ final List<String> collect = infoList.stream().map(IotServerInfo::getIotCode).collect(Collectors.toList());
|
|
|
|
|
+ //如果数据库中标识为当前在线的WS链接 并不存在于从所有实例汇总到的在线的WS链接中 则更新为离线
|
|
|
|
|
+ final List<String> offlineIotCode = collect.stream().filter(item -> !onlineIotCode.contains(item)).collect(Collectors.toList());
|
|
|
|
|
+ if (ObjectUtil.isNotEmpty(offlineIotCode)) {
|
|
|
|
|
+ final LambdaUpdateWrapper<IotServerInfo> wrapper = new LambdaUpdateWrapper<IotServerInfo>();
|
|
|
|
|
+ wrapper.in(IotServerInfo::getIotCode, offlineIotCode);
|
|
|
|
|
+ wrapper.set(IotServerInfo::getIotStatus, IotServerConnectStatus.OFFLINE.getIdx());
|
|
|
|
|
+ wrapper.set(IotServerInfo::getUpdateTime, new Date());
|
|
|
|
|
+ wrapper.set(IotServerInfo::getLastConnectTime, new Date());
|
|
|
|
|
+ baseMapper.update(null, wrapper);
|
|
|
|
|
+ LogUtils.SOCKET_CONNECT_STATUS_CHECK.info("发现存在数据库与实际链接状态不一致的WS链接,已修改为离线:{}", offlineIotCode);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //将从所有实例汇总到的在线的WS链接更新检测时间
|
|
|
|
|
+ final LambdaUpdateWrapper<IotServerInfo> wrapper = new LambdaUpdateWrapper<IotServerInfo>();
|
|
|
|
|
+ wrapper.set(IotServerInfo::getIotStatus, IotServerConnectStatus.CONNECTED.getIdx());
|
|
|
|
|
+ wrapper.set(IotServerInfo::getUpdateTime, new Date());
|
|
|
|
|
+ wrapper.set(IotServerInfo::getLastConnectTime, new Date());
|
|
|
|
|
+ wrapper.in(IotServerInfo::getIotCode, onlineIotCode);
|
|
|
|
|
+ baseMapper.update(null, wrapper);
|
|
|
|
|
+ LogUtils.SOCKET_CONNECT_STATUS_CHECK.info("修改在线的WS连接的检测时间:{}", JacksonUtils.toJSONString(onlineIotCode));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|