forked from Linyuzai/concept
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tanghanzheng
committed
Jun 23, 2022
1 parent
9aa64f9
commit bf8bb7a
Showing
10 changed files
with
187 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
.../src/main/java/com/github/linyuzai/router/ribbon/v1/RouterLoadBalancerClientFilterV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.github.linyuzai.router.ribbon.v1; | ||
|
||
import com.github.linyuzai.router.core.concept.RouterConcept; | ||
import com.github.linyuzai.router.ribbon.RouterLoadBalancerClient; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; | ||
import org.springframework.cloud.gateway.filter.GatewayFilterChain; | ||
import org.springframework.cloud.gateway.filter.GlobalFilter; | ||
import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter; | ||
import org.springframework.cloud.gateway.support.NotFoundException; | ||
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.net.URI; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; | ||
|
||
public class RouterLoadBalancerClientFilterV1 implements GlobalFilter, Ordered { | ||
private static final Log log = LogFactory.getLog(LoadBalancerClientFilter.class); | ||
public static final int LOAD_BALANCER_CLIENT_FILTER_ORDER = 10100; | ||
private final LoadBalancerClient loadBalancer; | ||
|
||
private final ApplicationContext context; | ||
|
||
private final RouterConcept concept; | ||
|
||
public RouterLoadBalancerClientFilterV1(ApplicationContext context, RouterConcept concept) { | ||
this.loadBalancer = context.getBean(LoadBalancerClient.class); | ||
this.context = context; | ||
this.concept = concept; | ||
} | ||
|
||
public int getOrder() { | ||
return LOAD_BALANCER_CLIENT_FILTER_ORDER; | ||
} | ||
|
||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | ||
URI url = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR); | ||
String schemePrefix = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR); | ||
if (url == null || !"lb".equals(url.getScheme()) && !"lb".equals(schemePrefix)) { | ||
return chain.filter(exchange); | ||
} else { | ||
ServerWebExchangeUtils.addOriginalRequestUrl(exchange, url); | ||
log.trace("LoadBalancerClientFilter url before: " + url); | ||
ServiceInstance instance = choose(exchange); | ||
if (instance == null) { | ||
throw new NotFoundException("Unable to find instance for " + url.getHost()); | ||
} else { | ||
URI uri = exchange.getRequest().getURI(); | ||
String overrideScheme = null; | ||
if (schemePrefix != null) { | ||
overrideScheme = url.getScheme(); | ||
} | ||
|
||
URI requestUrl = this.loadBalancer.reconstructURI(new DelegatingServiceInstance(instance, overrideScheme), uri); | ||
log.trace("LoadBalancerClientFilter url chosen: " + requestUrl); | ||
exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, requestUrl); | ||
return chain.filter(exchange); | ||
} | ||
} | ||
} | ||
|
||
protected ServiceInstance choose(ServerWebExchange exchange) { | ||
URI uri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR); | ||
String serviceId = Objects.requireNonNull(uri).getHost(); | ||
return getRouterLoadBalancerClient().choose(serviceId, exchange); | ||
} | ||
|
||
protected RouterLoadBalancerClient getRouterLoadBalancerClient() { | ||
if (loadBalancer instanceof RouterLoadBalancerClient) { | ||
return (RouterLoadBalancerClient) loadBalancer; | ||
} | ||
return new RouterLoadBalancerClient(context, loadBalancer, concept); | ||
} | ||
|
||
class DelegatingServiceInstance implements ServiceInstance { | ||
final ServiceInstance delegate; | ||
private String overrideScheme; | ||
|
||
DelegatingServiceInstance(ServiceInstance delegate, String overrideScheme) { | ||
this.delegate = delegate; | ||
this.overrideScheme = overrideScheme; | ||
} | ||
|
||
public String getServiceId() { | ||
return this.delegate.getServiceId(); | ||
} | ||
|
||
public String getHost() { | ||
return this.delegate.getHost(); | ||
} | ||
|
||
public int getPort() { | ||
return this.delegate.getPort(); | ||
} | ||
|
||
public boolean isSecure() { | ||
return this.delegate.isSecure(); | ||
} | ||
|
||
public URI getUri() { | ||
return this.delegate.getUri(); | ||
} | ||
|
||
public Map<String, String> getMetadata() { | ||
return this.delegate.getMetadata(); | ||
} | ||
|
||
public String getScheme() { | ||
String scheme = this.delegate.getScheme(); | ||
return scheme != null ? scheme : this.overrideScheme; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...rc/main/java/com/github/linyuzai/router/ribbon/v2/LoadBalancerClientFilterV2Enhancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.github.linyuzai.router.ribbon.v2; | ||
|
||
import com.github.linyuzai.router.core.concept.RouterConcept; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.lang.NonNull; | ||
|
||
@AllArgsConstructor | ||
public class LoadBalancerClientFilterV2Enhancer implements BeanPostProcessor { | ||
|
||
private final ApplicationContext context; | ||
|
||
private final RouterConcept concept; | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException { | ||
if (bean instanceof LoadBalancerClientFilter) { | ||
return new RouterLoadBalancerClientFilterV2(context, concept); | ||
} | ||
return bean; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
ext.'conceptRouterVersion' = '0.3.0' | ||
ext.'conceptRouterVersion' = '0.5.0' |