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