Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
peacewong committed Nov 16, 2023
1 parent f094821 commit fa3011f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public enum LinkisRpcErrorCodeSummary implements LinkisErrorCode {
10004, "The corresponding anti-sequence class:{0} failed to initialize(对应的反序列类:{0} 初始化失败)"),
APPLICATION_IS_NOT_EXISTS(
10051, "The instance:{0} of application {1} does not exist(应用程序:{0} 的实例:{1} 不存在)."),

INSTANCE_ERROR(10052, "The instance:{0} is error should ip:port."),
RPC_INIT_ERROR(10054, "Asyn RPC Consumer Thread has stopped!(Asyn RPC Consumer 线程已停止!)");

/** 错误码 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.apache.linkis.gateway.springcloud.loadbalancer;

import org.apache.linkis.gateway.springcloud.constant.GatewayConstant;
import org.apache.linkis.rpc.errorcode.LinkisRpcErrorCodeSummary;
import org.apache.linkis.rpc.exception.NoInstanceExistsException;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -32,6 +34,7 @@
import org.springframework.cloud.loadbalancer.core.SelectedInstanceCallback;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;

import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;
import java.util.Random;
Expand Down Expand Up @@ -94,25 +97,37 @@ private Response<ServiceInstance> processInstanceResponse(
private Response<ServiceInstance> getInstanceResponse(
List<ServiceInstance> instances, String clientIp) {
if (instances.isEmpty()) {
if (log.isWarnEnabled()) {
log.warn("No servers available for service: " + serviceId);
}
log.warn("No servers available for service: " + serviceId);
return new EmptyResponse();
}
int pos = this.position.incrementAndGet() & Integer.MAX_VALUE;

if (StringUtils.isEmpty(clientIp)) {
if (StringUtils.isBlank(clientIp)) {
return new DefaultResponse(instances.get(pos % instances.size()));
}
String[] ipAndPort = clientIp.split(":");
if (ipAndPort.length != 2) {
throw new NoInstanceExistsException(
LinkisRpcErrorCodeSummary.INSTANCE_ERROR.getErrorCode(),
MessageFormat.format(LinkisRpcErrorCodeSummary.INSTANCE_ERROR.getErrorDesc(), clientIp));
}
ServiceInstance chooseInstance = null;
for (ServiceInstance instance : instances) {
String[] ipAndPort = clientIp.split(":");
if (ipAndPort.length == 2
&& Objects.equals(ipAndPort[0], instance.getHost())
if (Objects.equals(ipAndPort[0], instance.getHost())
&& Objects.equals(ipAndPort[1], instance.getPort())) {
return new DefaultResponse(instance);
chooseInstance = instance;
break;
}
}

return new DefaultResponse(instances.get(pos % instances.size()));
if (null == chooseInstance) {
throw new NoInstanceExistsException(
LinkisRpcErrorCodeSummary.APPLICATION_IS_NOT_EXISTS.getErrorCode(),
MessageFormat.format(
LinkisRpcErrorCodeSummary.APPLICATION_IS_NOT_EXISTS.getErrorDesc(),
clientIp,
serviceId));
} else {
return new DefaultResponse(chooseInstance);
}
}
}

0 comments on commit fa3011f

Please sign in to comment.