From 5f17c47ef4b91e94be75cc02dca9ff41f70016c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Czzxxiansheng?= <14100340+zhou-zhuoxian@user.noreply.gitee.com> Date: Mon, 29 Apr 2024 16:53:52 +0800 Subject: [PATCH 1/2] [ISSUES#137]Added an API for the dashboard to read the runtime configuration --- .../console/controller/ConfigController.java | 12 ++- .../console/mapper/config/ConfigMapper.java | 18 +++-- .../console/service/config/ConfigService.java | 2 +- .../config/Impl/ConfigServiceImpl.java | 10 +-- .../config/remote/RemoteRuntimeService.java | 79 +++++++++++++++++++ .../main/resources/eventmesh-dashboard.sql | 16 ++-- 6 files changed, 111 insertions(+), 26 deletions(-) create mode 100644 eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/remote/RemoteRuntimeService.java diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/controller/ConfigController.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/controller/ConfigController.java index ca819b7d..ea67df56 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/controller/ConfigController.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/controller/ConfigController.java @@ -55,13 +55,17 @@ public String updateConfigsByTypeAndId(@Validated @RequestBody UpdateConfigDTO u public List getInstanceDetailConfigs(@Validated @RequestBody GetConfigsListDTO getConfigsListDTO) { List configEntityList = configService.selectToFront(getConfigsListDTO.getInstanceId(), getConfigsListDTO.getInstanceType(), getConfigsListDTO); - Map stringStringConcurrentHashMap = configService.selectDefaultConfig(getConfigsListDTO.getBusinessType(), - getConfigsListDTO.getInstanceId(), getConfigsListDTO.getInstanceType()); + Map stringStringConcurrentHashMap = configService.selectDefaultConfig(getConfigsListDTO.getBusinessType(), + getConfigsListDTO.getInstanceType()); ArrayList showDetailConfigsVOS = new ArrayList<>(); + System.out.println(stringStringConcurrentHashMap.get("eventMesh.server.global.scheduler")); configEntityList.forEach(n -> { DetailConfigsVO showDetailConfigsVO = new DetailConfigsVO(); - showDetailConfigsVO.setDefaultValue(stringStringConcurrentHashMap.get(n.getConfigName())); - showDetailConfigsVO.setIsModify(n.getIsModify()); + ConfigEntity defaultConfig = stringStringConcurrentHashMap.get(n.getConfigName()); + if (defaultConfig != null) { + showDetailConfigsVO.setDefaultValue(defaultConfig.getConfigValue()); + showDetailConfigsVO.setIsModify(defaultConfig.getIsModify()); + } showDetailConfigsVO.setConfigName(n.getConfigName()); showDetailConfigsVO.setConfigValue(n.getConfigValue()); showDetailConfigsVO.setAlreadyUpdate(n.getAlreadyUpdate()); diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/mapper/config/ConfigMapper.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/mapper/config/ConfigMapper.java index 838e95f8..7377927e 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/mapper/config/ConfigMapper.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/mapper/config/ConfigMapper.java @@ -63,14 +63,16 @@ public interface ConfigMapper { @Insert({ ""}) + "INSERT INTO config (cluster_id, business_type, instance_type, instance_id, config_name, config_value, start_version,", + "eventmesh_version, end_version, diff_type, description, edit, is_default, is_modify) VALUES ", + "", + "(#{c.clusterId}, #{c.businessType}, #{c.instanceType}, #{c.instanceId}, #{c.configName},", + "#{c.configValue}, #{c.startVersion}, #{c.eventmeshVersion}, #{c.endVersion}, #{c.diffType}, #{c.description},", + "#{c.edit}, #{c.isDefault}, #{c.isModify})", + "", + "ON DUPLICATE KEY UPDATE config_value = VALUES(config_value)", + "" + }) @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") void batchInsert(List configEntityList); diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/ConfigService.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/ConfigService.java index 3be1c537..4e43be8a 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/ConfigService.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/ConfigService.java @@ -51,7 +51,7 @@ public interface ConfigService { List selectByInstanceIdAndType(Long instanceId, Integer type); - Map selectDefaultConfig(String version, Long instanceId, Integer instanceType); + Map selectDefaultConfig(String version, Integer instanceType); diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/Impl/ConfigServiceImpl.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/Impl/ConfigServiceImpl.java index b3e06d65..010fdaf2 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/Impl/ConfigServiceImpl.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/Impl/ConfigServiceImpl.java @@ -45,7 +45,7 @@ public class ConfigServiceImpl implements ConfigService { @Autowired ConfigMapper configMapper; - private Map defaultConfigCache = new HashMap<>(); + private Map defaultConfigCache = new HashMap<>(); @EmLog(OprTarget = "Runtime", OprType = "UpdateConfigs") @@ -209,17 +209,17 @@ public void addDefaultConfigToCache() { List configEntityList = configMapper.selectAllDefaultConfig(); configEntityList.forEach(n -> { DefaultConfigKey defaultConfigKey = new DefaultConfigKey(n.getBusinessType(), n.getConfigName()); - defaultConfigCache.putIfAbsent(defaultConfigKey, n.getConfigValue()); + defaultConfigCache.putIfAbsent(defaultConfigKey, n); }); } @Override - public Map selectDefaultConfig(String businessType, Long instanceId, Integer instanceType) { + public Map selectDefaultConfig(String businessType, Integer instanceType) { if (defaultConfigCache.size() == 0) { this.addDefaultConfigToCache(); } - ConcurrentHashMap stringStringConcurrentHashMap = new ConcurrentHashMap<>(); + ConcurrentHashMap stringStringConcurrentHashMap = new ConcurrentHashMap<>(); defaultConfigCache.forEach((k, v) -> { if (k.getBusinessType().equals(businessType)) { stringStringConcurrentHashMap.put(k.getConfigName(), v); @@ -228,4 +228,4 @@ public Map selectDefaultConfig(String businessType, Long instanc return stringStringConcurrentHashMap; } -} +} \ No newline at end of file diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/remote/RemoteRuntimeService.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/remote/RemoteRuntimeService.java new file mode 100644 index 00000000..0f632422 --- /dev/null +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/remote/RemoteRuntimeService.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.dashboard.console.service.config.remote; + + +import org.apache.eventmesh.dashboard.console.entity.config.ConfigEntity; +import org.apache.eventmesh.dashboard.console.entity.runtime.RuntimeEntity; +import org.apache.eventmesh.dashboard.console.mapper.config.ConfigMapper; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map.Entry; +import java.util.Set; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; + +@Service +public class RemoteRuntimeService { + + @Autowired + private ConfigMapper configMapper; + + public void getRuntimeConfigByCluster(RuntimeEntity runtimeEntity) { + RestTemplate restTemplate = new RestTemplate(); + String forEntity = + restTemplate.getForObject("http://" + runtimeEntity.getHost() + ":" + runtimeEntity.getPort() + "/v2/configuration?format=properties", String.class, (Object) null); + JSONObject jsonMap = JSON.parseObject(forEntity); + Set> entries = jsonMap.entrySet(); + ArrayList configEntities = new ArrayList<>(); + for (Entry entry : entries) { + if (!entry.getKey().equals("data")) { + continue; + } + JSONObject parse = JSON.parseObject(entry.getValue().toString()); + Set> configurationEntry = parse.entrySet(); + configurationEntry.forEach((configuration) -> { + JSONObject parse1 = JSON.parseObject(configuration.getValue().toString()); + Set> configureEntry = parse1.entrySet(); + configureEntry.forEach((configure) -> { + ConfigEntity config = this.createRuntimeConfigEntity(runtimeEntity, configure.getKey(), configure.getValue().toString()); + configEntities.add(config); + }); + }); + } + configMapper.batchInsert(configEntities); + } + + private ConfigEntity createRuntimeConfigEntity(RuntimeEntity runtimeEntity, String configName, String configValue) { + ConfigEntity config = new ConfigEntity(); + config.setConfigName(configName); + config.setConfigValue(configValue); + config.setBusinessType("runtime"); + config.setInstanceType(0); + config.setClusterId(runtimeEntity.getClusterId()); + config.setIsDefault(0); + config.setInstanceId(runtimeEntity.getId()); + return config; + } +} diff --git a/eventmesh-dashboard-console/src/main/resources/eventmesh-dashboard.sql b/eventmesh-dashboard-console/src/main/resources/eventmesh-dashboard.sql index 51655bc4..9278875f 100644 --- a/eventmesh-dashboard-console/src/main/resources/eventmesh-dashboard.sql +++ b/eventmesh-dashboard-console/src/main/resources/eventmesh-dashboard.sql @@ -53,14 +53,14 @@ create table config instance_type tinyint not null comment '实例类型 0:runtime,1:storage,2:connector,3:topic', instance_id bigint default -1 not null comment '实例ID,上面配置对应的(比如runtime)的id', config_name varchar(192) default '' not null comment '配置名称', - config_value text null comment '配置值', - start_version varchar(64) default '' not null comment '配置开始使用的版本', - status int default 1 not null comment '0 关闭 1 开启 ', - is_default int default 1 null, - end_version varchar(64) default '' not null comment '配置结束使用的版本', - diff_type int default -1 not null comment '差异类型', - description varchar(1000) default '' not null comment '备注', - edit int default 1 not null comment '是否可以编辑 1 不可编辑(程序获取) 2 可编辑', + config_value text null comment '配置值', + start_version varchar(64) default '' null comment '配置开始使用的版本', + status int default 1 null comment '0 关闭 1 开启 ', + is_default int default 1 null, + end_version varchar(64) default '' null comment '配置结束使用的版本', + diff_type int default -1 null comment '差异类型', + description varchar(1000) default '' null comment '备注', + edit int default 1 null comment '是否可以编辑 1 不可编辑(程序获取) 2 可编辑', create_time timestamp default CURRENT_TIMESTAMP not null comment '创建时间', update_time timestamp default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '修改时间', is_modify int default 0 not null, From 5bb99756a6f184854667a501876c1297ce12018e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Czzxxiansheng?= <14100340+zhou-zhuoxian@user.noreply.gitee.com> Date: Mon, 29 Apr 2024 16:57:37 +0800 Subject: [PATCH 2/2] resolve error --- .../eventmesh/dashboard/console/controller/ConfigController.java | 1 - .../console/service/config/remote/RemoteRuntimeService.java | 1 - 2 files changed, 2 deletions(-) diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/controller/ConfigController.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/controller/ConfigController.java index ea67df56..9a2305ba 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/controller/ConfigController.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/controller/ConfigController.java @@ -58,7 +58,6 @@ public List getInstanceDetailConfigs(@Validated @RequestBody Ge Map stringStringConcurrentHashMap = configService.selectDefaultConfig(getConfigsListDTO.getBusinessType(), getConfigsListDTO.getInstanceType()); ArrayList showDetailConfigsVOS = new ArrayList<>(); - System.out.println(stringStringConcurrentHashMap.get("eventMesh.server.global.scheduler")); configEntityList.forEach(n -> { DetailConfigsVO showDetailConfigsVO = new DetailConfigsVO(); ConfigEntity defaultConfig = stringStringConcurrentHashMap.get(n.getConfigName()); diff --git a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/remote/RemoteRuntimeService.java b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/remote/RemoteRuntimeService.java index 0f632422..69045d31 100644 --- a/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/remote/RemoteRuntimeService.java +++ b/eventmesh-dashboard-console/src/main/java/org/apache/eventmesh/dashboard/console/service/config/remote/RemoteRuntimeService.java @@ -23,7 +23,6 @@ import org.apache.eventmesh.dashboard.console.mapper.config.ConfigMapper; import java.util.ArrayList; -import java.util.HashMap; import java.util.Map.Entry; import java.util.Set;