宣城新版位置汇聚修改

ds-xuancheng
luyya 2025-04-27 09:46:59 +08:00
parent 86666b8a04
commit 65a6e03c1d
11 changed files with 319 additions and 21 deletions

View File

@ -49,10 +49,15 @@ import static org.apache.dubbo.metadata.report.support.Constants.DEFAULT_METADAT
public class RedisMetadataReport extends AbstractMetadataReport {
private static final String REDIS_DATABASE_KEY = "database";
private static final String SENTINEL_KEY = "sentinel";
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(RedisMetadataReport.class);
// protected , for test
protected JedisPool pool;
protected JedisSentinelPool sentinelPool;
private Set<HostAndPort> jedisClusterNodes;
private int timeout;
private String password;
@ -75,6 +80,14 @@ public class RedisMetadataReport extends AbstractMetadataReport {
for (URL tmpUrl : urls) {
jedisClusterNodes.add(new HostAndPort(tmpUrl.getHost(), tmpUrl.getPort()));
}
} else if (url.getParameter(SENTINEL_KEY,false)) {
Set<String> sentinels = new HashSet<>();
List<URL> urls = url.getBackupUrls();
for (URL tmpUrl : urls) {
sentinels.add(tmpUrl.getHost()+":"+ tmpUrl.getPort());
}
int database = url.getParameter(REDIS_DATABASE_KEY, 0);
sentinelPool = new JedisSentinelPool("mymaster",sentinels ,new GenericObjectPoolConfig<>(), timeout, password, database);
} else {
int database = url.getParameter(REDIS_DATABASE_KEY, 0);
pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort(), timeout, password, database);
@ -128,11 +141,25 @@ public class RedisMetadataReport extends AbstractMetadataReport {
private void storeMetadata(BaseMetadataIdentifier metadataIdentifier, String v) {
if (pool != null) {
storeMetadataStandalone(metadataIdentifier, v);
}else if(sentinelPool != null) {
storeMetadataInSentinel(metadataIdentifier, v);
} else {
storeMetadataInCluster(metadataIdentifier, v);
}
}
private void storeMetadataInSentinel(BaseMetadataIdentifier metadataIdentifier, String v) {
try (Jedis jedisSentinel = sentinelPool.getResource()) {
jedisSentinel.set(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), v, jedisParams);
} catch (Throwable e) {
String msg =
"Failed to put " + metadataIdentifier + " to redis cluster " + v + ", cause: " + e.getMessage();
logger.error(TRANSPORT_FAILED_RESPONSE, "", "", msg, e);
throw new RpcException(msg, e);
}
}
private void storeMetadataInCluster(BaseMetadataIdentifier metadataIdentifier, String v) {
try (JedisCluster jedisCluster =
new JedisCluster(jedisClusterNodes, timeout, timeout, 2, password, new GenericObjectPoolConfig<>())) {
@ -158,11 +185,24 @@ public class RedisMetadataReport extends AbstractMetadataReport {
private void deleteMetadata(BaseMetadataIdentifier metadataIdentifier) {
if (pool != null) {
deleteMetadataStandalone(metadataIdentifier);
}else if(sentinelPool != null) {
deleteMetadataSentinel(metadataIdentifier);
} else {
deleteMetadataInCluster(metadataIdentifier);
}
}
private void deleteMetadataSentinel(BaseMetadataIdentifier metadataIdentifier) {
try (Jedis jedisSentinel = sentinelPool.getResource()) {
jedisSentinel.del(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY));
} catch (Throwable e) {
String msg = "Failed to delete " + metadataIdentifier + " from redis , cause: " + e.getMessage();
logger.error(TRANSPORT_FAILED_RESPONSE, "", "", msg, e);
throw new RpcException(msg, e);
}
}
private void deleteMetadataInCluster(BaseMetadataIdentifier metadataIdentifier) {
try (JedisCluster jedisCluster =
new JedisCluster(jedisClusterNodes, timeout, timeout, 2, password, new GenericObjectPoolConfig<>())) {
@ -187,11 +227,24 @@ public class RedisMetadataReport extends AbstractMetadataReport {
private String getMetadata(BaseMetadataIdentifier metadataIdentifier) {
if (pool != null) {
return getMetadataStandalone(metadataIdentifier);
}else if(sentinelPool != null) {
return getMetadataSentinel(metadataIdentifier);
} else {
return getMetadataInCluster(metadataIdentifier);
}
}
private String getMetadataSentinel(BaseMetadataIdentifier metadataIdentifier) {
try (Jedis jedisSentinel = sentinelPool.getResource()) {
return jedisSentinel.get(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY));
} catch (Throwable e) {
String msg = "Failed to get " + metadataIdentifier + " from redis , cause: " + e.getMessage();
logger.error(TRANSPORT_FAILED_RESPONSE, "", "", msg, e);
throw new RpcException(msg, e);
}
}
private String getMetadataInCluster(BaseMetadataIdentifier metadataIdentifier) {
try (JedisCluster jedisCluster =
new JedisCluster(jedisClusterNodes, timeout, timeout, 2, password, new GenericObjectPoolConfig<>())) {
@ -243,6 +296,8 @@ public class RedisMetadataReport extends AbstractMetadataReport {
private boolean storeMapping(String key, String field, String value, String ticket) {
if (pool != null) {
return storeMappingStandalone(key, field, value, ticket);
}else if(sentinelPool != null) {
return storeMappingSentinel(key, field, value, ticket);
} else {
return storeMappingInCluster(key, field, value, ticket);
}
@ -278,6 +333,33 @@ public class RedisMetadataReport extends AbstractMetadataReport {
return false;
}
/**
* use 'watch' to implement cas.
* Find information about slot distribution by key.
*/
private boolean storeMappingSentinel(String key, String field, String value, String ticket) {
try (Jedis jedisSentinel = sentinelPool.getResource()) {
jedisSentinel.watch(key);
String oldValue = jedisSentinel.hget(key, field);
if (null == oldValue || null == ticket || oldValue.equals(ticket)) {
Transaction transaction = jedisSentinel.multi();
transaction.hset(key, field, value);
List<Object> result = transaction.exec();
if (null != result) {
jedisSentinel.publish(buildPubSubKey(), field);
return true;
}
}
jedisSentinel.unwatch();
} catch (Throwable e) {
String msg = "Failed to put " + key + ":" + field + " to redis " + value + ", cause: " + e.getMessage();
logger.error(TRANSPORT_FAILED_RESPONSE, "", "", msg, e);
throw new RpcException(msg, e);
}
return false;
}
/**
* use 'watch' to implement cas.
* Find information about slot distribution by key.
@ -339,6 +421,8 @@ public class RedisMetadataReport extends AbstractMetadataReport {
private String getMappingData(String key, String field) {
if (pool != null) {
return getMappingDataStandalone(key, field);
}else if(sentinelPool != null) {
return getMappingDataSentinel(key, field);
} else {
return getMappingDataInCluster(key, field);
}
@ -355,6 +439,17 @@ public class RedisMetadataReport extends AbstractMetadataReport {
}
}
private String getMappingDataSentinel(String key, String field) {
try (Jedis jedisSentinel = sentinelPool.getResource()) {
return jedisSentinel.hget(key, field);
} catch (Throwable e) {
String msg = "Failed to get " + key + ":" + field + " from redis , cause: " + e.getMessage();
logger.error(TRANSPORT_FAILED_RESPONSE, "", "", msg, e);
throw new RpcException(msg, e);
}
}
private String getMappingDataStandalone(String key, String field) {
try (Jedis jedis = pool.getResource()) {
return jedis.hget(key, field);
@ -502,6 +597,14 @@ public class RedisMetadataReport extends AbstractMetadataReport {
logger.error(TRANSPORT_FAILED_RESPONSE, "", "", msg, e);
throw new RpcException(msg, e);
}
} else if (sentinelPool != null) {
try (Jedis jedisSentinel = sentinelPool.getResource()) {
jedisSentinel.subscribe(notifySub, path);
} catch (Throwable e) {
String msg = "Failed to subscribe " + path + ", cause: " + e.getMessage();
logger.error(TRANSPORT_FAILED_RESPONSE, "", "", msg, e);
throw new RpcException(msg, e);
}
} else {
try (JedisCluster jedisCluster = new JedisCluster(
jedisClusterNodes, timeout, timeout, 2, password, new GenericObjectPoolConfig<>())) {

View File

@ -23,15 +23,27 @@ dubbo:
address: redis://${spring.data.redis.host}:${spring.data.redis.port}
group: DUBBO_GROUP
username: dubbo
password: ${spring.data.redis.password}
password: ruoyi123
# 集群开关
cluster: false
sentinel: false
parameters:
namespace: ${spring.profiles.active}
database: ${spring.data.redis.database}
timeout: ${spring.data.redis.timeout}
# 集群地址 cluster 为 true 生效
backup: 127.0.0.1:6379,127.0.0.1:6381
backup: 10.129.128.116:26380,10.129.128.115:26380,10.129.128.114:26380
# metadata-report:
# address: redis://${spring.data.redis.host}:${spring.data.redis.port}
# group: DUBBO_GROUP
# username: dubbo
# password: ${spring.data.redis.password}
# # 集群开关
# cluster: false
# parameters:
# namespace: ${spring.profiles.active}
# database: ${spring.data.redis.database}
# timeout: ${spring.data.redis.timeout}
# # 集群地址 cluster 为 true 生效
# backup: 127.0.0.1:6379,127.0.0.1:6381
# 消费者相关配置
consumer:
# 结果缓存(LRU算法)
@ -43,3 +55,12 @@ dubbo:
retries: 0
# 初始化检查
check: false
logging:
level:
# 设置 Dubbo 核心包的日志级别为 DEBUG
org.apache.dubbo: DEBUG
# 如果需要更细粒度的调试,可指定元数据报告模块
org.apache.dubbo.metadata: DEBUG
# Redis 客户端日志(可选)
io.lettuce.core: WARN # 避免 Redis 连接日志过多

View File

@ -25,6 +25,7 @@ import org.springframework.core.task.VirtualThreadTaskExecutor;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.TimeZone;
/**
@ -94,6 +95,22 @@ public class RedisConfiguration {
.setReadMode(clusterServersConfig.getReadMode())
.setSubscriptionMode(clusterServersConfig.getSubscriptionMode());
}
// 哨兵模式
RedissonProperties.Sentinel sentinel = redissonProperties.getSentinel();
if (Objects.nonNull(sentinel)) {
config.useSentinelServers()
.setNameMapper(new KeyPrefixHandler(redissonProperties.getKeyPrefix()))
.setTimeout(sentinel.getTimeout())
.setClientName(sentinel.getClientName())
.setIdleConnectionTimeout(sentinel.getIdleConnectionTimeout())
.setSubscriptionConnectionPoolSize(sentinel.getSubscriptionConnectionPoolSize())
.setMasterConnectionMinimumIdleSize(sentinel.getMasterConnectionMinimumIdleSize())
.setMasterConnectionPoolSize(sentinel.getMasterConnectionPoolSize())
.setSlaveConnectionMinimumIdleSize(sentinel.getSlaveConnectionMinimumIdleSize())
.setSlaveConnectionPoolSize(sentinel.getSlaveConnectionPoolSize())
.setReadMode(sentinel.getReadMode())
.setSubscriptionMode(sentinel.getSubscriptionMode());
}
log.info("初始化 redis 配置");
};
}

View File

@ -40,6 +40,8 @@ public class RedissonProperties {
*/
private ClusterServersConfig clusterServersConfig;
private Sentinel sentinel;
@Data
@NoArgsConstructor
public static class SingleServerConfig {
@ -132,4 +134,60 @@ public class RedissonProperties {
}
@Data
@NoArgsConstructor
public static class Sentinel {
/**
*
*/
private String clientName;
/**
* master
*/
private int masterConnectionMinimumIdleSize;
/**
* master
*/
private int masterConnectionPoolSize;
/**
* slave
*/
private int slaveConnectionMinimumIdleSize;
/**
* slave
*/
private int slaveConnectionPoolSize;
/**
*
*/
private int idleConnectionTimeout;
/**
*
*/
private int timeout;
/**
*
*/
private int subscriptionConnectionPoolSize;
/**
*
*/
private ReadMode readMode;
/**
*
*/
private SubscriptionMode subscriptionMode;
}
}

View File

@ -1,25 +1,20 @@
package org.dromara.system.controller.system;
import cn.hutool.core.date.DateUtil;
import jdk.dynalink.linker.LinkerServices;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.DeviceRedis;
import org.dromara.system.domain.bo.SysDeptBo;
import org.dromara.system.domain.bo.TDeviceBo;
import org.dromara.system.domain.vo.DeviceStaticsVo;
import org.dromara.system.domain.vo.SysDeptVo;
import org.dromara.system.domain.vo.SysDictDataVo;
import org.dromara.system.domain.vo.*;
import org.dromara.system.service.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@RequiredArgsConstructor
@RestController
@ -34,14 +29,50 @@ public class IndexStaticsController extends BaseController {
private final IDeviceRedisService redisService;
/*
* 线
* 线
* */
@PostMapping("/onlineCount")
public R onlineCount(TDeviceBo deviceInfo){
List<TDeviceVo> list = deviceService.queryList(deviceInfo);
String keys = "online_users:";
//List<String> strs = redisUtil.match(keys);
Date date = new Date();
Integer count = 0;
return R.ok(count);
}
/*
* 线
* */
@GetMapping("/topPan")
public R topPan(String zzjgdm){
public R topPan(){
DeviceRedis redis = new DeviceRedis();
redis.setZzjgdm(zzjgdm);
return R.ok(redisService.countByCondition(redis));
List<DeviceRedisVo> list = redisService.countByCondition(redis);
return R.ok(list);
}
/*
* Redis
* */
@RequestMapping("/serviceStatus")
public R serviceStatus(){
DeviceRedis redis = new DeviceRedis();
List<DeviceRedisVo> list = redisService.countByCondition(redis);
List<HashMap> maps = new ArrayList<>();
for (DeviceRedisVo redisVo : list) {
HashMap map = new HashMap();
Integer status = 1;
map.put("deviceType",redisVo.getDeviceType());
if (0 == redisVo.getOnline()){
status = 0;
}
map.put("status",status);
maps.add(map);
}
return R.ok(maps);
}
/*
@ -142,7 +173,47 @@ public class IndexStaticsController extends BaseController {
}
/*
*
* */
@PostMapping("/deviceTypePen")
public R deviceTypePen(TDeviceBo deviceInfo){
List<SysDictDataVo> dataList = dictTypeService.selectDictDataByType("zd_device_type");
List<HashMap> list = new ArrayList<>();
for (SysDictDataVo data : dataList) {
HashMap map = new HashMap();
deviceInfo.setDeviceType(data.getDictValue());
Long count = deviceService.countByCondition(deviceInfo);
map.put("name",data.getDictLabel());
map.put("value",count);
list.add(map);
}
return R.ok(list);
}
/*
*
* */
@PostMapping("/deviceBar")
public R deviceBar(TDeviceBo deviceInfo){
SysDeptBo dept = new SysDeptBo();
dept.setParentId(deviceInfo.getZzjgdm());
//dept.setIsStuck("1");
List<SysDeptVo> depts = deptService.selectDeptList(dept);
HashMap map = new HashMap();
List<String> names = new ArrayList<>();
List<Long> datas = new ArrayList<>();
for (SysDeptVo sysDept : depts) {
names.add(sysDept.getDeptName());
deviceInfo.setZzjgdm(sysDept.getDeptId());
Long count = deviceService.countByCondition(deviceInfo);
datas.add(count);
}
map.put("name",names);
map.put("data",datas);
return R.ok(map);
}
}

View File

@ -124,6 +124,12 @@ public class TDeviceController extends BaseController {
return toAjax(tDeviceService.updateByBo(bo));
}
@RequestMapping("/updateBatch")
public R<Void> updateBatch(@RequestBody TDeviceBo deviceInfo){
int num = tDeviceService.updateBatch(deviceInfo.getDeviceCode(),deviceInfo.getZzjgdm(),deviceInfo.getZzjgmc());
return toAjax(num);
}
/**
* device
*

View File

@ -14,7 +14,7 @@ public class DeviceRedisVo {
private String deviceType;
private String online;
// private String online;
private String zzjgdm;
@ -22,6 +22,6 @@ public class DeviceRedisVo {
private Integer co;
private Integer onlien;
private Integer online;
}

View File

@ -13,6 +13,7 @@ import org.dromara.system.domain.vo.TDeviceExportVo;
import org.dromara.system.domain.vo.TDeviceVo;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
import java.util.HashMap;
import java.util.List;
/**
@ -35,4 +36,6 @@ public interface TDeviceMapper extends BaseMapperPlus<TDevice, TDeviceVo> {
})
Page<TDeviceVo> selectPageDevicetList(@Param("page") Page<TDevice> page, @Param(Constants.WRAPPER) Wrapper<TDevice> queryWrapper);
int updateBatch(HashMap<String,Object> map);
}

View File

@ -1,5 +1,6 @@
package org.dromara.system.service.impl;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@ -190,6 +191,16 @@ public class TDeviceServiceImpl implements ITDeviceService {
return baseMapper.updateById(update) > 0;
}
@Override
public int updateBatch(String deviceCodes, String zzjgdm, String zzjgmc) {
String[] ids = Convert.toStrArray(deviceCodes);
HashMap<String,Object> map = new HashMap<>();
map.put("zzjgdm",zzjgdm);
map.put("deviceCode",ids);
map.put("zzjgmc",zzjgmc);
return baseMapper.updateBatch(map);
}
/**
*
*/

View File

@ -24,7 +24,7 @@
<!-- 全省各类设备总数、在线数 -->
<select id="countByCondition" resultMap="deviceRedisResult">
SELECT d.dict_label type_name,dict_value,COALESCE(r.co,0) online,COALESCE(td.co,0) co from sys_dict_data d
SELECT d.dict_label type_name,dict_value device_type,COALESCE(r.co,0) online,COALESCE(td.co,0) co from sys_dict_data d
LEFT JOIN (
SELECT device_type,count(*) co FROM (
SELECT * FROM t_device_redis

View File

@ -34,4 +34,12 @@
${ew.getCustomSqlSegment}
</select>
<update id="updateBatch" parameterType="map">
update t_device set zzjgdm = #{zzjgdm},zzjgmc = #{zzjgmc}
where device_code in
<foreach item="deviceCode" collection="deviceCode" open="(" separator="," close=")">
#{deviceCode}
</foreach>
</update>
</mapper>