package com.whyc.controller;
|
|
import com.alibaba.fastjson2.JSON;
|
import com.whyc.dto.Response;
|
import com.whyc.dto.interfaceB.DataB;
|
import com.whyc.dto.interfaceC.DataC;
|
import com.whyc.dto.interfaceC.PointC;
|
import com.whyc.pojo.InterfaceCData;
|
import com.whyc.service.ConfigService;
|
import com.whyc.service.DataService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.kafka.core.KafkaTemplate;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.annotation.Resource;
|
import java.util.LinkedList;
|
import java.util.List;
|
|
import static java.lang.Thread.sleep;
|
|
@RestController
|
@RequestMapping("dcim_north")
|
public class DcimNorthController{
|
|
@Resource
|
private KafkaTemplate template;
|
|
@Autowired
|
private ConfigService configService;
|
|
@Autowired
|
private DataService dataService;
|
|
/**
|
* 这个仅测试使用,正式环境不使用
|
* @param dataB
|
* @return
|
*/
|
@PostMapping("upload_config")
|
public Response uploadConfig(@RequestBody DataB dataB){
|
//发送到数据库
|
configService.add(dataB);
|
return new Response().set(1,"配置上传完成");
|
}
|
|
@PostMapping("push_data")
|
public Response pushData(@RequestBody InterfaceCData data){
|
//发送到数据库
|
dataService.add(data);
|
//发送到kafka
|
String jsonStr = JSON.toJSONString(data);
|
template.send("topic_device_data2",jsonStr);
|
return new Response().set(1,"推送完成");
|
}
|
|
@GetMapping("dataList")
|
public Response getDataList(){
|
//发送到数据库
|
return dataService.getDataList();
|
}
|
|
@GetMapping("benchmarkTest")
|
public void benchmarkTest(){
|
long currentTimeMillis = System.currentTimeMillis();
|
//生产80万条数据,然后发送
|
List<String> dataCList = new LinkedList<>();
|
for (int i = 0; i < 8000; i++) {
|
DataC dataC = new DataC();
|
dataC.setDevice_id(i+"");
|
dataC.setDevice_type("test");
|
dataC.setIdc("test");
|
|
List<PointC> pointCList = new LinkedList<>();
|
PointC pointC = new PointC();
|
pointC.setPoint_code(i);
|
pointC.setTag("test");
|
pointC.setTimestamp(currentTimeMillis);
|
pointC.setValue(Double.parseDouble(i+""));
|
pointCList.add(pointC);
|
dataC.setPoints(pointCList);
|
|
String dataCStr = JSON.toJSONString(dataC);
|
dataCList.add(dataCStr);
|
}
|
//long startTime = System.currentTimeMillis();
|
while (true) {
|
for (int i = 0; i < dataCList.size(); i++) {
|
String dataCStr = dataCList.get(i);
|
template.send("topic_device_data2", dataCStr);
|
/*.addCallback(
|
success->{
|
//System.out.println(success);
|
},
|
failure->{
|
//System.err.println(failure.getMessage());
|
//failure.printStackTrace();
|
});*/
|
}
|
long endTime = System.currentTimeMillis();
|
//System.out.println("花费时长(毫秒):" + (endTime - startTime));
|
try {
|
sleep(1000);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
|
}
|
|
|
}
|