diff --git a/cloud-common/src/main/java/com/cm/common/base/EnvController.java b/cloud-common/src/main/java/com/cm/common/base/EnvController.java new file mode 100644 index 0000000..25d416c --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/base/EnvController.java @@ -0,0 +1,43 @@ +package com.cm.common.base; + +import com.cm.common.annotation.CheckRequestBodyAnnotation; +import com.cm.common.constants.ISystemConstant; +import com.cm.common.pojo.dtos.env.EnvDTO; +import com.cm.common.pojo.vos.env.EnvListVO; +import com.cm.common.result.ErrorResult; +import com.cm.common.result.SuccessResult; +import com.cm.common.service.IEnvService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "环境变量") +@RestController +@RequestMapping(ISystemConstant.API_PREFIX + "/env") +public class EnvController extends AbstractController { + + @Autowired + private IEnvService envService; + + @ApiOperation(value = "更新", notes = "更新") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PutMapping("update") + @CheckRequestBodyAnnotation + public SuccessResult update(@RequestBody EnvListVO envListVO) { + envService.update(envListVO); + return new SuccessResult(); + } + + @ApiOperation(value = "获取配置", notes = "获取配置接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @GetMapping("list") + public List list() { + return envService.list(); + } + +} diff --git a/cloud-common/src/main/java/com/cm/common/base/EnvRouteController.java b/cloud-common/src/main/java/com/cm/common/base/EnvRouteController.java new file mode 100644 index 0000000..06d4459 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/base/EnvRouteController.java @@ -0,0 +1,21 @@ +package com.cm.common.base; + +import com.cm.common.constants.ISystemConstant; +import io.swagger.annotations.Api; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.ModelAndView; + +@Api(tags = ISystemConstant.ROUTE_TAGS_PREFIX + "环境变量") +@RestController +@RequestMapping(ISystemConstant.ROUTE_PREFIX + "/env") +public class EnvRouteController { + + @GetMapping("update") + public ModelAndView update() { + ModelAndView modelAndView = new ModelAndView("env/update"); + return modelAndView; + } + +} diff --git a/cloud-common/src/main/java/com/cm/common/dao/IEnvDao.java b/cloud-common/src/main/java/com/cm/common/dao/IEnvDao.java new file mode 100644 index 0000000..9c49b92 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/dao/IEnvDao.java @@ -0,0 +1,57 @@ +package com.cm.common.dao; + +import com.cm.common.exception.RemoveException; +import com.cm.common.exception.SaveException; +import com.cm.common.exception.SearchException; +import com.cm.common.exception.UpdateException; +import com.cm.common.pojo.dtos.env.EnvDTO; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Map; + +@Repository +public interface IEnvDao { + + /** + * 新增 + * + * @param params + * @throws SaveException + */ + void save(Map params) throws SaveException; + + /** + * 修改 + * + * @param params + * @throws UpdateException + */ + void update(Map params) throws UpdateException; + + /** + * 删除 + * + * @throws RemoveException + */ + void delete() throws RemoveException; + + /** + * 列表 + * + * @return + * @throws SearchException + */ + List list() throws SearchException; + + /** + * 详情 + * + * @param envKey + * @return + * @throws SearchException + */ + EnvDTO get(String envKey) throws SearchException; + +} + diff --git a/cloud-common/src/main/java/com/cm/common/manager/EnvManager.java b/cloud-common/src/main/java/com/cm/common/manager/EnvManager.java new file mode 100644 index 0000000..5e16489 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/manager/EnvManager.java @@ -0,0 +1,78 @@ +package com.cm.common.manager; + + +import com.cm.common.dao.IEnvDao; +import com.cm.common.pojo.Env; +import com.cm.common.pojo.dtos.env.EnvDTO; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + + +public class EnvManager { + + private static final Logger LOG = LoggerFactory.getLogger(EnvManager.class); + private static EnvManager envManager = EnvManagerBuilder.envManager; + private static Pattern ENV_VARIABLE = Pattern.compile("%[a-zA-Z\\d\\_\\-]+%"); + private IEnvDao envDao; + private Env env = new Env(); + + private EnvManager() {} + + public static EnvManager getInstance() { + return envManager; + } + + /** + * 刷新环境变量 + */ + public void refreshEnv() { + env.clear(); + List envDTOS = envDao.list(); + envDTOS.forEach(envDTO -> { + env.put(envDTO.getEnvKey(), envDTO.getEnvValue()); + }); + } + + /** + * 获得环境变量 + * + * @return + */ + public Env getEnv() { + return env; + } + + public boolean isKeyExist(String key) { + return env.containsKey(key); + } + + public String getValue(String key) { + String value = env.get(key); + value = value == null ? "" : value; + // 替换变量 + Matcher matcher = ENV_VARIABLE.matcher(value); + while (matcher.find()) { + String variable = matcher.group(); + String variableValue = env.get(variable.substring(1, variable.length() - 1)); + if (StringUtils.isBlank(variableValue)) { + continue; + } + value = value.replaceFirst(variable, variableValue); + } + return value; + } + + public void setEnvDao(IEnvDao envDao) { + this.envDao = envDao; + } + + private static class EnvManagerBuilder { + public static final EnvManager envManager = new EnvManager(); + } + +} diff --git a/cloud-common/src/main/java/com/cm/common/pojo/Env.java b/cloud-common/src/main/java/com/cm/common/pojo/Env.java new file mode 100644 index 0000000..19a48f0 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/pojo/Env.java @@ -0,0 +1,76 @@ +package com.cm.common.pojo; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +public class Env implements Map { + + Map envMap = new ConcurrentHashMap<>(); + + @Override + public int size() { + return envMap.size(); + } + + @Override + public boolean isEmpty() { + return envMap.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return envMap.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return envMap.containsValue(value); + } + + @Override + public String get(Object key) { + return envMap.get(key); + } + + @Override + public String put(String key, String value) { + return envMap.put(key, value); + } + + @Override + public String remove(Object key) { + return envMap.remove(key); + } + + @Override + public void putAll(Map m) { + envMap.putAll(m); + } + + @Override + public void clear() { + envMap.clear(); + } + + @Override + public Set keySet() { + return envMap.keySet(); + } + + @Override + public Collection values() { + return envMap.values(); + } + + @Override + public Set> entrySet() { + return envMap.entrySet(); + } + + @Override + public String toString() { + return envMap.toString(); + } +} diff --git a/cloud-common/src/main/java/com/cm/common/pojo/dtos/env/EnvDTO.java b/cloud-common/src/main/java/com/cm/common/pojo/dtos/env/EnvDTO.java new file mode 100644 index 0000000..1d87616 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/pojo/dtos/env/EnvDTO.java @@ -0,0 +1,39 @@ +package com.cm.common.pojo.dtos.env; + +import io.swagger.annotations.ApiModelProperty; + +import java.io.Serializable; + +public class EnvDTO implements Serializable { + + @ApiModelProperty(name = "envKey", value = "环境变量") + private String envKey; + @ApiModelProperty(name = "envExplain", value = "环境变量说明") + private String envExplain; + @ApiModelProperty(name = "envValue", value = "环境变量值") + private String envValue; + + public String getEnvKey() { + return envKey == null ? "" : envKey.trim(); + } + + public void setEnvKey(String envKey) { + this.envKey = envKey; + } + + public String getEnvExplain() { + return envExplain == null ? "" : envExplain.trim(); + } + + public void setEnvExplain(String envExplain) { + this.envExplain = envExplain; + } + + public String getEnvValue() { + return envValue == null ? "" : envValue.trim(); + } + + public void setEnvValue(String envValue) { + this.envValue = envValue; + } +} diff --git a/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvListVO.java b/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvListVO.java new file mode 100644 index 0000000..1b2bdcf --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvListVO.java @@ -0,0 +1,31 @@ +package com.cm.common.pojo.vos.env; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.ArrayList; +import java.util.List; + +@ApiModel +public class EnvListVO { + + @ApiModelProperty(name = "envs", value = "环境变量列表") + private List envs; + + public List getEnvs() { + return envs == null ? new ArrayList() : envs; + } + + public void setEnvs(List envs) { + this.envs = envs; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("{"); + sb.append("\"envs\":") + .append(envs); + sb.append('}'); + return sb.toString(); + } +} \ No newline at end of file diff --git a/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvVO.java b/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvVO.java new file mode 100644 index 0000000..f1d8f7d --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvVO.java @@ -0,0 +1,52 @@ +package com.cm.common.pojo.vos.env; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel +public class EnvVO { + + @ApiModelProperty(name = "envKey", value = "环境变量") + private String envKey; + @ApiModelProperty(name = "envExplain", value = "环境变量说明") + private String envExplain; + @ApiModelProperty(name = "envValue", value = "环境变量值") + private String envValue; + + public String getEnvKey() { + return envKey == null ? "" : envKey.trim(); + } + + public void setEnvKey(String envKey) { + this.envKey = envKey; + } + + public String getEnvExplain() { + return envExplain == null ? "" : envExplain.trim(); + } + + public void setEnvExplain(String envExplain) { + this.envExplain = envExplain; + } + + public String getEnvValue() { + return envValue == null ? "" : envValue.trim(); + } + + public void setEnvValue(String envValue) { + this.envValue = envValue; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("{"); + sb.append("\"envKey\":\"") + .append(envKey).append('\"'); + sb.append(",\"envExplain\":\"") + .append(envExplain).append('\"'); + sb.append(",\"envValue\":\"") + .append(envValue).append('\"'); + sb.append('}'); + return sb.toString(); + } +} diff --git a/cloud-common/src/main/java/com/cm/common/service/IEnvService.java b/cloud-common/src/main/java/com/cm/common/service/IEnvService.java new file mode 100644 index 0000000..0738e88 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/service/IEnvService.java @@ -0,0 +1,24 @@ +package com.cm.common.service; + +import com.cm.common.pojo.dtos.env.EnvDTO; +import com.cm.common.pojo.vos.env.EnvListVO; + +import java.util.List; + +public interface IEnvService { + + /** + * 更新 + * + * @param envMap + */ + void update(EnvListVO envListVO); + + /** + * 详情 + * + * @return + */ + List list(); + +} \ No newline at end of file diff --git a/cloud-common/src/main/java/com/cm/common/service/impl/EnvServiceImpl.java b/cloud-common/src/main/java/com/cm/common/service/impl/EnvServiceImpl.java new file mode 100644 index 0000000..66e7914 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/service/impl/EnvServiceImpl.java @@ -0,0 +1,45 @@ +package com.cm.common.service.impl; + +import com.cm.common.base.AbstractService; +import com.cm.common.dao.IEnvDao; +import com.cm.common.manager.EnvManager; +import com.cm.common.pojo.Env; +import com.cm.common.pojo.dtos.env.EnvDTO; +import com.cm.common.pojo.vos.env.EnvListVO; +import com.cm.common.pojo.vos.env.EnvVO; +import com.cm.common.service.IEnvService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +@Service +public class EnvServiceImpl extends AbstractService implements IEnvService { + + @Autowired + private IEnvDao envDao; + + @Override + public void update(EnvListVO envListVO) { + // 清空 + envDao.delete(); + // 新增 + Map params = getHashMap(5); + for (EnvVO envVO : envListVO.getEnvs()) { + params.put("envKey", envVO.getEnvKey()); + params.put("envExplain", envVO.getEnvExplain()); + params.put("envValue", envVO.getEnvValue()); + envDao.save(params); + } + // 刷新缓存 + EnvManager.getInstance().refreshEnv(); + } + + @Override + public List list() { + Env env = EnvManager.getInstance().getEnv(); + System.out.println(env.toString()); + return envDao.list(); + } +} diff --git a/cloud-common/src/main/resources/mybatis/mapper/env-mapper.xml b/cloud-common/src/main/resources/mybatis/mapper/env-mapper.xml new file mode 100644 index 0000000..fccbe09 --- /dev/null +++ b/cloud-common/src/main/resources/mybatis/mapper/env-mapper.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + INSERT INTO sys_env( + env_key, + env_explain, + env_value + ) VALUES( + #{envKey}, + #{envExplain}, + #{envValue} + ) + + + + + DELETE FROM sys_env + + + + + UPDATE + sys_env + SET + env_value = #{envValue}, + env_explain = #{envExplain} + WHERE + env_key = #{envKey} + + + + + + + + \ No newline at end of file diff --git a/cloud-common/src/main/resources/templates/env/update.html b/cloud-common/src/main/resources/templates/env/update.html new file mode 100644 index 0000000..84cfe7c --- /dev/null +++ b/cloud-common/src/main/resources/templates/env/update.html @@ -0,0 +1,252 @@ + + + + + + + + + + + + + +
+
+ +
+
+ +
+
+
+
+ + + + \ No newline at end of file