Merge remote-tracking branch 'origin/main'

This commit is contained in:
2025-07-04 10:58:11 +08:00
23 changed files with 316 additions and 153 deletions

26
pom.xml
View File

@ -92,7 +92,6 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version> <version>2.3.0</version>
</dependency> </dependency>
<!-- Nacos 配置依赖(移除手动版本,由上方依赖管理控制) --> <!-- Nacos 配置依赖(移除手动版本,由上方依赖管理控制) -->
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
@ -103,7 +102,6 @@
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId> <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency> </dependency>
<!-- 测试依赖 --> <!-- 测试依赖 -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -137,8 +135,6 @@
<artifactId>mapstruct</artifactId> <artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version> <!-- 确保版本 ≥1.2.0 --> <version>1.5.5.Final</version> <!-- 确保版本 ≥1.2.0 -->
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
@ -206,19 +202,17 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.38</version> <version>1.18.38</version>
</path> </path>
</annotationProcessorPaths> <path>
</configuration> <groupId>org.mapstruct</groupId>
</plugin> <artifactId>mapstruct-processor</artifactId>
<plugin> <version>1.5.5.Final</version>
<groupId>org.springframework.boot</groupId> </path>
<artifactId>spring-boot-maven-plugin</artifactId> <path>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok-mapstruct-binding</artifactId>
</exclude> <version>0.2.0</version>
</excludes> </path>
</annotationProcessorPaths>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View File

@ -1,10 +1,12 @@
package com.bipt.intelligentapplicationorchestrationservice; package com.bipt.intelligentapplicationorchestrationservice;
import com.bipt.intelligentapplicationorchestrationservice.config.IpConfig;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
@ -12,6 +14,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication @SpringBootApplication
@EnableTransactionManagement @EnableTransactionManagement
@EnableDiscoveryClient @EnableDiscoveryClient
@EnableConfigurationProperties(IpConfig.class)
//@Slf4j //@Slf4j
public class IntelligentApplicationOrchestrationServiceApplication { public class IntelligentApplicationOrchestrationServiceApplication {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(IntelligentApplicationOrchestrationServiceApplication.class); private static final Logger log = org.slf4j.LoggerFactory.getLogger(IntelligentApplicationOrchestrationServiceApplication.class);

View File

@ -0,0 +1,21 @@
package com.bipt.intelligentapplicationorchestrationservice.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.List;
@Configuration
@ConfigurationProperties(prefix = "available")
public class IpConfig {
private List<String> ips;
public List<String> getIps() {
return ips;
}
public void setIps(String ips) {
this.ips = Arrays.asList(ips.split(","));
}
}

View File

@ -1,6 +1,9 @@
package com.bipt.intelligentapplicationorchestrationservice.config; package com.bipt.intelligentapplicationorchestrationservice.config;
import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.lettuce.core.ClientOptions; import io.lettuce.core.ClientOptions;
import io.lettuce.core.SocketOptions; import io.lettuce.core.SocketOptions;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -60,38 +63,41 @@ public class RedisConfig {
.clientOptions(clientOptions) // 注入 ClientOptions .clientOptions(clientOptions) // 注入 ClientOptions
.commandTimeout(Duration.ofSeconds(30)) // 全局命令超时 .commandTimeout(Duration.ofSeconds(30)) // 全局命令超时
.build(); .build();
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(redisHost); config.setHostName(redisHost);
config.setPort(redisPort); config.setPort(redisPort);
config.setUsername(redisUsername); // Redis 6.0+ 支持用户名 config.setUsername(redisUsername); // Redis 6.0+ 支持用户名
config.setPassword(RedisPassword.of(redisPassword)); config.setPassword(RedisPassword.of(redisPassword));
// LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
// .commandTimeout(Duration.ofSeconds(30)) // 增加命令超时
// .socketOptions(SocketOptions.builder()
// .connectTimeout(Duration.ofSeconds(15)) // TCP连接超时
// .build())
// .build();
return new LettuceConnectionFactory(config, clientConfig); return new LettuceConnectionFactory(config, clientConfig);
} }
// @Bean
// public RedisConnectionFactory redisConnectionFactory() {
// RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
// config.setPassword("");
// return new LettuceConnectionFactory(config);
// }
@Bean @Bean
public RedisTemplate<String, Object> redisTemplate(){ public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisTemplate<String, Object> template = new RedisTemplate<>();
Logger log = (Logger) LoggerFactory.getLogger(RedisConfig.class); Logger log = (Logger) LoggerFactory.getLogger(RedisConfig.class);
log.info("开始创建redis模板对象..."); log.info("开始创建redis模板对象...");
template.setConnectionFactory(redisConnectionFactory()); template.setConnectionFactory(redisConnectionFactory());
// 创建自定义的ObjectMapper并注册JavaTimeModule
ObjectMapper mapper = new ObjectMapper();
// 禁用将日期序列化为时间戳
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// 注册Java 8日期时间模块
mapper.registerModule(new JavaTimeModule());
// 使用自定义的ObjectMapper创建JSON序列化器
GenericJackson2JsonRedisSerializer jsonSerializer =
new GenericJackson2JsonRedisSerializer(mapper);
// 设置键和值的序列化方式
template.setKeySerializer(new StringRedisSerializer()); template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setValueSerializer(jsonSerializer);
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jsonSerializer);
template.afterPropertiesSet();
return template; return template;
} }
} }

View File

@ -9,11 +9,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping @RequestMapping("/gpu")
@CrossOrigin(origins = "http://localhost:3000")
public class GpuResourceController { public class GpuResourceController {
@Autowired @Autowired
private GpuManageService gpuManageService; private GpuManageService gpuManageService;
@PostMapping @PostMapping(value = "/add", produces = "application/json")
public ResponseVO addGpu(@Valid @RequestBody GpuCreateDTO dto){ public ResponseVO addGpu(@Valid @RequestBody GpuCreateDTO dto){
return gpuManageService.createGpuResource(dto); return gpuManageService.createGpuResource(dto);
} }

View File

@ -1,11 +1,16 @@
package com.bipt.intelligentapplicationorchestrationservice.controller; package com.bipt.intelligentapplicationorchestrationservice.controller;
import com.bipt.intelligentapplicationorchestrationservice.config.IpConfig;
import com.bipt.intelligentapplicationorchestrationservice.entity.DeployRequest;
import com.bipt.intelligentapplicationorchestrationservice.mapper.ModelMapper;
import com.bipt.intelligentapplicationorchestrationservice.pojo.*; import com.bipt.intelligentapplicationorchestrationservice.pojo.*;
import com.bipt.intelligentapplicationorchestrationservice.service.ModelDeployer;
import com.bipt.intelligentapplicationorchestrationservice.service.PublishService; import com.bipt.intelligentapplicationorchestrationservice.service.PublishService;
import com.bipt.intelligentapplicationorchestrationservice.util.NacosServiceUtil; import com.bipt.intelligentapplicationorchestrationservice.util.NacosServiceUtil;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -17,6 +22,8 @@ import java.util.List;
@RestController @RestController
@RequestMapping("/publish") @RequestMapping("/publish")
@Slf4j @Slf4j
@CrossOrigin(origins = "http://localhost:3000") // 生产环境指定具体域名
public class PublishController { public class PublishController {
@Autowired @Autowired
private PublishService publishService; private PublishService publishService;
@ -24,21 +31,35 @@ public class PublishController {
@Autowired @Autowired
private NacosServiceUtil nacosServiceUtil; private NacosServiceUtil nacosServiceUtil;
@Autowired
private ModelMapper modelMapper;
@Autowired
private IpConfig ipConfig;
@Autowired
private ModelDeployer modelDeployer;
@PostMapping @PostMapping
@Operation(summary ="新增发布请求") @Operation(summary ="新增发布请求")
@Transactional @Transactional
public OptResult<List<ServicePublishVO>> save(@RequestBody ServicePublishDTO servicePublishDTO) { public OptResult<List<ServicePublishVO>> save(@RequestBody ServicePublishDTO servicePublishDTO) {
log.info("模型发布请求:{}", servicePublishDTO); log.info("模型发布请求:{}", servicePublishDTO);
publishService.save(servicePublishDTO); publishService.save(servicePublishDTO);
//todo 调用模型部署 //调用模型部署
DeployRequest request = new DeployRequest();
Long modelId = servicePublishDTO.getModelId();
ModelVersion modelVersion = modelMapper.selectById(modelId);
String modelConfig = modelVersion.getModelConfig();
//假设modelConfig只存GPU数据
request.setModelId(String.valueOf(modelId));
request.setRequiredMemory(Integer.parseInt(modelConfig));
modelDeployer.deploy(request);
// 获取前端传来的IP字符串 // 获取前端传来的IP字符串
String ipListStr = servicePublishDTO.getIp(); String ipListStr = servicePublishDTO.getIp();
if (ipListStr == null || ipListStr.trim().isEmpty()) { if (ipListStr == null || ipListStr.trim().isEmpty()) {
log.warn("IP列表为空不进行Nacos注册"); log.warn("IP列表为空不进行Nacos注册");
return OptResult.success(); return OptResult.success();
} }
try { try {
// 使用逗号分割IP字符串 // 使用逗号分割IP字符串
String[] ipArray = ipListStr.split(","); String[] ipArray = ipListStr.split(",");
@ -59,15 +80,31 @@ public class PublishController {
log.error("Nacos服务注册失败", e); log.error("Nacos服务注册失败", e);
return OptResult.error("Nacos服务注册失败"); // 根据业务需求返回错误 return OptResult.error("Nacos服务注册失败"); // 根据业务需求返回错误
} }
return OptResult.success(); return OptResult.success();
} }
/**
* 获取已发布的服务列表
* @return
*/
@GetMapping("/list")
@Operation(summary ="获取已发布服务列表")
public OptResult<List<ServicePublishVO>> listPublishedServices() {
log.info("获取已发布服务列表接口被调用");
List<ServicePublishVO> services = publishService.listPublishedServices();
log.info("返回的数据: {}", services);
return OptResult.success(services);
}
/**
* 获取IP列表
* @return
*/
@GetMapping("/config/ips")
@Operation(summary = "获取可用IP地址列表")
public OptResult<List<String>> getAvailableIps() {
List<String> ips = ipConfig.getIps();
log.info("返回列表;{}",ips);
return OptResult.success(ips);
}
} }

View File

@ -1,12 +1,12 @@
package com.bipt.intelligentapplicationorchestrationservice.mapper; package com.bipt.intelligentapplicationorchestrationservice.entity;
import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuCreateDTO; import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuCreateDTO;
import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuResponseDTO; import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuResponseDTO;
import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuUpdateDTO; import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuUpdateDTO;
import com.bipt.intelligentapplicationorchestrationservice.entity.GpuResource;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants; import org.springframework.stereotype.Component;
@Component
@Mapper(componentModel = "spring") @Mapper(componentModel = "spring")
public interface GpuMapper { public interface GpuMapper {
GpuResource toEntity(GpuCreateDTO dto); GpuResource toEntity(GpuCreateDTO dto);

View File

@ -1,52 +1,21 @@
package com.bipt.intelligentapplicationorchestrationservice.entity; package com.bipt.intelligentapplicationorchestrationservice.entity;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data; import lombok.*;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@Setter
@Data @Data
@AllArgsConstructor
@Setter
@Getter
public class GpuResource { public class GpuResource {
@Getter
@TableField("GPUId") @TableField("GPUId")
private Long GPUId; private Long GPUId;
@Getter
@TableField("GPUModel") @TableField("GPUModel")
private String GPUModel; private String GPUModel;
public Integer getGPUMemorySize() {
return GPUMemorySize;
}
public Long getGPUId() {
return GPUId;
}
public String getGPUModel() {
return GPUModel;
}
public String getIp() {
return Ip;
}
public LocalDateTime getCreateTime() {
return CreateTime;
}
public LocalDateTime getUpdateTime() {
return UpdateTime;
}
public Integer getGPUMaxMemory() {
return GPUMaxMemory;
}
@Getter
@TableField("GPUMemorySize") @TableField("GPUMemorySize")
private Integer GPUMemorySize; private Integer GPUMemorySize;
@ -56,15 +25,12 @@ public class GpuResource {
@TableField("Ip") @TableField("Ip")
private String Ip; private String Ip;
@Getter @TableField("created_time")
@TableField("CreatedTime") private LocalDateTime createTime;
private LocalDateTime CreateTime;
@Getter
@TableField("update_time") @TableField("update_time")
private LocalDateTime UpdateTime; private LocalDateTime UpdateTime;
@Getter
@TableField("GPUMaxMemory") @TableField("GPUMaxMemory")
private Integer GPUMaxMemory; private Integer GPUMaxMemory;
@ -83,16 +49,40 @@ public class GpuResource {
public GpuResource(Long Id, String Model, Integer MemorySize, String ip, LocalDateTime create_time) { public GpuResource(Long Id, String GPUModel, Integer GPUMemorySize, String ip, LocalDateTime create_time) {
this.GPUId = Id; this.GPUId = Id;
this.GPUModel = Model; this.GPUModel = GPUModel;
this.GPUMemorySize = MemorySize; this.GPUMemorySize = GPUMemorySize;
this.Ip = ip; this.Ip = ip;
this.CreateTime = create_time; this.createTime = create_time;
} }
public GpuResource() {} public GpuResource() {}
public Integer getGPUMemorySize() {
return GPUMemorySize;
}
public Long getGPUId() {
return GPUId;
}
public String getGPUModel() {
return GPUModel;
}
public String getIp() {
return Ip;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public Integer getGPUMaxMemory() {
return GPUMaxMemory;
}
public void setGPUId(Long GPUId) { public void setGPUId(Long GPUId) {
this.GPUId = GPUId; this.GPUId = GPUId;
} }
@ -114,11 +104,7 @@ public class GpuResource {
} }
public void setCreateTime(LocalDateTime createTime) { public void setCreateTime(LocalDateTime createTime) {
CreateTime = createTime; this.createTime = createTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
UpdateTime = updateTime;
} }

View File

@ -12,7 +12,7 @@ import java.util.Map;
public interface GpuResourceDao { public interface GpuResourceDao {
//---------------------- 基础CRUD ------------------------ //---------------------- 基础CRUD ------------------------
@Insert("INSERT INTO Ipz.public.gpu_resource (GPUModel, GPUMemorySize, Ip) " + @Insert("INSERT INTO Ipz.public.gpu_resource (GPUModel, GPUMemorySize, Ip) " +
"VALUES (#{model}, #{memory}, #{ip})") "VALUES (#{GPUModel}, #{GPUMemorySize}, #{Ip})")
@Options(useGeneratedKeys = true, keyProperty = "GPUId") @Options(useGeneratedKeys = true, keyProperty = "GPUId")
Integer insert(GpuResource entity); Integer insert(GpuResource entity);
@ -27,7 +27,7 @@ public interface GpuResourceDao {
Integer isDeleted(@Param("gpuId") Long gpuId); Integer isDeleted(@Param("gpuId") Long gpuId);
@Update("UPDATE Ipz.public.gpu_resource " + @Update("UPDATE Ipz.public.gpu_resource " +
"SET GPUModel = #{model}, GPUMemorySize = #{memory}, Ip = #{ip} " + "SET GPUModel = #{GPUModel}, GPUMemorySize = #{GPUMemorySize}, Ip = #{Ip} " +
"WHERE GPUId = #{GPUId}") "WHERE GPUId = #{GPUId}")
Integer updateById(GpuResource entity); Integer updateById(GpuResource entity);

View File

@ -6,6 +6,8 @@ import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishVO
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper @Mapper
public interface PublishMapper { public interface PublishMapper {
@ -15,5 +17,6 @@ public interface PublishMapper {
Long getByApiUrl(String apiUrl); Long getByApiUrl(String apiUrl);
@Select("SELECT model_id,api_url,ip FROM service_publish")
List<ServicePublishVO> listPublishedServices();
} }

View File

@ -1,5 +1,6 @@
package com.bipt.intelligentapplicationorchestrationservice.pojo; package com.bipt.intelligentapplicationorchestrationservice.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern; import jakarta.validation.constraints.Pattern;
@ -14,14 +15,17 @@ public class GpuCreateDTO {
@NotBlank(message = "GPU型号不能为空") @NotBlank(message = "GPU型号不能为空")
@Pattern(regexp = "^([A-Z][A-Z0-9-]+)-\\w+", @Pattern(regexp = "^([A-Z][A-Z0-9-]+)-\\w+",
message = "型号格式应为 [厂商(大写字母开头)]-[型号],如 Intel-Xe_GPU") message = "型号格式应为 [厂商(大写字母开头)]-[型号],如 Intel-Xe_GPU")
@JsonProperty("GPUModel") // 显示指定JSON映射名称
private String GPUModel; private String GPUModel;
@NotNull(message = "显存容量不能为空") @NotNull(message = "显存容量不能为空")
@JsonProperty("GPUMemorySize")
private Integer GPUMemorySize; private Integer GPUMemorySize;
@NotBlank(message = "IP地址不能为空") @NotBlank(message = "IP地址不能为空")
@Pattern(regexp = "^\\\\d{1,3}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}$", @Pattern(regexp = "^(\\d{1,3}\\.){3}\\d{1,3}$",
message = "IP地址格式无效") message = "IP地址格式无效")
@JsonProperty("Ip") // 显示指定JSON映射名称
private String Ip; private String Ip;
} }

View File

@ -1,23 +1,30 @@
package com.bipt.intelligentapplicationorchestrationservice.pojo; package com.bipt.intelligentapplicationorchestrationservice.pojo;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Data; import lombok.Data;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@Data @Data
public class GpuResponseDTO { public class GpuResponseDTO {
private Long id; @JsonView
private Long GPUId;
private String GPUModel; private String GPUModel;
private Integer GPUMemorySize; private Integer GPUMemorySize;
private String Ip; private String Ip;
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime createTime; private LocalDateTime createTime;
// Builder类 // Builder类
public static class Builder { public static class Builder {
private Long id; private Long id;
private String model; private String model;
private Integer memory; private Integer memory;
private String ip; private String ip;
private LocalDateTime createdTime; private LocalDateTime createTime = LocalDateTime.now(); // 统一命名为createTime
public Builder id(Long id) { public Builder id(Long id) {
this.id = id; this.id = id;
@ -39,19 +46,29 @@ public class GpuResponseDTO {
return this; return this;
} }
public Builder createdTime(LocalDateTime createdTime) { public Builder createTime(LocalDateTime createTime) {
this.createdTime = createdTime; this.createTime = createTime;
return this; return this;
} }
public GpuResponseDTO build() { public GpuResponseDTO build() {
// 必填字段校验如网页2的推荐 // 必填字段校验
if (id == null) { if (id == null) {
throw new IllegalArgumentException("GPU ID必须填写"); throw new IllegalArgumentException("GPU ID必须填写");
} }
return new GpuResponseDTO();
GpuResponseDTO dto = new GpuResponseDTO();
dto.setGPUId(id);
dto.setGPUModel(model);
dto.setGPUMemorySize(memory);
dto.setIp(ip);
dto.setCreateTime(createTime); // 正确赋值createTime
return dto;
} }
} }
public String getCreateTimeStr(){
return "GPU创建时间" + createTime.toString(); public String getCreateTimeStr() {
return "GPU创建时间" + (createTime != null ? createTime.toString() : "未设置");
} }
} }

View File

@ -1,5 +1,6 @@
package com.bipt.intelligentapplicationorchestrationservice.pojo; package com.bipt.intelligentapplicationorchestrationservice.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern; import jakarta.validation.constraints.Pattern;
import lombok.*; import lombok.*;
@ -7,14 +8,36 @@ import lombok.*;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
//@Setter @Setter
//@Getter @Getter
public class GpuUpdateDTO { public class GpuUpdateDTO {
public @NotNull(message = "GPU ID cannot be null") Long getGPUId() {
return GPUId; private Long GPUId;
public @Pattern(regexp = "^([A-Z][A-Z0-9-]+)-\\w+",
message = "型号格式应为 [厂商(大写字母开头)]-[型号],如 Intel-Xe_GPU") String getGPUModel() {
return GPUModel;
} }
public void setGPUId(@NotNull(message = "GPU ID cannot be null") Long GPUId) { public @Pattern(regexp = "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$",
message = "IP地址格式无效") String getIp() {
return Ip;
}
@Pattern(regexp = "^([A-Z][A-Z0-9-]+)-\\w+",
message = "型号格式应为 [厂商(大写字母开头)]-[型号],如 Intel-Xe_GPU")
@JsonProperty("GPUModel") // 显示指定JSON映射名称
private String GPUModel;
@JsonProperty("GPUMemorySize") // 显示指定JSON映射名称
private Integer GPUMemorySize;
@Pattern(regexp = "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$",
message = "IP地址格式无效")
@JsonProperty("Ip") // 显示指定JSON映射名称
private String Ip;
public void setGPUId(Long GPUId) {
this.GPUId = GPUId; this.GPUId = GPUId;
} }
@ -27,18 +50,15 @@ public class GpuUpdateDTO {
message = "IP地址格式无效") String ip) { message = "IP地址格式无效") String ip) {
Ip = ip; Ip = ip;
} }
public Long getGPUId() {
return GPUId;
}
@NotNull(message = "GPU ID cannot be null") public Integer getGPUMemorySize() {
private Long GPUId; return GPUMemorySize;
}
@Pattern(regexp = "^([A-Z][A-Z0-9-]+)-\\w+", public void setGPUMemorySize(Integer GPUMemorySize) {
message = "型号格式应为 [厂商(大写字母开头)]-[型号],如 Intel-Xe_GPU") this.GPUMemorySize = GPUMemorySize;
private String GPUModel; }
@Setter
private Integer GPUMemorySize;
@Pattern(regexp = "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$",
message = "IP地址格式无效")
private String Ip;
} }

View File

@ -35,4 +35,28 @@ public class ResponseVO<T> implements Serializable {
public static <T> ResponseVO<T> error(ErrorCodeEnum errorCode) { public static <T> ResponseVO<T> error(ErrorCodeEnum errorCode) {
return new ResponseVO<>(errorCode.getCode(), errorCode.getMessage(), null); return new ResponseVO<>(errorCode.getCode(), errorCode.getMessage(), null);
} }
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
} }

View File

@ -17,9 +17,9 @@ import java.time.LocalDateTime;
@AllArgsConstructor @AllArgsConstructor
public class ServicePublishVO implements Serializable { public class ServicePublishVO implements Serializable {
private Long modelId; private Long modelId;
private String GPUModel; /*private String GPUModel;*/
private String ip; private String ip;
private String GPUMemorySize; /* private String GPUMemorySize;*/
private String apiUrl; private String apiUrl;

View File

@ -3,8 +3,10 @@ package com.bipt.intelligentapplicationorchestrationservice.service;
import com.bipt.intelligentapplicationorchestrationservice.mapper.GpuResourceDao; import com.bipt.intelligentapplicationorchestrationservice.mapper.GpuResourceDao;
import com.bipt.intelligentapplicationorchestrationservice.exception.CacheInitException; import com.bipt.intelligentapplicationorchestrationservice.exception.CacheInitException;
import com.bipt.intelligentapplicationorchestrationservice.entity.GpuResource; import com.bipt.intelligentapplicationorchestrationservice.entity.GpuResource;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.RedisConnectionFailureException; import org.springframework.data.redis.RedisConnectionFailureException;
@ -18,7 +20,6 @@ import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
@Transactional // 添加类级别事务管理
@Component @Component
public class CacheManager { public class CacheManager {
@Autowired @Autowired
@ -27,6 +28,9 @@ public class CacheManager {
@Autowired @Autowired
private GpuResourceDao gpuResourceDao; private GpuResourceDao gpuResourceDao;
@Autowired
private ObjectMapper objectMapper; // 注入ObjectMapper用于类型转换
private final ReentrantLock lock = new ReentrantLock(); private final ReentrantLock lock = new ReentrantLock();
@Value("${cache.redis-key-prefix:gpu:}") @Value("${cache.redis-key-prefix:gpu:}")
@ -38,9 +42,9 @@ public class CacheManager {
@Value("${cache.init-batch-size:500}") @Value("${cache.init-batch-size:500}")
private int initBatchSize; private int initBatchSize;
private static final Logger log = org.slf4j.LoggerFactory.getLogger(CacheManager.class); private static final Logger log = LoggerFactory.getLogger(CacheManager.class);
// 全量加载(带分页和分布式锁) // 全量加载(带分页和分布式锁)
@Transactional(propagation = Propagation.REQUIRED) // 方法级别覆盖
@PostConstruct @PostConstruct
public void loadFullCache() { public void loadFullCache() {
if (tryLock()) { if (tryLock()) {
@ -82,16 +86,12 @@ public class CacheManager {
// 带随机TTL的缓存设置 // 带随机TTL的缓存设置
private void setCacheWithTTL(GpuResource entity) { private void setCacheWithTTL(GpuResource entity) {
String key = buildKey(entity.getGPUId().toString()); String key = buildKey(entity.getGPUId().toString());
GpuResource cached = (GpuResource) redisTemplate.opsForValue().get(key);
// 保留原有内存字段值 // 直接存储实体对象,确保类型一致性
if (cached != null && cached.getGPUMemorySize() != null) {
entity.setGPUMemorySize(cached.getGPUMemorySize());
}
redisTemplate.opsForValue().set( redisTemplate.opsForValue().set(
key, key,
entity, entity,
ttlBase + (int)(Math.random() * 600), // 随机TTL防止雪崩 ttlBase + (int)(Math.random() * 600),
TimeUnit.SECONDS TimeUnit.SECONDS
); );
} }
@ -114,6 +114,7 @@ public class CacheManager {
private void unlock() { private void unlock() {
lock.unlock(); lock.unlock();
} }
// 分页加载入口 // 分页加载入口
public void loadFullCache(int batchSize) { public void loadFullCache(int batchSize) {
int page = 0; int page = 0;
@ -121,12 +122,11 @@ public class CacheManager {
List<GpuResource> batch = gpuResourceDao.findByPage(page * batchSize, batchSize); List<GpuResource> batch = gpuResourceDao.findByPage(page * batchSize, batchSize);
if (batch.isEmpty()) break; if (batch.isEmpty()) break;
batch.forEach(this::refreshWithRetry); // 带重试的刷新逻辑 batch.forEach(this::refreshWithRetry);
page++; page++;
} }
} }
// 带重试机制的缓存刷新 // 带重试机制的缓存刷新
public void refreshWithRetry(GpuResource entity) { public void refreshWithRetry(GpuResource entity) {
try { try {
@ -135,7 +135,7 @@ public class CacheManager {
// 3次重试逻辑 // 3次重试逻辑
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
try { try {
log.info("重试第 {} 次", i + 1); // 添加日志 log.info("重试第 {} 次", i + 1);
Thread.sleep(1000); Thread.sleep(1000);
setCacheWithTTL(entity); setCacheWithTTL(entity);
return; return;
@ -148,7 +148,6 @@ public class CacheManager {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
} }
} }
} }
@ -162,8 +161,35 @@ public class CacheManager {
redisTemplate.delete(key); redisTemplate.delete(key);
} }
// 修改获取缓存的方法,增加类型安全处理
@SuppressWarnings("unchecked")
public GpuResource getFromCache(String gpuId) { public GpuResource getFromCache(String gpuId) {
return (GpuResource) redisTemplate.opsForValue().get("gpu:" + gpuId); String key = buildKey(gpuId);
Object value = redisTemplate.opsForValue().get(key);
// 处理可能的类型不匹配问题
if (value == null) {
return null;
} }
try {
// 优先尝试直接转换
if (value instanceof GpuResource) {
return (GpuResource) value;
}
// 如果是LinkedHashMap使用ObjectMapper转换
else if (value instanceof java.util.LinkedHashMap) {
return objectMapper.convertValue(value, GpuResource.class);
}
// 其他情况尝试序列化后反序列化适用于JSON存储场景
else {
// 先序列化为JSON字符串再反序列化为对象
String json = objectMapper.writeValueAsString(value);
return objectMapper.readValue(json, GpuResource.class);
}
} catch (Exception e) {
log.error("获取缓存时类型转换失败key: {}, valueType: {}", key, value.getClass().getName(), e);
return null;
}
}
} }

View File

@ -97,6 +97,7 @@ public class AlgorithmInfoServiceImpl implements AlgorithmInfoService {
@Override @Override
public String run(Long id, String param) { public String run(Long id, String param) {
//todo从分布式存储中拿到文件以下是示例
String file = algorithmInfoMapper.getFileById(id); String file = algorithmInfoMapper.getFileById(id);
StringBuilder result = new StringBuilder(); // 用于存储结果 StringBuilder result = new StringBuilder(); // 用于存储结果

View File

@ -2,12 +2,15 @@ package com.bipt.intelligentapplicationorchestrationservice.service.Impl;
import com.bipt.intelligentapplicationorchestrationservice.mapper.PublishMapper; import com.bipt.intelligentapplicationorchestrationservice.mapper.PublishMapper;
import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishDTO; import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishDTO;
import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishVO;
import com.bipt.intelligentapplicationorchestrationservice.service.PublishService; import com.bipt.intelligentapplicationorchestrationservice.service.PublishService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/** /**
* @author hky * @author hky
*/ */
@ -35,6 +38,10 @@ public class PublishServiceImpl implements PublishService {
publishMapper.insert(servicePublishDTO); publishMapper.insert(servicePublishDTO);
} }
@Override
public List<ServicePublishVO> listPublishedServices() {
return publishMapper.listPublishedServices();
}
} }

View File

@ -1,12 +1,14 @@
package com.bipt.intelligentapplicationorchestrationservice.service; package com.bipt.intelligentapplicationorchestrationservice.service;
import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishDTO; import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishDTO;
import com.bipt.intelligentapplicationorchestrationservice.pojo.ServicePublishVO;
import java.util.List;
public interface PublishService { public interface PublishService {
void save(ServicePublishDTO servicePublishDTO); void save(ServicePublishDTO servicePublishDTO);
List<ServicePublishVO> listPublishedServices();
} }

View File

@ -1,7 +1,7 @@
package com.bipt.intelligentapplicationorchestrationservice.service.impl; package com.bipt.intelligentapplicationorchestrationservice.service.impl;
import com.bipt.intelligentapplicationorchestrationservice.mapper.GpuResourceDao; import com.bipt.intelligentapplicationorchestrationservice.mapper.GpuResourceDao;
import com.bipt.intelligentapplicationorchestrationservice.mapper.GpuMapper; import com.bipt.intelligentapplicationorchestrationservice.entity.GpuMapper;
import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuCreateDTO; import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuCreateDTO;
import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuResponseDTO; import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuResponseDTO;
import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuUpdateDTO; import com.bipt.intelligentapplicationorchestrationservice.pojo.GpuUpdateDTO;

View File

@ -57,4 +57,6 @@ spring.servlet.multipart.max-request-size=100MB
# 激活开发环境!告诉 Spring加载 application-dev.properties 里的配置 # 激活开发环境!告诉 Spring加载 application-dev.properties 里的配置
spring.profiles.active=dev spring.profiles.active=dev
#配置IP列表后续根据需求修改ip数据以下仅为测试用例
available.ips=192.168.1.100,192.168.1.101,192.168.1.102

View File

@ -2,10 +2,19 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bipt.intelligentapplicationorchestrationservice.mapper.GpuResourceDao"> <mapper namespace="com.bipt.intelligentapplicationorchestrationservice.mapper.GpuResourceDao">
<resultMap id="gpuResourceMap" type="com.bipt.intelligentapplicationorchestrationservice.entity.GpuResource">
<id property="GPUId" column="GPUId" /> <!-- 强制映射 -->
<result property="createTime" column="created_time"/>
<result property="UpdateTime" column="update_time"/>
<result property="GPUModel" column="GPUModel"/>
<result property="GPUMemorySize" column="GPUMemorySize"/>
<result property="Ip" column="Ip"/>
<result property="isDeleted" column="is_deleted"/>
<result property="GPUMaxMemory" column="GPUMaxMemory"/>
</resultMap>
<!-- 动态条件查询 --> <!-- 动态条件查询 -->
<select id="selectByFields" <select id="selectByFields"
resultType="com.bipt.intelligentapplicationorchestrationservice.entity.GpuResource"> resultMap="gpuResourceMap">
SELECT * SELECT *
FROM Ipz.public.gpu_resource FROM Ipz.public.gpu_resource
<where> <where>

View File

@ -3,8 +3,8 @@
<mapper namespace="com.bipt.intelligentapplicationorchestrationservice.mapper.PublishMapper"> <mapper namespace="com.bipt.intelligentapplicationorchestrationservice.mapper.PublishMapper">
<insert id="insert"> <insert id="insert">
INSERT INTO service_publish INSERT INTO service_publish
(id,model_id,api_url,create_time) (id,model_id,api_url,create_time,ip)
values (#{id}, #{modelId}, #{apiUrl}, #{createTime}) values (#{id}, #{modelId}, #{apiUrl}, #{createTime},#{ip})
</insert> </insert>
<select id="getByApiUrl" resultType="java.lang.Long"> <select id="getByApiUrl" resultType="java.lang.Long">