Skip to content

Commit

Permalink
表结构设计:
Browse files Browse the repository at this point in the history
iot_config_center
id,parent_id(-1为根目录),key,value,status (-1删除,0失效,1有效),app_id,created,update,update_uid

iot_app_config 应用表
id,app_code,name,status (-1删除,0失效,1有效),created,update,update_uid

配置平台接口
configCreate(parent_id,key,value)
configUpdate(id,value)
configStatusUpdate(id,status)

微服务创建
appConfigCreate(value)

Redis
config_appId_key value
后台管理系统保存回溯覆盖

内存缓存(单实例)
map(app_id, hashmap(key, Object))
Object:value,childKey_list

微服务
getValue(app_code(业务id),key)
Http

应用场景:低数据量高频变动高性能配置化需求
  • Loading branch information
charging-kuafuai committed Dec 6, 2023
1 parent 747eda9 commit 7ffa9c6
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 119 deletions.
38 changes: 38 additions & 0 deletions src/main/java/com/aiassistant/controller/ConfigController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.aiassistant.controller;

import com.aiassistant.model.Config;
import com.aiassistant.service.ConfigService;
import com.aiassistant.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/config")
public class ConfigController {
private final ConfigService configService;

@Autowired
public ConfigController(ConfigService configService) {
this.configService = configService;
}

@PostMapping("/add")
public ResultModel addConfig(@RequestBody Config config) {
return configService.addConfig(config);
}

@PostMapping("/updateValue")
public ResultModel updateConfigValue(@RequestBody Config config) {
return configService.updateConfigValue(config);
}

@PostMapping("/updateStatus")
public ResultModel updateConfigStatus(@RequestBody Config config) {
return configService.updateConfigStatus(config);
}

@GetMapping("/getValue")
public ResultModel getConfigValue(@RequestParam String key, @RequestParam String defaultValue) {
return configService.getConfigValue(key, defaultValue);
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/aiassistant/mapper/ConfigMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.aiassistant.mapper;

import com.aiassistant.model.Config;
import org.apache.ibatis.annotations.*;

@Mapper
public interface ConfigMapper {
@Insert("INSERT INTO config(app_code, key, value, status) VALUES(#{appCode}, #{key}, #{value}, #{status})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
int insertConfig(Config config);

@Update("UPDATE config SET value = #{value} WHERE app_code = #{appCode} AND key = #{key}")
int updateConfigValue(Config config);

@Update("UPDATE config SET status = #{status} WHERE app_code = #{appCode} AND key = #{key}")
int updateConfigStatus(Config config);

@Select("SELECT value FROM config WHERE app_code = #{appCode} AND key = #{key}")
String selectConfigValue(@Param("appCode") String appCode, @Param("key") String key);
}
49 changes: 49 additions & 0 deletions src/main/java/com/aiassistant/model/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.aiassistant.model;

public class Config {
private int id;
private int parentId;
private String key;
private String value;
private int status;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getParentId() {
return parentId;
}

public void setParentId(int parentId) {
this.parentId = parentId;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}
}
53 changes: 53 additions & 0 deletions src/main/java/com/aiassistant/service/ConfigService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.aiassistant.service;

import com.aiassistant.mapper.ConfigMapper;
import com.aiassistant.model.Config;
import com.aiassistant.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ConfigService {
@Autowired
private ConfigMapper configMapper;

public ResultModel addConfig(Config config) {
try {
configMapper.insertConfig(config);
return ResultModel.ofSuccess();
} catch (Exception e) {
return ResultModel.ofError("Failed to add config", e);
}
}

public ResultModel updateConfigValue(Config config) {
try {
configMapper.updateConfigValue(config);
return ResultModel.ofSuccess();
} catch (Exception e) {
return ResultModel.ofError("Failed to update config value", e);
}
}

public ResultModel updateConfigStatus(Config config) {
try {
configMapper.updateConfigStatus(config);
return ResultModel.ofSuccess();
} catch (Exception e) {
return ResultModel.ofError("Failed to update config status", e);
}
}

public ResultModel<String> getConfigValue(String configKey, String defaultValue) {
try {
String configValue = configMapper.getConfigValue(configKey);
if (configValue != null) {
return ResultModel.ofSuccess(configValue);
} else {
return ResultModel.ofSuccess(defaultValue);
}
} catch (Exception e) {
return ResultModel.ofError("Failed to get config value", e);
}
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/aiassistant/service/impl/ConfigServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.aiassistant.service.impl;

import com.aiassistant.mapper.ConfigMapper;
import com.aiassistant.model.Config;
import com.aiassistant.service.ConfigService;
import com.aiassistant.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ConfigServiceImpl implements ConfigService {

private final ConfigMapper configMapper;

@Autowired
public ConfigServiceImpl(ConfigMapper configMapper) {
this.configMapper = configMapper;
}

@Override
public ResultModel addConfig(Config config) {
try {
configMapper.insertConfig(config);
return ResultModel.ofSuccess();
} catch (Exception e) {
return ResultModel.ofError("Failed to add config", e);
}
}

@Override
public ResultModel updateConfigValue(Config config) {
try {
configMapper.updateConfigValue(config);
return ResultModel.ofSuccess();
} catch (Exception e) {
return ResultModel.ofError("Failed to update config value", e);
}
}

@Override
public ResultModel updateConfigStatus(Config config) {
try {
configMapper.updateConfigStatus(config);
return ResultModel.ofSuccess();
} catch (Exception e) {
return ResultModel.ofError("Failed to update config status", e);
}
}

@Override
public ResultModel<String> getConfigValue(String configKey, String defaultValue) {
try {
String configValue = configMapper.getConfigValue(configKey);
if (configValue != null) {
return ResultModel.ofSuccess(configValue);
} else {
return ResultModel.ofSuccess(defaultValue);
}
} catch (Exception e) {
return ResultModel.ofError("Failed to get config value", e);
}
}
}
122 changes: 58 additions & 64 deletions src/main/java/com/aiassistant/utils/ResultModel.java
Original file line number Diff line number Diff line change
@@ -1,91 +1,85 @@
package com.aiassistant.utils;

import lombok.Data;

/**
* 控制层-对外返回数据结构
*
* @param <T>
*/
@Data
public class ResultModel<T> {

private Exception exception = null;
private T data = null;
private String msg;
private Integer code;
private String msg;
private T data;
private Exception exception;

public static <T> ResultModel ofResult(Integer code, String msg, T data) {
return ofResult(code, msg, data, null);
public ResultModel(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}

public static <T> ResultModel ofResult(Integer code, String msg, T data, Exception exception) {
ResultModel<T> resultModel = new ResultModel();
resultModel.setCode(code);
resultModel.setData(data);
resultModel.setMsg(msg);
resultModel.setException(exception);
return resultModel;
public ResultModel(Integer code, String msg, T data, Exception exception) {
this.code = code;
this.msg = msg;
this.data = data;
this.exception = exception;
}

/**
* 创建成功返回
*
* @return
*/
public static ResultModel ofSuccess() {
public static <T> ResultModel<T> ofResult(Integer code, String msg, T data) {
return new ResultModel<>(code, msg, data);
}

return ofSuccess(null);
public static <T> ResultModel<T> ofResult(Integer code, String msg, T data, Exception exception) {
return new ResultModel<>(code, msg, data, exception);
}

/**
* 创建成功返回-带返回数据
*
* @param data
* @param <T>
* @return
*/
public static <T> ResultModel ofSuccess(T data) {
public static <T> ResultModel<T> ofSuccess() {
return new ResultModel<>(0, "success", null);
}

return ofSuccess(null, data);
public static <T> ResultModel<T> ofSuccess(T data) {
return new ResultModel<>(0, "success", data);
}

/**
* 创建成功返回-带返回信息和返回数据
*
* @param msg
* @param data
* @param <T>
* @return
*/
public static <T> ResultModel ofSuccess(String msg, T data) {
public static <T> ResultModel<T> ofSuccess(String msg, T data) {
return new ResultModel<>(0, msg, data);
}

return ofResult(0, msg, data);
public static <T> ResultModel<T> ofError() {
return new ResultModel<>(-1, "error", null);
}

/**
* 创建失败返回
*
* @return
*/
public static ResultModel ofError() {
public static <T> ResultModel<T> ofError(String msg) {
return new ResultModel<>(-1, msg, null);
}

return ofError(null);
public static <T> ResultModel<T> ofError(String msg, Exception exception) {
return new ResultModel<>(-1, msg, null, exception);
}

/**
* 创建失败返回-带返回信息
*
* @param msg
* @return
*/
public static ResultModel ofError(String msg) {
public Integer getCode() {
return code;
}

return ofError(msg, null);
public void setCode(Integer code) {
this.code = code;
}

public static ResultModel ofError(String msg, Exception exception) {
public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

public Exception getException() {
return exception;
}

return ofResult(999, msg, null, exception);
public void setException(Exception exception) {
this.exception = exception;
}
}
Loading

0 comments on commit 7ffa9c6

Please sign in to comment.