| | |
| | | |
| | | import com.alibaba.cloud.nacos.NacosDiscoveryProperties; |
| | | import com.alibaba.cloud.nacos.NacosServiceManager; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.nacos.api.exception.NacosException; |
| | | import com.alibaba.nacos.api.naming.NamingService; |
| | | import com.alibaba.nacos.api.naming.pojo.Instance; |
| | | import com.alibaba.nacos.api.naming.pojo.ServiceInfo; |
| | | import com.vci.ubcs.common.constant.LauncherConstant; |
| | | import com.vci.ubcs.omd.dto.BtmAndLinkTypeDdlDTO; |
| | | import com.vci.ubcs.omd.dto.TableAddColumnDTO; |
| | | import com.vci.ubcs.omd.dto.TableCheckDTO; |
| | | import com.vci.ubcs.omd.dto.TableCheckResultDTO; |
| | | import com.vci.ubcs.omd.vo.BtmTypeVO; |
| | | import com.vci.ubcs.omd.vo.DomainVO; |
| | | import com.vci.ubcs.omd.vo.LinkTypeVO; |
| | | import com.vci.ubcs.starter.web.enumpck.NewAppConstantEnum; |
| | | import org.springblade.core.launch.constant.AppConstant; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.core.tool.utils.BeanUtil; |
| | | import org.springblade.core.tool.utils.StringPool; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.http.*; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.client.HttpClientErrorException; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | |
| | | public static final String API_CHECK_DIFFERENT_REFLEX = API_PREFIX + StringPool.SLASH + "checkDifferentAndReflex"; |
| | | |
| | | /** |
| | | * 检查数据表是否有数据,没有按需求删除 |
| | | */ |
| | | public static final String API_CHECK_TABLE_HAS_DATA_THEN_DELETE = API_PREFIX + StringPool.SLASH + "checkTableHasDataThenDelete"; |
| | | |
| | | /** |
| | | * 数据表添加字段 |
| | | */ |
| | | public static final String API_TABLE_ADD_COLUMN = API_PREFIX + StringPool.SLASH + "addColumnForTable"; |
| | | |
| | | /** |
| | | * 用于构建静态服务类 |
| | | */ |
| | | public static DomainRepeater domainRepeater; |
| | |
| | | * Nacos服务列表 |
| | | */ |
| | | private static NamingService namingService; |
| | | |
| | | @Autowired |
| | | private NacosServiceManager manager; |
| | | |
| | | @Autowired |
| | | private NacosDiscoveryProperties properties; |
| | | |
| | | |
| | | /** |
| | | * 拿取服务中的所有表信息 |
| | | * @param serviceName 服务名称 |
| | | * @return 返回结果 |
| | | * @throws NacosException 找不到服务时抛出异常 |
| | | */ |
| | | public static R getFromTable(String serviceName) throws NacosException{ |
| | | ResponseEntity<R> responseEntity = executeGet(getUrl(serviceName, API_GET_ALL_TABLE_INFO), new HashMap<>()); |
| | | if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { |
| | | R body = Objects.requireNonNull(responseEntity.getBody()); |
| | | return body; |
| | | } |
| | | return R.fail(String.valueOf(responseEntity.getStatusCode().value())); |
| | | } |
| | | |
| | | /** |
| | | * 检查数据表是否存在数据,如果不存在则删除 |
| | | * @param checkList 需要检查的表集合 |
| | | * @return 检查结果 |
| | | * @throws NacosException 找不到服务的时候会抛出异常 |
| | | */ |
| | | public static R checkTableHasDataByTableNameThenDrop(List<TableCheckDTO> checkList) throws NacosException { |
| | | Map<String, List<TableCheckDTO>> serviceMap = checkList.stream().collect(Collectors.groupingBy(TableCheckDTO::getDomain)); |
| | | List<TableCheckResultDTO> result = new ArrayList<>(); |
| | | for (String key : serviceMap.keySet()) { |
| | | String url = getUrl(key, API_CHECK_TABLE_HAS_DATA_THEN_DELETE); |
| | | ResponseEntity<R> responseEntity = executePost(url, JSONObject.toJSONString(serviceMap.get(key)), new HashMap<>(16)); |
| | | if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { |
| | | R r = responseEntity.getBody(); |
| | | if (r.getData() instanceof List){ |
| | | List dataList = (List) r.getData(); |
| | | dataList.forEach(data -> { |
| | | TableCheckResultDTO dto = BeanUtil.copy(JSON.parseObject(JSON.toJSONString(data)), TableCheckResultDTO.class); |
| | | result.add(dto); |
| | | }); |
| | | } |
| | | } |
| | | } |
| | | return R.data(result); |
| | | } |
| | | |
| | | /** |
| | | * 为表添加字段 |
| | | * @param addColumnDTO 添加的对象 |
| | | * @param serviceName 服务名 |
| | | * @return 执行结果 |
| | | * @throws NacosException 找不到服务时抛出异常 |
| | | */ |
| | | public static R addColumnForTable(TableAddColumnDTO addColumnDTO, String serviceName) throws NacosException { |
| | | try { |
| | | String url = getUrl(serviceName,API_TABLE_ADD_COLUMN); |
| | | ResponseEntity<R> responseEntity = executePost(url, JSONObject.toJSONString(addColumnDTO), null); |
| | | if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { |
| | | R body = Objects.requireNonNull(responseEntity.getBody()); |
| | | return body; |
| | | } |
| | | return R.fail(String.valueOf(responseEntity.getStatusCode().value())); |
| | | }catch (HttpClientErrorException e) { |
| | | if (HttpStatus.NOT_FOUND.equals(e.getStatusCode())) { |
| | | R fail = R.fail("调用服务失败"); |
| | | fail.setCode(HttpStatus.NOT_FOUND.value()); |
| | | return fail; |
| | | } |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | return R.fail("未知错误"); |
| | | } |
| | | |
| | | @PostConstruct |
| | | private void init() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 执行httpGet接口 |
| | | * |
| | | * @param url 地址 |
| | | * @param extraHeaders 请求头参数 |
| | | * @return 执行结果 |
| | | */ |
| | | private static ResponseEntity<R> executeGet(String url,Map<String, String> extraHeaders) { |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | HttpHeaders httpHeaders = new HttpHeaders(); |
| | | httpHeaders.add("Blade-Auth", AuthUtil.getHeader()); |
| | | HttpEntity<String> request = new HttpEntity<>(httpHeaders); |
| | | if (!CollectionUtils.isEmpty(extraHeaders)){ |
| | | StringBuilder sb = new StringBuilder().append("?"); |
| | | extraHeaders.forEach( (k,v) -> { |
| | | sb.append(k).append(StringPool.EQUALS).append(v).append(StringPool.AMPERSAND); |
| | | }); |
| | | String str = sb.toString(); |
| | | if (str.endsWith(StringPool.AMPERSAND)){ |
| | | str = str.substring(0,str.length()-1); |
| | | } |
| | | url = url + str; |
| | | } |
| | | return restTemplate.exchange(url, HttpMethod.GET,request,R.class,""); |
| | | } |
| | | |
| | | /** |
| | | * 获取所有已注册的服务 |
| | | * @return 不包含默认服务的其他服务 |
| | | * @throws NacosException |
| | | */ |
| | | public static List<String> getDomain() throws NacosException { |
| | | List<ServiceInfo> services = namingService.getSubscribeServices(); |
| | | Set<String> serviceNames = new HashSet<>(); |
| | | serviceNames.add(AppConstant.APPLICATION_GATEWAY_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_ADMIN_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_AUTH_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_DESK_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_DEVELOP_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_FLOW_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_FLOWDESIGN_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_LOG_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_REPORT_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_SWAGGER_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_SYSTEM_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_TURBINE_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_USER_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_WEBSOCKET_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_ZIPKIN_NAME); |
| | | serviceNames.add(AppConstant.APPLICATION_NAME_OMD); |
| | | return services.stream().map(ServiceInfo::getName).filter(s -> !serviceNames.contains(s)).collect(Collectors.toList()); |
| | | public static List<DomainVO> getDomain() throws NacosException { |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | String url = "http://" +LauncherConstant.NACOS_DEV_ADDR + "/nacos/v1/ns/service/list?pageNo=1&pageSize=50"; |
| | | String forObject = restTemplate.getForObject(url, String.class); |
| | | JSONObject jsonObject = JSON.parseObject(forObject); |
| | | List<String> doms = (List<String>) jsonObject.get("doms"); |
| | | NewAppConstantEnum[] apps = NewAppConstantEnum.values(); |
| | | return Arrays.stream(apps).filter(app -> doms.stream().anyMatch(s -> StringUtil.equals(app.getName(), s))).map(app -> { |
| | | DomainVO domain = new DomainVO(); |
| | | domain.setValue(app.getName()); |
| | | domain.setLabel(app.getText()); |
| | | return domain; |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | } |