Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nacos to nacos support all groups sync #344

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ public class SkyWalkerConstants {
public static final String SYNC_INSTANCE_TAG="sync.instance.tag";
public static final Integer MAX_THREAD_NUM = 200;

public static final String ALL = "ALL";

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ private List<Predicate> getPredicates(Root<ClusterDO> root, CriteriaBuilder crit
public int findClusterLevel(String sourceClusterId){
ClusterDO clusterDO = clusterRepository.findByClusterId(sourceClusterId);
if (clusterDO != null) {
return clusterDO.getClusterLevel();
Integer level = clusterDO.getClusterLevel();
if (null == level) {
return 0;
}
return level;
}
return -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alibaba.nacossync.dao;

import com.alibaba.nacossync.constant.SkyWalkerConstants;
import com.alibaba.nacossync.pojo.QueryCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -116,7 +117,7 @@ private Page<TaskDO> getTaskDOS(QueryCondition queryCondition, Pageable pageable
}

public List<TaskDO> findServiceNameIsNull() {
return taskRepository.findAllByServiceNameEquals("ALL");
return taskRepository.findAllByServiceNameEquals(SkyWalkerConstants.ALL);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 com.alibaba.nacossync.pojo.request;

import com.alibaba.nacossync.pojo.view.ServiceView;
import lombok.Data;

import java.util.List;

/**
* @author NacosSync
* @since 2024-01-04 15:54:20
*/
@Data
public class CatalogServiceResult {
/**
* count,not equal serviceList.size .
*/
private int count;

private List<ServiceView> serviceList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 com.alibaba.nacossync.pojo.view;

import lombok.Data;

/**
* @author NacosSync
* @since 2024-01-04 15:54:36
*/
@Data
public class ServiceView {
private String name;

private String groupName;

private int clusterCount;

private int ipCount;

private int healthyInstanceCount;

private String triggerFlag;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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 com.alibaba.nacossync.service;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.client.naming.NacosNamingService;
import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.common.utils.HttpMethod;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacossync.pojo.request.CatalogServiceResult;
import org.springframework.util.ReflectionUtils;

import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.alibaba.nacossync.constant.SkyWalkerConstants.GROUP_NAME_PARAM;
import static com.alibaba.nacossync.constant.SkyWalkerConstants.PAGE_NO;
import static com.alibaba.nacossync.constant.SkyWalkerConstants.PAGE_SIZE;
import static com.alibaba.nacossync.constant.SkyWalkerConstants.SERVICE_NAME_PARAM;

/**
* @author NacosSync
* @since 2024-01-04 15:53:40
*/
public class NacosEnhanceNamingService {


protected NamingService delegate;

protected NamingProxy serverProxy;

public NacosEnhanceNamingService(NamingService namingService) {
if (!(namingService instanceof NacosNamingService)) {
throw new IllegalArgumentException(
"namingService only support instance of com.alibaba.nacos.client.naming.NacosNamingService.");
}
this.delegate = namingService;

// serverProxy
final Field serverProxyField = ReflectionUtils.findField(NacosNamingService.class, "serverProxy");
assert serverProxyField != null;
ReflectionUtils.makeAccessible(serverProxyField);
this.serverProxy = (NamingProxy) ReflectionUtils.getField(serverProxyField, delegate);
}

public CatalogServiceResult catalogServices(@Nullable String serviceName, @Nullable String group)
throws NacosException {
int pageNo = 1; // start with 1
int pageSize = 100;

final CatalogServiceResult result = catalogServices(serviceName, group, pageNo, pageSize);

CatalogServiceResult tmpResult = result;

while (Objects.nonNull(tmpResult) && tmpResult.getServiceList().size() >= pageSize) {
pageNo++;
tmpResult = catalogServices(serviceName, group, pageNo, pageSize);

if (tmpResult != null) {
result.getServiceList().addAll(tmpResult.getServiceList());
}
}

return result;
}

/**
* @see com.alibaba.nacos.client.naming.core.HostReactor#getServiceInfoDirectlyFromServer(String, String)
*/
public CatalogServiceResult catalogServices(@Nullable String serviceName, @Nullable String group, int pageNo,
int pageSize) throws NacosException {

// pageNo
// pageSize
// serviceNameParam
// groupNameParam
final Map<String, String> params = new HashMap<>(8);
params.put(CommonParams.NAMESPACE_ID, serverProxy.getNamespaceId());
params.put(SERVICE_NAME_PARAM, serviceName);
params.put(GROUP_NAME_PARAM, group);
params.put(PAGE_NO, String.valueOf(pageNo));
params.put(PAGE_SIZE, String.valueOf(pageSize));

final String result = this.serverProxy.reqApi(UtilAndComs.nacosUrlBase + "/catalog/services", params,
HttpMethod.GET);
if (StringUtils.isNotEmpty(result)) {
return JacksonUtils.toObj(result, CatalogServiceResult.class);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@

package com.alibaba.nacossync.template.processor;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.client.naming.NacosNamingService;
import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.common.utils.HttpMethod;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacossync.constant.TaskStatusEnum;
import com.alibaba.nacossync.dao.ClusterAccessService;
import com.alibaba.nacossync.dao.TaskAccessService;
Expand All @@ -34,28 +26,19 @@
import com.alibaba.nacossync.extension.holder.NacosServerHolder;
import com.alibaba.nacossync.pojo.model.ClusterDO;
import com.alibaba.nacossync.pojo.model.TaskDO;
import com.alibaba.nacossync.pojo.request.CatalogServiceResult;
import com.alibaba.nacossync.pojo.request.TaskAddAllRequest;
import com.alibaba.nacossync.pojo.request.TaskAddRequest;
import com.alibaba.nacossync.pojo.result.TaskAddResult;
import com.alibaba.nacossync.pojo.view.ServiceView;
import com.alibaba.nacossync.service.NacosEnhanceNamingService;
import com.alibaba.nacossync.template.Processor;
import com.alibaba.nacossync.util.SkyWalkerUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;

import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.alibaba.nacossync.constant.SkyWalkerConstants.GROUP_NAME_PARAM;
import static com.alibaba.nacossync.constant.SkyWalkerConstants.PAGE_NO;
import static com.alibaba.nacossync.constant.SkyWalkerConstants.PAGE_SIZE;
import static com.alibaba.nacossync.constant.SkyWalkerConstants.SERVICE_NAME_PARAM;

/**
* @author NacosSync
* @version $Id: TaskAddAllProcessor.java, v 0.1 2022-03-23 PM11:40 NacosSync Exp $$
Expand Down Expand Up @@ -103,7 +86,7 @@ public void process(TaskAddAllRequest addAllRequest, TaskAddResult taskAddResult
throw new SkyWalkerException("only support sync type that the source of the Nacos.");
}

final EnhanceNamingService enhanceNamingService = new EnhanceNamingService(sourceNamingService);
final NacosEnhanceNamingService enhanceNamingService = new NacosEnhanceNamingService(sourceNamingService);
final CatalogServiceResult catalogServiceResult = enhanceNamingService.catalogServices(null, null);
if (catalogServiceResult == null || catalogServiceResult.getCount() <= 0) {
throw new SkyWalkerException("sourceCluster data empty");
Expand Down Expand Up @@ -146,105 +129,7 @@ private void dealTask(TaskAddAllRequest addAllRequest, TaskAddRequest taskAddReq
}
taskAccessService.addTask(taskDO);
}

static class EnhanceNamingService {

protected NamingService delegate;

protected NamingProxy serverProxy;

protected EnhanceNamingService(NamingService namingService) {
if (!(namingService instanceof NacosNamingService)) {
throw new IllegalArgumentException(
"namingService only support instance of com.alibaba.nacos.client.naming.NacosNamingService.");
}
this.delegate = namingService;

// serverProxy
final Field serverProxyField = ReflectionUtils.findField(NacosNamingService.class, "serverProxy");
assert serverProxyField != null;
ReflectionUtils.makeAccessible(serverProxyField);
this.serverProxy = (NamingProxy) ReflectionUtils.getField(serverProxyField, delegate);
}

public CatalogServiceResult catalogServices(@Nullable String serviceName, @Nullable String group)
throws NacosException {
int pageNo = 1; // start with 1
int pageSize = 100;

final CatalogServiceResult result = catalogServices(serviceName, group, pageNo, pageSize);

CatalogServiceResult tmpResult = result;

while (Objects.nonNull(tmpResult) && tmpResult.serviceList.size() >= pageSize) {
pageNo++;
tmpResult = catalogServices(serviceName, group, pageNo, pageSize);

if (tmpResult != null) {
result.serviceList.addAll(tmpResult.serviceList);
}
}

return result;
}

/**
* @see com.alibaba.nacos.client.naming.core.HostReactor#getServiceInfoDirectlyFromServer(String, String)
*/
public CatalogServiceResult catalogServices(@Nullable String serviceName, @Nullable String group, int pageNo,
int pageSize) throws NacosException {

// pageNo
// pageSize
// serviceNameParam
// groupNameParam
final Map<String, String> params = new HashMap<>(8);
params.put(CommonParams.NAMESPACE_ID, serverProxy.getNamespaceId());
params.put(SERVICE_NAME_PARAM, serviceName);
params.put(GROUP_NAME_PARAM, group);
params.put(PAGE_NO, String.valueOf(pageNo));
params.put(PAGE_SIZE, String.valueOf(pageSize));

final String result = this.serverProxy.reqApi(UtilAndComs.nacosUrlBase + "/catalog/services", params,
HttpMethod.GET);
if (StringUtils.isNotEmpty(result)) {
return JacksonUtils.toObj(result, CatalogServiceResult.class);
}
return null;
}

}

/**
* Copy from Nacos Server.
*/
@Data
static class ServiceView {

private String name;

private String groupName;

private int clusterCount;

private int ipCount;

private int healthyInstanceCount;

private String triggerFlag;

}

@Data
static class CatalogServiceResult {

/**
* count,not equal serviceList.size .
*/
private int count;

private List<ServiceView> serviceList;

}



}
Loading