commit
a1f1a4565b
|
|
@ -31,5 +31,10 @@
|
|||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
package com.ruoyi.common.loadbalance.constant;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* 常量类
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
public class LoadBalancerConstant {
|
||||
|
||||
/**
|
||||
* 获取服务host
|
||||
* 默认自动获取
|
||||
*/
|
||||
public static String getHost() {
|
||||
String host = "127.0.0.1";
|
||||
try {
|
||||
// 如需自定义ip可修改此处
|
||||
String address = InetAddress.getLocalHost().getHostAddress();
|
||||
if (address != null) {
|
||||
host = address;
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.debug("[LoadBalancer] - 本机IP地址: {}", host);
|
||||
return host;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package com.ruoyi.common.loadbalance.core;
|
||||
|
||||
import com.ruoyi.common.loadbalance.constant.LoadBalancerConstant;
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.dubbo.common.URL;
|
||||
import org.apache.dubbo.rpc.Invocation;
|
||||
|
|
@ -21,7 +21,7 @@ public class CustomDubboLoadBalancer extends AbstractLoadBalance {
|
|||
@Override
|
||||
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
|
||||
for (Invoker<T> invoker : invokers) {
|
||||
if (invoker.getUrl().getHost().equals(LoadBalancerConstant.getHost())) {
|
||||
if (NetUtil.localIpv4s().contains(invoker.getUrl().getHost())) {
|
||||
return invoker;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.ruoyi.common.loadbalance.core;
|
||||
|
||||
import com.ruoyi.common.loadbalance.constant.LoadBalancerConstant;
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
|
|
@ -54,7 +54,7 @@ public class CustomSpringCloudLoadBalancer implements ReactorServiceInstanceLoad
|
|||
return new EmptyResponse();
|
||||
}
|
||||
for (ServiceInstance instance : instances) {
|
||||
if (instance.getHost().equals(LoadBalancerConstant.getHost())) {
|
||||
if (NetUtil.localIpv4s().contains(instance.getHost())) {
|
||||
return new DefaultResponse(instance);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue