集群列表、新增集群
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.xuxd.kafka.console.beans.dto;
|
||||
|
||||
import com.xuxd.kafka.console.beans.dos.ClusterInfoDO;
|
||||
import com.xuxd.kafka.console.utils.ConvertUtil;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* kafka-console-ui.
|
||||
*
|
||||
* @author xuxd
|
||||
* @date 2022-01-04 20:19:03
|
||||
**/
|
||||
@Data
|
||||
public class ClusterInfoDTO {
|
||||
private Long id;
|
||||
|
||||
private String clusterName;
|
||||
|
||||
private String address;
|
||||
|
||||
private String properties;
|
||||
|
||||
private String updateTime;
|
||||
|
||||
public ClusterInfoDO to() {
|
||||
ClusterInfoDO infoDO = new ClusterInfoDO();
|
||||
infoDO.setId(id);
|
||||
infoDO.setClusterName(clusterName);
|
||||
infoDO.setAddress(address);
|
||||
|
||||
if (StringUtils.isNotBlank(properties)) {
|
||||
infoDO.setProperties(ConvertUtil.propertiesStr2JsonStr(properties));
|
||||
}
|
||||
|
||||
return infoDO;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.xuxd.kafka.console.beans.vo;
|
||||
|
||||
import com.xuxd.kafka.console.beans.dos.ClusterInfoDO;
|
||||
import com.xuxd.kafka.console.utils.ConvertUtil;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* kafka-console-ui.
|
||||
*
|
||||
* @author xuxd
|
||||
* @date 2022-01-04 19:16:11
|
||||
**/
|
||||
@Data
|
||||
public class ClusterInfoVO {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String clusterName;
|
||||
|
||||
private String address;
|
||||
|
||||
private List<String> properties;
|
||||
|
||||
private String updateTime;
|
||||
|
||||
public static ClusterInfoVO from(ClusterInfoDO infoDO) {
|
||||
ClusterInfoVO vo = new ClusterInfoVO();
|
||||
vo.setId(infoDO.getId());
|
||||
vo.setClusterName(infoDO.getClusterName());
|
||||
vo.setAddress(infoDO.getAddress());
|
||||
vo.setUpdateTime(infoDO.getUpdateTime());
|
||||
if (StringUtils.isNotBlank(infoDO.getProperties())) {
|
||||
vo.setProperties(ConvertUtil.jsonStr2List(infoDO.getProperties()));
|
||||
}
|
||||
return vo;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,7 @@ public class Bootstrap implements SmartInitializingSingleton {
|
||||
infoDO.setAddress(config.getBootstrapServer().trim());
|
||||
infoDO.setProperties(ConvertUtil.toJsonString(config.getProperties()));
|
||||
clusterInfoMapper.insert(infoDO);
|
||||
log.info("Insert default config: {}", infoDO);
|
||||
}
|
||||
|
||||
@Override public void afterSingletonsInstantiated() {
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.xuxd.kafka.console.controller;
|
||||
|
||||
import com.xuxd.kafka.console.beans.dto.ClusterInfoDTO;
|
||||
import com.xuxd.kafka.console.service.ClusterService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -23,4 +26,14 @@ public class ClusterController {
|
||||
public Object getClusterInfo() {
|
||||
return clusterService.getClusterInfo();
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
public Object getClusterInfoList() {
|
||||
return clusterService.getClusterInfoList();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Object addClusterInfo(@RequestBody ClusterInfoDTO dto) {
|
||||
return clusterService.addClusterInfo(dto.to());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.xuxd.kafka.console.service;
|
||||
|
||||
import com.xuxd.kafka.console.beans.ResponseData;
|
||||
import com.xuxd.kafka.console.beans.dos.ClusterInfoDO;
|
||||
|
||||
/**
|
||||
* kafka-console-ui.
|
||||
@@ -10,4 +11,8 @@ import com.xuxd.kafka.console.beans.ResponseData;
|
||||
**/
|
||||
public interface ClusterService {
|
||||
ResponseData getClusterInfo();
|
||||
|
||||
ResponseData getClusterInfoList();
|
||||
|
||||
ResponseData addClusterInfo(ClusterInfoDO infoDO);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package com.xuxd.kafka.console.service.impl;
|
||||
|
||||
import com.xuxd.kafka.console.beans.ResponseData;
|
||||
import com.xuxd.kafka.console.beans.dos.ClusterInfoDO;
|
||||
import com.xuxd.kafka.console.beans.vo.ClusterInfoVO;
|
||||
import com.xuxd.kafka.console.dao.ClusterInfoMapper;
|
||||
import com.xuxd.kafka.console.service.ClusterService;
|
||||
import java.util.stream.Collectors;
|
||||
import kafka.console.ClusterConsole;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -18,7 +22,21 @@ public class ClusterServiceImpl implements ClusterService {
|
||||
@Autowired
|
||||
private ClusterConsole clusterConsole;
|
||||
|
||||
@Autowired
|
||||
private ClusterInfoMapper clusterInfoMapper;
|
||||
|
||||
@Override public ResponseData getClusterInfo() {
|
||||
return ResponseData.create().data(clusterConsole.clusterInfo()).success();
|
||||
}
|
||||
|
||||
@Override public ResponseData getClusterInfoList() {
|
||||
return ResponseData.create().data(clusterInfoMapper.selectList(null)
|
||||
.stream().map(ClusterInfoVO::from).collect(Collectors.toList())).success();
|
||||
}
|
||||
|
||||
@Override public ResponseData addClusterInfo(ClusterInfoDO infoDO) {
|
||||
clusterInfoMapper.insert(infoDO);
|
||||
return ResponseData.create().success();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.xuxd.kafka.console.utils;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -53,4 +57,36 @@ public class ConvertUtil {
|
||||
public static Properties toProperties(String jsonStr) {
|
||||
return GsonUtil.INSTANCE.get().fromJson(jsonStr, Properties.class);
|
||||
}
|
||||
|
||||
public static String jsonStr2PropertiesStr(String jsonStr) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Map<String, Object> map = GsonUtil.INSTANCE.get().fromJson(jsonStr, Map.class);
|
||||
map.keySet().forEach(k -> {
|
||||
sb.append(k).append("=").append(map.get(k).toString()).append(System.lineSeparator());
|
||||
});
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static List<String> jsonStr2List(String jsonStr) {
|
||||
List<String> res = new LinkedList<>();
|
||||
Map<String, Object> map = GsonUtil.INSTANCE.get().fromJson(jsonStr, Map.class);
|
||||
map.forEach((k, v) -> {
|
||||
res.add(k + "=" + v);
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static String propertiesStr2JsonStr(String propertiesStr) {
|
||||
String res = "{}";
|
||||
try (ByteArrayInputStream baos = new ByteArrayInputStream(propertiesStr.getBytes())) {
|
||||
Properties properties = new Properties();
|
||||
properties.load(baos);
|
||||
res = toJsonString(properties);
|
||||
} catch (IOException e) {
|
||||
log.error("propertiesStr2JsonStr error.", e);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user