pull/3/head
张腾 2024-09-28 12:30:20 +08:00
parent 49fe2961f0
commit b299c8d3a1
16 changed files with 755 additions and 0 deletions

View File

@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.muyu</groupId>
<artifactId>cloud-modules</artifactId>
<version>3.6.3</version>
</parent>
<artifactId>cloud-modules-carData</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringCloud Alibaba Sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- SpringBoot Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Mysql Connector -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- MuYu Common DataSource -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-common-datasource</artifactId>
</dependency>
<!-- MuYu Common DataScope -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-common-datascope</artifactId>
</dependency>
<!-- MuYu Common Log -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-common-log</artifactId>
</dependency>
<!-- 接口模块 -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-common-api-doc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-common-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>org.apache.iotdb</groupId>
<artifactId>iotdb-session</artifactId>
<version>0.13.1</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,25 @@
package com.muyu.carData;
import com.muyu.common.security.annotation.EnableMyFeignClients;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import javax.swing.*;
/**
* @Author
* @Packagecom.muyu.carData
* @Projectcloud-server-8
* @nameCarDataApplication
* @Date2024/9/26 15:33
*/
@SpringBootApplication
@EnableMyFeignClients
public class CarDataApplication {
public static void main(String[] args) {
SpringApplication.run(CarDataApplication.class,args);
System.out.println("caused: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer;;caused: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer;");
}
}

View File

@ -0,0 +1,18 @@
package com.muyu.carData.annotation;
import java.lang.annotation.*;
/**
* @Author
* @Packagecom.muyu.carData.annotation
* @Projectcloud-server-8
* @nameIoTTableName
* @Date2024/9/27 19:29
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
public @interface IoTTableName {
String value() default "";
}

View File

@ -0,0 +1,29 @@
package com.muyu.carData.config.cacheconfig;
import com.github.benmanes.caffeine.cache.Expiry;
import org.checkerframework.checker.index.qual.NonNegative;
import java.util.concurrent.TimeUnit;
/**
* @Author
* @Packagecom.muyu.carData.cacheconfig
* @Projectcloud-server-8
* @nameCacheExpiry
* @Date2024/9/26 23:46
*/
public class CacheExpiry implements Expiry<String,ExpiryTime>{
@Override
public long expireAfterCreate(String key, ExpiryTime value, long currentTime) {
return TimeUnit.SECONDS.toNanos(value.getExpiryTime());
}
@Override
public long expireAfterUpdate(String key, ExpiryTime value, long currentTime, @NonNegative long currentDuration) {
return TimeUnit.SECONDS.toNanos(value.getRefreshTime());
}
@Override
public long expireAfterRead(String key, ExpiryTime value, long currentTime, @NonNegative long currentDuration) {
return TimeUnit.SECONDS.toNanos(value.getRefreshTime());
}
}

View File

@ -0,0 +1,26 @@
package com.muyu.carData.config.cacheconfig;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author
* @Packagecom.muyu.carData.cacheconfig
* @Projectcloud-server-8
* @nameCaffeineConfig
* @Date2024/9/26 23:51
*/
@Configuration
public class CaffeineConfig {
@Bean
public Cache<String, ? extends ExpiryTime> caffeineCache(){
CacheExpiry cacheExpiry = new CacheExpiry();
return Caffeine.newBuilder()
.expireAfter(cacheExpiry)
.initialCapacity(128)
.build();
}
}

View File

@ -0,0 +1,33 @@
package com.muyu.carData.config.cacheconfig;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* @Author
* @Packagecom.muyu.carData.cacheconfig
* @Projectcloud-server-8
* @nameExpiryTime
* @Date2024/9/26 23:44
*/
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class ExpiryTime {
/**
* 30
*/
@Builder.Default
private long expiryTime = 30 * 60;
/**
* 15
*/
@Builder.Default
private long refreshTime = 15 * 60;
}

View File

@ -0,0 +1,24 @@
package com.muyu.carData.config.kafkaconfig;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
/**
* @Author
* @Packagecom.muyu.carData.config.kafkaconfig
* @Projectcloud-server-8
* @nameKafkaConfig
* @Date2024/9/28 12:19
*/
@Configuration
public class KafkaConfig {
@Bean
public KafkaProducer kafkaProducer(){
HashMap<String, Object> configs = new HashMap<>();
return null;
}
}

View File

@ -0,0 +1,282 @@
package com.muyu.carData.config.lotdbconfig;
import com.muyu.carData.domain.IoTDBRecord;
import com.muyu.carData.interfaces.IoTDBRecordable;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.session.SessionDataSet;
import org.apache.iotdb.session.util.Version;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @Author
* @Packagecom.muyu.carData.lotdbconfig
* @Projectcloud-server-8
* @nameIotDBSessionConfig
* @Date2024/9/27 12:25
* IotDB线
*/
@Component
@Configuration
public class IotDBSessionConfig {
private static Logger logger = LoggerFactory.getLogger(IotDBSessionConfig.class);
/**
*
*/
@Value("${spring.iotdb.username}")
private String username;
/**
*
*/
@Value("${spring.iotdb.password}")
private String password;
/**
* ip
*/
@Value("${spring.iotdb.ip}")
private String ip;
/**
*
*/
@Value("${spring.iotdb.port}")
private Integer port;
/**
*
*/
@Value("${spring.iotdb.maxSize}")
private Integer maxSize;
private static Session session;
/**
* session iotdb
* @return
*/
public Session getSession(){
if (null == session){
try {
logger.info("正在连接iotdb......");
session = new Session.Builder()
.host(ip)
.port(port)
.username(username)
.password(password)
.version(Version.V_0_13)
.build();
IotDBSessionConfig.session.open(false);
session.setFetchSize(maxSize);
//设置时区
session.setTimeZone("+08:00");
} catch (IoTDBConnectionException | StatementExecutionException e) {
logger.error(String.valueOf(e.getCause()));
}
}
return session;
}
/**
*
* @param records
* @return
* @throws Exception
*/
private List<String> getDeviceIds(List<? extends IoTDBRecordable> records) throws Exception {
List<String> deviceIds = new ArrayList<>();
for (IoTDBRecordable record : records) {
IoTDBRecord ioTDBRecord = record.toRecord();
String deviceId = ioTDBRecord.getDeviceId();
deviceIds.add(deviceId);
}
return deviceIds;
}
/**
*
* @param records
* @return
* @throws Exception
*/
private List<Long> getTimes(List<? extends IoTDBRecordable> records) throws Exception {
List<Long> times = new ArrayList<>();
for (IoTDBRecordable record : records) {
long time = record.toRecord().getTime();
times.add(time);
}
return times;
}
/**
*
* @param records
* @return
* @throws Exception
*/
private List<List<String>> getMeasurementsList(List<? extends IoTDBRecordable> records) throws Exception {
List<List<String>> measurementList = new ArrayList<>();
for (IoTDBRecordable record : records) {
List<String> iotDBRecord = record.toRecord().getMeasurementList();
measurementList.add(iotDBRecord);
}
return measurementList;
}
/**
*
* @param records
* @return
* @throws Exception
*/
public List<List<Object>> getValueList(List<? extends IoTDBRecordable> records) throws Exception {
List<List<Object>> valueList = new ArrayList<>();
for (IoTDBRecordable record : records) {
List<Object> iotDBRecord = record.toRecord().getValueList();
valueList.add(iotDBRecord);
}
return valueList;
}
/**
*
* @param records
* @return
* @throws Exception
*/
private List<List<TSDataType>> getTypeList(List<? extends IoTDBRecordable> records) throws Exception {
List<List<TSDataType>> typeList = new ArrayList<>();
for (IoTDBRecordable record : records) {
IoTDBRecord tdbRecord = record.toRecord();
List<TSDataType> strList = new ArrayList<>();
for (String str : tdbRecord.getTypeList()) {
strList.add(TSDataType.valueOf(str));
}
typeList.add(strList);
}
return typeList;
}
/**
*
* @param type
* @return
*/
private TSDataType convertTypeByEntity(String type){
switch (type){
case "java.lang.Double":
return TSDataType.DOUBLE;
case "java.lang.Integer":
return TSDataType.INT32;
case "java.lang.Long":
return TSDataType.INT64;
case "java.lang.Boolean":
return TSDataType.BOOLEAN;
case "java.lang.Float":
return TSDataType.FLOAT;
default:
return TSDataType.TEXT;
}
}
/**
*
* @param records
*/
public void insertRecords(List<? extends IoTDBRecordable> records){
try {
session.insertRecords(getDeviceIds(records),
getTimes(records),getMeasurementsList(records),getTypeList(records),getValueList(records));
} catch (Exception e) {
logger.error("IotDB批量插入异常{}",e.getMessage());
}
}
public void insertRecord(IoTDBRecordable ioTDBRecordable){
try {
IoTDBRecord tdbRecord = ioTDBRecordable.toRecord();
List<TSDataType> typeList = new ArrayList<>();
for (String str : tdbRecord.getTypeList()) {
typeList.add(TSDataType.valueOf(str));
}
session.insertRecord(tdbRecord.getDeviceId(), tdbRecord.getTime(), tdbRecord.getMeasurementList(), typeList, tdbRecord.getValueList());
} catch (Exception e) {
logger.error("IotDB插入异常{}",e.getMessage());
}
}
/**
* sql
* @param sql sql
* @return
*/
public SessionDataSet query(String sql) throws IoTDBConnectionException, StatementExecutionException {
return session.executeQueryStatement(sql);
}
/**
*
* @param groupName
* @throws IoTDBConnectionException
* @throws StatementExecutionException
*/
public void deleteStorageGroup(String groupName) throws IoTDBConnectionException, StatementExecutionException {
session.deleteStorageGroup(groupName);
}
/**
*
*/
public void deleteTimeSeries(String timeSeries) throws IoTDBConnectionException, StatementExecutionException {
session.deleteTimeseries(timeSeries);
}
/**
*
* @param timeSeriesList
*/
public void batchDeleteTimeSeries(List<String> timeSeriesList) throws IoTDBConnectionException, StatementExecutionException {
session.deleteTimeseries(timeSeriesList);
}
/**
*
*/
public void deleteStorageGroupList(List<String> storageGroupList) throws IoTDBConnectionException, StatementExecutionException {
session.deleteStorageGroups(storageGroupList);
}
/**
*
* @param path
* @param endTime
*/
public void deleteDataByPathAndEndTime(String path,Long endTime){
}
}

View File

@ -0,0 +1,13 @@
package com.muyu.carData.constract;
/**
* @Author
* @Packagecom.muyu.carData.constract
* @Projectcloud-server-8
* @nameIoTDBTableParam
* @Date2024/9/27 20:02
*/
public class IoTDBTableParam {
public static final String SYSLOG_IOT_TABLE = "student";
}

View File

@ -0,0 +1,17 @@
package com.muyu.carData.consumer;
import org.springframework.stereotype.Component;
/**
* @Author
* @Packagecom.muyu.carData.consumer
* @Projectcloud-server-8
* @nameMyKafkaConsumer
* @Date2024/9/26 15:42
*/
@Component
public class MyKafkaConsumer {
}

View File

@ -0,0 +1,44 @@
package com.muyu.carData.controller;
import com.github.benmanes.caffeine.cache.Cache;
import com.muyu.carData.config.cacheconfig.CaffeineConfig;
import com.muyu.carData.pojo.Student;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author
* @Packagecom.muyu.carData.controller
* @Projectcloud-server-8
* @nameTestController
* @Date2024/9/26 23:56
*/
@RestController
@RequestMapping("/testCache")
@Log4j2
public class TestController {
@Autowired
private CaffeineConfig caffeineConfig;
@RequestMapping("/caffeine")
public String caffeine() throws InterruptedException {
Cache<String, Student> stringCache = (Cache<String, Student>) caffeineConfig.caffeineCache();
Student build = Student.builder().id(1)
.name("小马")
.sex("男")
.expiryTime(20 * 60)
.refreshTime(15 * 30)
.build();
stringCache.put("1", build);
Thread.sleep(1000);
//返回缓存的个数
log.info(stringCache.estimatedSize());
//返回缓存的数据
log.info(stringCache.getIfPresent("1"));
return "111";
}
}

View File

@ -0,0 +1,47 @@
package com.muyu.carData.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* @Author
* @Packagecom.muyu.carData.domain
* @Projectcloud-server-8
* @nameIoTDBRecord
* @Date2024/9/27 19:25
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class IoTDBRecord {
/**
*
*/
private String deviceId;
/**
*
*/
private long time = System.currentTimeMillis();
/**
*
*/
private List<String> measurementList;
/**
*
*/
private List<Object> valueList;
/**
*
*/
private List<String> typeList;
}

View File

@ -0,0 +1,53 @@
package com.muyu.carData.interfaces;
import com.muyu.carData.annotation.IoTTableName;
import com.muyu.carData.domain.IoTDBRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
* @Author
* @Packagecom.muyu.carData.interfaces
* @Projectcloud-server-8
* @nameIoTDBRecordable
* @Date2024/9/27 19:22
* iot
*/
public interface IoTDBRecordable {
Logger logger = LoggerFactory.getLogger(IoTDBRecordable.class);
/**
*
* @return Record
*/
default IoTDBRecord toRecord() throws Exception {
IoTDBRecord ioTDBRecord = new IoTDBRecord();
Object getIoTDBTime = this.getClass().getMethod("getIoTDBTime").invoke(this);
if (null != getIoTDBTime){
ioTDBRecord.setTime((Long) getIoTDBTime);
}
Class aClass = this.getClass();
IoTTableName name = this.getClass().getAnnotation(IoTTableName.class);
ioTDBRecord.setDeviceId(name.value());
Field[] declaredFields = aClass.getDeclaredFields();
ArrayList<String> measurements = new ArrayList<>();
ArrayList<Object> records = new ArrayList<>();
ArrayList<String> types = new ArrayList<>();
for (Field declaredField : declaredFields) {
measurements.add(declaredField.getName());
String methodNamePro = declaredField.getName().substring(0, 1).toUpperCase() + declaredField.getName().substring(1);
Method methodName = this.getClass().getMethod("get" + methodNamePro);
records.add(methodName.invoke(this));
types.add(methodName.getReturnType().getName());
}
ioTDBRecord.setMeasurementList(measurements);
ioTDBRecord.setValueList(records);
ioTDBRecord.setTypeList(types);
return ioTDBRecord;
}
}

View File

@ -0,0 +1,45 @@
package com.muyu.carData.pojo;
import com.muyu.carData.annotation.IoTTableName;
import com.muyu.carData.config.cacheconfig.ExpiryTime;
import com.muyu.carData.constract.IoTDBTableParam;
import com.muyu.carData.interfaces.IoTDBRecordable;
import lombok.*;
import lombok.experimental.SuperBuilder;
/**
* @Author
* @Packagecom.muyu.carData.pojo
* @Projectcloud-server-8
* @nameStudent
* @Date2024/9/27 0:40
*/
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@IoTTableName(value = IoTDBTableParam.SYSLOG_IOT_TABLE)
public class Student extends ExpiryTime implements IoTDBRecordable {
/**
*
*/
private Integer id;
/**
*
*/
private String name;
/**
*
*/
private String sex;
/**
*
*/
private long time = System.currentTimeMillis();
}

View File

@ -80,6 +80,7 @@
<groupId>com.muyu</groupId>
<artifactId>cloud-common-core</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -12,6 +12,7 @@
<module>cloud-modules-system</module>
<module>cloud-modules-gen</module>
<module>cloud-modules-file</module>
<module>cloud-modules-carData</module>
</modules>
<artifactId>cloud-modules</artifactId>