feat 获取车辆cpu使用率 以及总数量
parent
79bac537c7
commit
7bff86090a
6
pom.xml
6
pom.xml
|
@ -156,6 +156,12 @@
|
|||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.13</version> <!-- 使用你需要的版本 -->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package com.car;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@MapperScan
|
||||
public class ZnCarApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -9,8 +9,17 @@ import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
|||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.car.demos.ConnectWeight;
|
||||
import com.car.demos.loadenter.Auth;
|
||||
import com.car.demos.loadenter.Content;
|
||||
import com.car.demos.loadenter.LoadEnterNumber;
|
||||
import com.car.service.impl.ConnectServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
|
@ -18,7 +27,7 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
|
@ -44,11 +53,14 @@ public class InitConnectWeight implements ApplicationRunner {
|
|||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
connectService.loadCenterDel();
|
||||
|
||||
ArrayList<ConnectWeight> connectWeightList = new ArrayList<>();
|
||||
|
||||
ArrayList<String> ipList = new ArrayList<>();
|
||||
|
||||
LoadEnterNumber loadEnterNumber = new LoadEnterNumber();
|
||||
|
||||
Client client = null;
|
||||
//获取阿里云客户端
|
||||
try {
|
||||
|
@ -94,15 +106,22 @@ public class InitConnectWeight implements ApplicationRunner {
|
|||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
}
|
||||
//网关收集节点
|
||||
int gatewayNum = 0;
|
||||
int gatewayNum = ipList.size();
|
||||
//数据解析结点数量
|
||||
int dataNum=0;
|
||||
//整体负载率
|
||||
String overallLoad="";
|
||||
//总车辆
|
||||
int connectEventSize =0;
|
||||
|
||||
//内存使用率
|
||||
String memoryUseRate = null;
|
||||
//cpu使用率
|
||||
String cpuUsage = null;
|
||||
|
||||
//遍历所有ip,获取每一个服务的连接数
|
||||
for (String ip : ipList) {
|
||||
//网关收集节点
|
||||
gatewayNum = ip.length();
|
||||
log.info("gatewayNum:{}", gatewayNum);
|
||||
//fluxMq连接
|
||||
String url = "http://" + ip + ":8080/public/login";
|
||||
|
@ -144,52 +163,84 @@ public class InitConnectWeight implements ApplicationRunner {
|
|||
log.info("响应是:{}", responseInfo.getBody());
|
||||
|
||||
JSONArray jsonArray = JSON.parseArray(responseInfo.getBody());
|
||||
|
||||
if (jsonArray.size() > 0) {
|
||||
JSONObject jsonObject = jsonArray.getJSONObject(0);
|
||||
// Integer connectSize = jsonObject.getJSONObject("mqttInfo").getJSONObject("runtimes").getInteger("mqtt.connect");
|
||||
Integer connectSize = Integer.valueOf(jsonObject.getJSONObject("mqttInfo").getString("connectSize"));
|
||||
|
||||
//获取内存使用率
|
||||
JSONObject flowInfo = jsonObject.getJSONObject("jvmInfo");
|
||||
//内存大小
|
||||
String heapMaxSize = flowInfo.getString("heap-max");
|
||||
//堆内存使用率
|
||||
String heapUsedSize = flowInfo.getString("heap-used");
|
||||
|
||||
// 定义单位到字节的转换系数
|
||||
final double GB_TO_BYTES = 1024 * 1024 * 1024; // 1 GB = 1024^3 bytes
|
||||
final double MB_TO_BYTES = 1024 * 1024; // 1 MB = 1024^2 bytes
|
||||
|
||||
// 解析字符串并转换为字节
|
||||
double maxHeapBytes = Double.parseDouble(heapMaxSize.substring(0, heapMaxSize.length() - 3)) * GB_TO_BYTES; // 去掉"GB"并转换
|
||||
double usedHeapBytes = Double.parseDouble(heapUsedSize.substring(0, heapUsedSize.length() - 3)) * MB_TO_BYTES; // 去掉"MB"并转换
|
||||
|
||||
// 计算使用率(百分比)
|
||||
double usagePercentage = (usedHeapBytes / maxHeapBytes) * 100;
|
||||
//内存率
|
||||
memoryUseRate=usagePercentage+"%";
|
||||
|
||||
//cpu
|
||||
JSONObject cpuInfo = jsonObject.getJSONObject("cpuInfo");
|
||||
String string = cpuInfo.getString("user");
|
||||
cpuUsage =string;
|
||||
|
||||
|
||||
//获取mqtt
|
||||
JSONObject mqttInfo = jsonObject.getJSONObject("mqttInfo");
|
||||
//连接数
|
||||
Integer connectSize = Integer.valueOf(mqttInfo.getString("connectSize"));
|
||||
//小车总连接数
|
||||
connectEventSize = Integer.valueOf(mqttInfo.getString("connectEventSize"));
|
||||
|
||||
connectWeightList.add(new ConnectWeight(ip,100-connectSize));
|
||||
//权重值
|
||||
Integer weightValue = 100 - connectSize;
|
||||
|
||||
log.info("链接数量:{}", connectSize);
|
||||
} else {
|
||||
log.info("得到的相应数据为null");
|
||||
}
|
||||
|
||||
}
|
||||
// Integer sum =0;
|
||||
// for (ConnectWeight connectWeight : connectWeightList) {
|
||||
// sum = sum + connectWeight.getWeightValue();
|
||||
// }
|
||||
// //获取车辆列表
|
||||
// String postList = "http://"+ip+":8080/mqtt/connection";
|
||||
//
|
||||
// int max = 0;
|
||||
// for (ConnectWeight connectWeight : connectWeightList) {
|
||||
// log.info("权重值:{}",connectWeight.getWeightValue());
|
||||
// Integer result = BigDecimal.valueOf(connectWeight.getWeightValue() * 100).divide(BigDecimal.valueOf(sum), 0, RoundingMode.DOWN).intValue();
|
||||
// if (result > max){
|
||||
// max = result;
|
||||
// }
|
||||
// connectWeight.setWeightValue(result);
|
||||
// log.info("100次轮询次数:{}",result);
|
||||
// }
|
||||
//
|
||||
// ArrayList<String> weightIpList = new ArrayList<>();
|
||||
//
|
||||
// //轮询出现次数
|
||||
// for (int i = 0; i <= max; i++) {
|
||||
// for (ConnectWeight connectWeight : connectWeightList) {
|
||||
// if (connectWeight.getWeightValue() > i){
|
||||
// weightIpList.add(connectWeight.getCarServerIp());
|
||||
// }else if (connectWeight.getWeightValue() == max ){
|
||||
// weightIpList.add(connectWeight.getCarServerIp());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// //存入redis
|
||||
// redisTemplate.delete("ips");
|
||||
// for (String ip : weightIpList) {
|
||||
// redisTemplate.opsForList().rightPush("ips",ip);
|
||||
// }
|
||||
// HttpClient closeableHttpClient = HttpClients.createDefault();
|
||||
// HttpPost httpPost = new HttpPost(postList);
|
||||
// JSONObject object = JSONObject.parseObject(result);
|
||||
// httpPost.addHeader("accessToken",object.getString("accessToken") ); // 设置请求头
|
||||
|
||||
// try {
|
||||
// HttpResponse response = closeableHttpClient.execute(httpPost);
|
||||
// Object o = restTemplate.postForObject(postList, response, Object.class);
|
||||
// log.info("响应是:{}", o);
|
||||
// JSONObject jsonObject = JSONObject.parseObject(response.toString());
|
||||
// JSONArray content = jsonObject.getJSONArray("content");
|
||||
// for (int i = 0; i < content.size(); i++) {
|
||||
// // 获取每个元素(JSONObject)
|
||||
// JSONObject contentItem = content.getJSONObject(i);
|
||||
// // 获取state字段的值
|
||||
// int state = contentItem.getIntValue("state");
|
||||
// if (state == 1) {
|
||||
// // 如果state为1,则将当前对象添加到列表中
|
||||
// filteredContent.add(contentItem);
|
||||
// }
|
||||
// }
|
||||
// log.info("车辆列表:{}", response);
|
||||
// } catch (IOException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
|
||||
}
|
||||
log.info("车辆在线是",connectEventSize);
|
||||
|
||||
//每个连接数的权重总和
|
||||
Integer sum =0;
|
||||
|
@ -199,15 +250,27 @@ public class InitConnectWeight implements ApplicationRunner {
|
|||
System.out.println("sum"+sum);
|
||||
|
||||
int max=0;
|
||||
//权重总和
|
||||
double sumOfWeights=0;
|
||||
for (ConnectWeight connectWeight : connectWeightList) {
|
||||
Integer result = BigDecimal.valueOf(connectWeight.getWeightValue() * 100).divide(BigDecimal.valueOf(sum), 0, RoundingMode.DOWN).intValue();
|
||||
BigDecimal divide = BigDecimal.valueOf(connectWeight.getWeightValue() * 100).divide(BigDecimal.valueOf(sum), 0, RoundingMode.DOWN);
|
||||
int result = divide.intValue();
|
||||
if (result > max){
|
||||
max = result;
|
||||
}
|
||||
connectWeight.setWeightValue(result);
|
||||
System.out.println("100次轮询次数:{}"+result);
|
||||
sumOfWeights+=connectWeight.getWeightValue();
|
||||
}
|
||||
|
||||
overallLoad =sumOfWeights/ipList.size()+"%";
|
||||
log.info("总负载",overallLoad);
|
||||
LoadEnterNumber build = LoadEnterNumber.builder()
|
||||
.carSum(connectEventSize)
|
||||
.gatewayNum(ipList.size())
|
||||
.dataNum(dataNum)
|
||||
.overallLoad(overallLoad)
|
||||
.build();
|
||||
connectService.LoadCenterAdd(build);
|
||||
ArrayList<String> weightIpList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i <= max; i++) {
|
||||
|
@ -223,13 +286,25 @@ public class InitConnectWeight implements ApplicationRunner {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 存入redis
|
||||
redisTemplate.delete("ips");
|
||||
for (String ip : weightIpList) {
|
||||
redisTemplate.opsForList().rightPush("ips",ip);
|
||||
}
|
||||
|
||||
// List<Content> contents = new ArrayList<>();
|
||||
// for (JSONObject jsonObject : filteredContent) {
|
||||
// Content build = Content.builder()
|
||||
// .cleanSession(jsonObject.getInteger("cleanSession"))
|
||||
// .id(jsonObject.getInteger("id"))
|
||||
// .clientPort(jsonObject.getInteger("clientPort"))
|
||||
// .protocolType(jsonObject.getString("protocolType"))
|
||||
// .version(jsonObject.getString("version"))
|
||||
// .state(jsonObject.getInteger("state"))
|
||||
// .connectTime(jsonObject.getDate("connectTime"))
|
||||
// .auth(jsonObject.getJSONArray("auth").toJavaList(Auth.class))
|
||||
// .build();
|
||||
// contents.add(build);
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.car.controller;
|
|||
|
||||
import com.car.demos.MqttServerModel;
|
||||
import com.car.demos.Result;
|
||||
import com.car.demos.loadenter.LoadEnterNumber;
|
||||
import com.car.demos.req.VehicleConnectionReq;
|
||||
import com.car.service.ConnectService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -26,4 +27,24 @@ public class ConnectController {
|
|||
public Result<MqttServerModel>getConnect(@RequestBody VehicleConnectionReq vehicleConnectionReq){
|
||||
return connectService.getConnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加负载中心总条数
|
||||
* @param loadEnterNumber
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/LoadCenterAdd")
|
||||
public Result LoadCenterAdd(@RequestBody LoadEnterNumber loadEnterNumber){
|
||||
connectService.LoadCenterAdd(loadEnterNumber);
|
||||
return Result.success("添加成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除负载中心总条数
|
||||
*/
|
||||
@PostMapping("/loadCenterDel")
|
||||
public Result loadCenterDel(){
|
||||
connectService.loadCenterDel();
|
||||
return Result.success("删除成功");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.car.demos.loadenter;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* 用户信息 Auth
|
||||
*
|
||||
* @author Yangle
|
||||
* Date 2024/6/2 11:45
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@SuperBuilder
|
||||
public class Auth {
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 用户密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.car.demos.loadenter;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 车辆详细信息 Content
|
||||
*
|
||||
* @author Yangle
|
||||
* Date 2024/6/2 11:32
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@SuperBuilder
|
||||
public class Content {
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 客户端id
|
||||
*/
|
||||
private Integer clientId;
|
||||
/*
|
||||
客户端地址
|
||||
*/
|
||||
private String clientIp;
|
||||
|
||||
private Integer clientPort;
|
||||
private String protocolType;
|
||||
private String nodeIp;
|
||||
private String version;
|
||||
private Integer keepalive;
|
||||
private Integer cleanSession;
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private Integer state;
|
||||
/**
|
||||
* 连接时间
|
||||
*/
|
||||
private Date connectTime;
|
||||
|
||||
private List<Auth> auth;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.car.demos.loadenter;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 负载中心数据总数量展示 loadEnterNumber
|
||||
*
|
||||
* @author Yangle
|
||||
* Date 2024/6/2 11:29
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@SuperBuilder
|
||||
public class LoadEnterNumber {
|
||||
|
||||
/*
|
||||
主键
|
||||
*/
|
||||
private Integer id;
|
||||
/*
|
||||
网关收集节点数量
|
||||
*/
|
||||
private Integer gatewayNum;
|
||||
/*
|
||||
数据解析节点数量
|
||||
*/
|
||||
private Integer dataNum;
|
||||
/*
|
||||
整体负载率
|
||||
*/
|
||||
private String overallLoad;
|
||||
/*
|
||||
车辆在线数量
|
||||
*/
|
||||
private Integer carSum;
|
||||
//
|
||||
// /**
|
||||
// * 车辆对象
|
||||
// */
|
||||
// private List<Content> contents;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.car.mapper;
|
||||
|
||||
import com.car.demos.loadenter.LoadEnterNumber;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* ConnerMapper
|
||||
*
|
||||
* @author Yangle
|
||||
* Date 2024/6/2 11:52
|
||||
*/
|
||||
@Mapper
|
||||
public interface ConnerMapper {
|
||||
void lodaCenterAdd(LoadEnterNumber loadEnterNumber);
|
||||
|
||||
void loadCenterDel();
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@ package com.car.service;
|
|||
|
||||
import com.car.demos.MqttServerModel;
|
||||
import com.car.demos.Result;
|
||||
import com.car.demos.loadenter.LoadEnterNumber;
|
||||
|
||||
/**
|
||||
* 车辆连接业务层 ConnectImpl
|
||||
|
@ -12,4 +13,8 @@ import com.car.demos.Result;
|
|||
public interface ConnectService {
|
||||
Result<MqttServerModel> getConnect();
|
||||
|
||||
void LoadCenterAdd(LoadEnterNumber loadEnterNumber);
|
||||
|
||||
void loadCenterDel();
|
||||
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@ import com.aliyun.teaopenapi.models.Config;
|
|||
import com.car.demos.MqttServerModel;
|
||||
import com.car.demos.Result;
|
||||
|
||||
import com.car.demos.loadenter.LoadEnterNumber;
|
||||
import com.car.mapper.ConnerMapper;
|
||||
import com.car.service.ConnectService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
@ -18,8 +20,8 @@ import org.springframework.stereotype.Service;
|
|||
*/
|
||||
@Service
|
||||
public class ConnectServiceImpl implements ConnectService {
|
||||
// @Autowired
|
||||
// private ConnerMapper connerMapper;
|
||||
@Autowired
|
||||
private ConnerMapper connerMapper;
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
@Override
|
||||
|
@ -42,6 +44,23 @@ public class ConnectServiceImpl implements ConnectService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加负载中心
|
||||
* @param loadEnterNumber
|
||||
*/
|
||||
@Override
|
||||
public void LoadCenterAdd(LoadEnterNumber loadEnterNumber) {
|
||||
connerMapper.lodaCenterAdd(loadEnterNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除负载中心
|
||||
*/
|
||||
@Override
|
||||
public void loadCenterDel() {
|
||||
connerMapper.loadCenterDel();
|
||||
}
|
||||
|
||||
|
||||
public static Client createClient() throws Exception {
|
||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
|
|
|
@ -14,7 +14,7 @@ spring:
|
|||
matching-strategy: ant_path_matcher
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://115.159.211.196:3306/data_basete?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
|
||||
url: jdbc:mysql://115.159.211.196:3306/zncar?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
|
||||
username: root
|
||||
password: yl@123
|
||||
druid:
|
||||
|
@ -48,13 +48,13 @@ spring:
|
|||
port: 6379
|
||||
password: yl030509
|
||||
|
||||
## mybatis
|
||||
#mybatis:
|
||||
# configuration:
|
||||
# map-underscore-to-camel-case: true
|
||||
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
# mapper-locations: classpath*:mapper/*Mapper.xml
|
||||
# global-config:
|
||||
# db-config:
|
||||
# id-type: auto
|
||||
# mybatis
|
||||
mybatis:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
mapper-locations: classpath*:mapper/*Mapper.xml
|
||||
global-config:
|
||||
db-config:
|
||||
id-type: auto
|
||||
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
<?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.car.mapper.ConnerMapper">
|
||||
|
||||
<insert id="lodaCenterAdd">
|
||||
INSERT INTO `zncar`.`load_enter_number`
|
||||
( `gateway_num`, `data_num`, `overall_load`, `car_sum`)
|
||||
VALUES ( #{gatewayNum}, #{dataNum}, #{overallLoad}, #{carSum});
|
||||
</insert>
|
||||
<delete id="loadCenterDel">
|
||||
DELETE FROM `zncar`.`load_enter_number`
|
||||
</delete>
|
||||
</mapper>
|
Loading…
Reference in New Issue