feature(forest): 使用 spring cloud loadbalance 组件来选择服务
This commit is contained in:
@@ -27,6 +27,10 @@
|
|||||||
<artifactId>forest-spring-boot-starter</artifactId>
|
<artifactId>forest-spring-boot-starter</artifactId>
|
||||||
<version>1.5.30</version>
|
<version>1.5.30</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -1,20 +1,18 @@
|
|||||||
package com.lanyuanxiaoyao.service.forest.configuration;
|
package com.lanyuanxiaoyao.service.forest.configuration;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.RandomUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.core.util.URLUtil;
|
||||||
import com.dtflys.forest.auth.BasicAuth;
|
import com.dtflys.forest.auth.BasicAuth;
|
||||||
import com.dtflys.forest.http.ForestAddress;
|
import com.dtflys.forest.http.ForestAddress;
|
||||||
import com.dtflys.forest.http.ForestRequest;
|
import com.dtflys.forest.http.ForestRequest;
|
||||||
import com.dtflys.forest.interceptor.Interceptor;
|
import com.dtflys.forest.interceptor.Interceptor;
|
||||||
|
import com.eshore.odcp.hudi.connector.Constants;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import org.eclipse.collections.api.factory.Lists;
|
|
||||||
import org.eclipse.collections.api.list.ImmutableList;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,10 +23,10 @@ import org.springframework.stereotype.Component;
|
|||||||
public class SpringCloudDiscoveryInterceptor implements Interceptor<Object> {
|
public class SpringCloudDiscoveryInterceptor implements Interceptor<Object> {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(SpringCloudDiscoveryInterceptor.class);
|
private static final Logger logger = LoggerFactory.getLogger(SpringCloudDiscoveryInterceptor.class);
|
||||||
|
|
||||||
private final DiscoveryClient client;
|
private final LoadBalancerClient loadBalancerClient;
|
||||||
|
|
||||||
public SpringCloudDiscoveryInterceptor(DiscoveryClient client) {
|
public SpringCloudDiscoveryInterceptor(LoadBalancerClient loadBalancerClient) {
|
||||||
this.client = client;
|
this.loadBalancerClient = loadBalancerClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -37,16 +35,14 @@ public class SpringCloudDiscoveryInterceptor implements Interceptor<Object> {
|
|||||||
URL url = URLUtil.url(request.getUrl());
|
URL url = URLUtil.url(request.getUrl());
|
||||||
String host = url.getHost();
|
String host = url.getHost();
|
||||||
if (StrUtil.isNotBlank(host)) {
|
if (StrUtil.isNotBlank(host)) {
|
||||||
ImmutableList<ServiceInstance> instances = Lists.immutable.ofAll(client.getInstances(host));
|
ServiceInstance instance = loadBalancerClient.choose(host);
|
||||||
if (ObjectUtil.isNotEmpty(instances)) {
|
if (ObjectUtil.isNotNull(instance)) {
|
||||||
int index = RandomUtil.randomInt(instances.size());
|
|
||||||
ServiceInstance instance = instances.get(index);
|
|
||||||
request.setAddress(new ForestAddress(instance.getScheme(), instance.getHost(), instance.getPort()));
|
request.setAddress(new ForestAddress(instance.getScheme(), instance.getHost(), instance.getPort()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Basic auth
|
// Basic auth
|
||||||
BasicAuth basicAuth = new BasicAuth("AxhEbscwsJDbYMH2", "cYxg3b4PtWoVD5SjFayWxtnSVsjzRsg4");
|
BasicAuth basicAuth = new BasicAuth(Constants.SPRING_SECURITY_USERNAME, Constants.SPRING_SECURITY_PASSWORD_PLAIN);
|
||||||
basicAuth.enhanceAuthorization(request);
|
basicAuth.enhanceAuthorization(request);
|
||||||
|
|
||||||
return Interceptor.super.beforeExecute(request);
|
return Interceptor.super.beforeExecute(request);
|
||||||
|
|||||||
Reference in New Issue
Block a user