fix 完善负载中心 可以简单的进行测压
parent
fec04652bb
commit
c6b07df79b
|
@ -248,17 +248,16 @@ public class InitConnectWeight implements ApplicationRunner {
|
|||
.build();
|
||||
connectService.LoadCenterAdd(build);
|
||||
ArrayList<String> weightIpList = new ArrayList<>();
|
||||
redisTemplate.delete("ip");
|
||||
redisTemplate.delete("subscript");
|
||||
|
||||
for (int i = 0; i <= max; i++) {
|
||||
for (ConnectWeight connectWeight : connectWeightList) {
|
||||
redisTemplate.opsForList().rightPush("ip", String.valueOf(i));
|
||||
log.info("权重值:{}",connectWeight.getWeightValue());
|
||||
// log.info("权重值:{}",connectWeight.getWeightValue());
|
||||
if (connectWeight.getWeightValue() > i) {
|
||||
weightIpList.add(connectWeight.getCarServerIp());
|
||||
}else if (connectWeight.getWeightValue() == max ){
|
||||
weightIpList.add(connectWeight.getCarServerIp());
|
||||
log.info("轮询结果:{}",connectWeight.getCarServerIp());
|
||||
// log.info("轮询结果:{}",connectWeight.getCarServerIp());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -269,6 +268,7 @@ public class InitConnectWeight implements ApplicationRunner {
|
|||
for (String ip : weightIpList) {
|
||||
redisTemplate.opsForList().rightPush("ips",ip);
|
||||
}
|
||||
redisTemplate.opsForValue().set("subscript","0");
|
||||
// //每台服务器百分之八十
|
||||
// HashMap<String, Double> hashMap = new HashMap<>(){{
|
||||
// for (String s : ipList) {
|
||||
|
|
|
@ -10,6 +10,7 @@ import com.car.demos.loadenter.Node;
|
|||
import com.car.demos.loadenter.NodeReq;
|
||||
import com.car.demos.req.VehicleConnectionReq;
|
||||
import com.car.service.ConnectService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
@ -24,6 +25,7 @@ import java.util.List;
|
|||
* @author Yangle
|
||||
* Date 2024/5/28 21:46
|
||||
*/
|
||||
@Log4j2
|
||||
@RestController
|
||||
@RequestMapping("/connect")
|
||||
public class ConnectController {
|
||||
|
@ -32,7 +34,9 @@ public class ConnectController {
|
|||
private ConnectService connectService;
|
||||
@PostMapping("/getConnect")
|
||||
public Result<MqttServerModel>getConnect(@RequestBody VehicleConnectionReq vehicleConnectionReq){
|
||||
return connectService.getConnect(vehicleConnectionReq);
|
||||
Result<MqttServerModel> connect = connectService.getConnect(vehicleConnectionReq);
|
||||
log.info("车辆上线的返回值:{}",connect);
|
||||
return connect;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -25,4 +25,6 @@ public class MqttServerModel {
|
|||
* MQTT订阅主题
|
||||
*/
|
||||
private String topic;
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ import org.springframework.data.redis.core.StringRedisTemplate;
|
|||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* 业务实现层 ConnectImplImpl
|
||||
|
@ -31,24 +32,69 @@ public class ConnectServiceImpl implements ConnectService {
|
|||
private ConnerMapper connerMapper;
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
@Override
|
||||
public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) {
|
||||
if (redisTemplate.hasKey("Redis")) {
|
||||
Integer count = Integer.valueOf(redisTemplate.opsForValue().get("ip"));
|
||||
if (count == 100){
|
||||
redisTemplate.opsForValue().set("Redis",String.valueOf(0));
|
||||
}else {
|
||||
redisTemplate.opsForValue().set("Redis",String.valueOf(count + 1));
|
||||
// 创建一个ReentrantLock对象
|
||||
ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
if (redisTemplate.hasKey("subscript")) {
|
||||
lock.lock(); // 加锁
|
||||
try {
|
||||
Integer count = Integer.valueOf(redisTemplate.opsForValue().get("subscript"));
|
||||
if (count == 100) {
|
||||
redisTemplate.opsForValue().set("subscript", String.valueOf(0));
|
||||
} else {
|
||||
redisTemplate.opsForValue().set("subscript", String.valueOf(count + 1));
|
||||
}
|
||||
|
||||
String ip = redisTemplate.opsForList().index("ips", count);
|
||||
return Result.success(new MqttServerModel("tcp://" + ip + ":1883", "test1"));
|
||||
} finally {
|
||||
lock.unlock(); // 释放锁
|
||||
}
|
||||
} else {
|
||||
lock.lock(); // 加锁
|
||||
try {
|
||||
redisTemplate.opsForValue().set("subscript", String.valueOf(1));
|
||||
String ip = redisTemplate.opsForList().index("ips", 0);
|
||||
return Result.success(new MqttServerModel("tcp://" + ip + ":1883", "test1"));
|
||||
} finally {
|
||||
lock.unlock(); // 释放锁
|
||||
}
|
||||
|
||||
String ip = redisTemplate.opsForList().index("ips", count);
|
||||
return Result.success(new MqttServerModel("tcp://"+ip+":1883","test1"));
|
||||
}else {
|
||||
redisTemplate.opsForValue().set("count",String.valueOf(1));
|
||||
String ip = redisTemplate.opsForList().index("ips", 0);
|
||||
return Result.success(new MqttServerModel("tcp://"+ip+":1883","test1"));
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) {
|
||||
// // 检查Redis中连接次数"count"是否存在
|
||||
// if (redisTemplate.hasKey("count")){
|
||||
// // 获取当前连接次数
|
||||
// Integer count = Integer.valueOf(redisTemplate.opsForValue().get("count"));
|
||||
// // 当连接次数达到100时,重置连接次数为0
|
||||
// if (count == 100){
|
||||
// redisTemplate.opsForValue().set("count", String.valueOf(0));
|
||||
// }else{
|
||||
// // 连接次数未达到100,递增连接次数
|
||||
// redisTemplate.opsForValue().set("count", String.valueOf(count+1));
|
||||
// }
|
||||
// // 根据当前连接次数,获取对应的IP地址
|
||||
// String ip = redisTemplate.opsForList().index("ipList", count);
|
||||
// long timestamp = System.currentTimeMillis();
|
||||
// this.insert(new VinIp(vehicleConnectionReq.getVehicleVin(), ip,timestamp));
|
||||
// // 创建并返回Mqtt服务器模型
|
||||
// MqttServerModel mqttServerModel = new MqttServerModel("tcp://" + ip + ":1883", "test1");
|
||||
// return Result.success(mqttServerModel);
|
||||
// }else{
|
||||
// // "count"不存在时,初始化连接次数为1
|
||||
// redisTemplate.opsForValue().set("count", String.valueOf(1));
|
||||
// // 获取列表中的第一个IP地址
|
||||
// String s = redisTemplate.opsForList().index("ipList", 0);
|
||||
// long timestamp = System.currentTimeMillis();
|
||||
// this.insert(new VinIp(vehicleConnectionReq.getVehicleVin(), s,timestamp));
|
||||
// // 创建并返回第一个IP地址对应的Mqtt服务器模型
|
||||
// MqttServerModel test1 = new MqttServerModel("tcp://" + s + ":1883", "test1");
|
||||
// return Result.success(test1);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
/**
|
||||
* 添加负载中心
|
||||
|
|
Loading…
Reference in New Issue