!77 修改LoadBalancer模块,支持`多网卡`

Merge pull request !77 from Yjoioooo/dev
2.X
疯狂的狮子Li 2 years ago committed by Gitee
commit a1f1a4565b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

@ -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…
Cancel
Save