Skip to content

Commit

Permalink
Merge pull request #1331 from luanwenfei-venus/develop_issue_1329_fix
Browse files Browse the repository at this point in the history
针对Servicecomb注册截断超过64位长度的hostname
  • Loading branch information
Sherlockhan authored Oct 17, 2023
2 parents 317d2ea + ed0f935 commit 6b19b6b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public class RegistryServiceImpl implements RegistryService {

private static final int REGISTRATION_WAITE_TIME = 30;

private static final int MAX_HOST_NAME_LENGTH = 64;

private static final List<Subscription> PENDING_SUBSCRIBE_EVENT = new CopyOnWriteArrayList<>();

private static final AtomicBoolean SHUTDOWN = new AtomicBoolean();
Expand Down Expand Up @@ -394,7 +396,11 @@ private void createMicroserviceInstance() {

private String getHost() {
try {
return InetAddress.getLocalHost().getHostName();
String hostName = InetAddress.getLocalHost().getHostName();

// ServiceComb不支持64个字符以上的hostname注册, 此处参考spring-cloud-huawei进行截断处理
return hostName != null && hostName.length() > MAX_HOST_NAME_LENGTH ? hostName.substring(0,
MAX_HOST_NAME_LENGTH) : hostName;
} catch (UnknownHostException e) {
LOGGER.warning("Cannot get the host.");
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public class ScClient {

private static final int FLAG = -1;

private static final int MAX_HOST_NAME_LENGTH = 64;

private final AtomicBoolean isDelayed = new AtomicBoolean();

private ServiceCenterConfiguration serviceCenterConfiguration;
Expand Down Expand Up @@ -150,7 +152,7 @@ public void deRegister() {
}
if (serviceCenterClient != null) {
serviceCenterClient.deleteMicroserviceInstance(this.microservice.getServiceId(),
this.microserviceInstance.getInstanceId());
this.microserviceInstance.getInstanceId());
}
}

Expand All @@ -161,8 +163,7 @@ public void deRegister() {
*/
public String getRegisterCenterStatus() {
try {
final MicroserviceInstancesResponse response =
serviceCenterClient.getServiceCenterInstances();
final MicroserviceInstancesResponse response = serviceCenterClient.getServiceCenterInstances();
if (response == null || response.getInstances() == null) {
return Register.DOWN;
}
Expand All @@ -180,15 +181,14 @@ public String getRegisterCenterStatus() {
*/
public String getInstanceStatus() {
try {
MicroserviceInstance instance = serviceCenterClient
.getMicroserviceInstance(microserviceInstance.getServiceId(),
microserviceInstance.getInstanceId());
MicroserviceInstance instance = serviceCenterClient.getMicroserviceInstance(
microserviceInstance.getServiceId(), microserviceInstance.getInstanceId());
final String status = instance.getStatus() != null ? instance.getStatus().name() : Register.UN_KNOWN;
RegisterContext.INSTANCE.getClientInfo().setStatus(status);
return status;
} catch (OperationException ex) {
LOGGER.severe(String.format(Locale.ENGLISH, "[RegistryPlugin] failed to get instance status, %s",
ex.getMessage()));
ex.getMessage()));
}
return Register.DOWN;
}
Expand All @@ -201,11 +201,11 @@ public String getInstanceStatus() {
public void updateInstanceStatus(String status) {
try {
serviceCenterClient.updateMicroserviceInstanceStatus(microserviceInstance.getServiceId(),
microserviceInstance.getInstanceId(), MicroserviceInstanceStatus.valueOf(status));
microserviceInstance.getInstanceId(), MicroserviceInstanceStatus.valueOf(status));
RegisterContext.INSTANCE.getClientInfo().setStatus(status);
} catch (OperationException ex) {
LOGGER.severe(String.format(Locale.ENGLISH, "[RegistryPlugin] Updated instance status to %s failed",
status));
LOGGER.severe(
String.format(Locale.ENGLISH, "[RegistryPlugin] Updated instance status to %s failed", status));
}
}

Expand All @@ -225,8 +225,8 @@ public List<MicroserviceInstance> queryInstancesByServiceId(String serviceName)
}
} catch (OperationException ex) {
LOGGER.severe(String.format(Locale.ENGLISH,
"Queried service [%s] instance list from service center failed, reason [%s]", serviceName,
ex.getMessage()));
"Queried service [%s] instance list from service center failed, reason [%s]", serviceName,
ex.getMessage()));
}
if (instances == null) {
return Collections.emptyList();
Expand All @@ -243,14 +243,13 @@ private List<MicroserviceInstance> queryAllAppInstances(String serviceName) {
return Collections.emptyList();
}
final List<Microservice> allServices = response.getServices();
final List<String> allServiceIds =
allServices.stream().filter(service -> StringUtils.equals(service.getServiceName(),
getRealServiceName(true, serviceName)))
final List<String> allServiceIds = allServices.stream()
.filter(service -> StringUtils.equals(service.getServiceName(), getRealServiceName(true, serviceName)))
.map(Microservice::getServiceId).distinct().collect(Collectors.toList());
List<MicroserviceInstance> microserviceInstances = new ArrayList<>();
allServiceIds.forEach(serviceId -> {
final MicroserviceInstancesResponse instanceResponse =
serviceCenterClient.getMicroserviceInstanceList(serviceId);
final MicroserviceInstancesResponse instanceResponse = serviceCenterClient.getMicroserviceInstanceList(
serviceId);
if (instanceResponse != null && instanceResponse.getInstances() != null) {
microserviceInstances.addAll(instanceResponse.getInstances());
}
Expand Down Expand Up @@ -302,8 +301,8 @@ private List<MicroserviceInstance> zoneAwareFilter(List<MicroserviceInstance> in
private boolean regionAndAzMatch(MicroserviceInstance myself, MicroserviceInstance target) {
if (myself.getDataCenterInfo() != null && target.getDataCenterInfo() != null) {
return myself.getDataCenterInfo().getRegion().equals(target.getDataCenterInfo().getRegion())
&& myself.getDataCenterInfo().getAvailableZone()
.equals(target.getDataCenterInfo().getAvailableZone());
&& myself.getDataCenterInfo().getAvailableZone()
.equals(target.getDataCenterInfo().getAvailableZone());
}
return false;
}
Expand Down Expand Up @@ -388,8 +387,8 @@ private void startServiceCenterRegistration() {
if (serviceCenterClient == null) {
return;
}
serviceCenterRegistration =
new ServiceCenterRegistration(serviceCenterClient, serviceCenterConfiguration, EVENT_BUS);
serviceCenterRegistration = new ServiceCenterRegistration(serviceCenterClient, serviceCenterConfiguration,
EVENT_BUS);
EVENT_BUS.register(this);
serviceCenterRegistration.setMicroservice(buildMicroService());
buildMicroServiceInstance();
Expand All @@ -407,18 +406,21 @@ private void initScClientAndWatch() {
final AddressManager addressManager = createAddressManager(registerConfig.getProject(), getScUrls());
final SSLProperties sslProperties = createSslProperties(registerConfig.isSslEnabled());
serviceCenterClient = new ServiceCenterClient(addressManager, sslProperties,
signRequest -> Collections.emptyMap(), "default", Collections.emptyMap());
signRequest -> Collections.emptyMap(), "default", Collections.emptyMap());
}

private List<String> buildEndpoints() {
return Collections.singletonList(String.format(Locale.ENGLISH, "rest://%s:%d", HostUtils.getMachineIp(),
RegisterContext.INSTANCE.getClientInfo().getPort()));
RegisterContext.INSTANCE.getClientInfo().getPort()));
}

private void buildMicroServiceInstance() {
microserviceInstance = new MicroserviceInstance();
microserviceInstance.setStatus(MicroserviceInstanceStatus.UP);
microserviceInstance.setHostName(RegisterContext.INSTANCE.getClientInfo().getHost());
String hostName = RegisterContext.INSTANCE.getClientInfo().getHost();
microserviceInstance.setHostName(
hostName != null && hostName.length() > MAX_HOST_NAME_LENGTH ? hostName.substring(0,
MAX_HOST_NAME_LENGTH) : hostName);
microserviceInstance.setEndpoints(buildEndpoints());
HealthCheck healthCheck = new HealthCheck();
healthCheck.setMode(HealthCheckMode.push);
Expand Down Expand Up @@ -459,7 +461,7 @@ private Microservice buildMicroService() {
microservice = new Microservice();
if (registerConfig.isAllowCrossApp()) {
microservice.setAlias(registerConfig.getApplication() + ConfigConstants.APP_SERVICE_SEPARATOR
+ RegisterContext.INSTANCE.getClientInfo().getServiceId());
+ RegisterContext.INSTANCE.getClientInfo().getServiceId());
}
microservice.setAppId(registerConfig.getApplication());
microservice.setEnvironment(registerConfig.getEnvironment());
Expand Down

0 comments on commit 6b19b6b

Please sign in to comment.