Skip to content

Commit

Permalink
router 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghanzheng committed Jun 23, 2022
1 parent 9aa64f9 commit bf8bb7a
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.linyuzai.router.core.concept.Router;

public class ForceUnavailableServiceRouterLocator extends ServiceRequestRouterLocator {
public class DefaultServiceRouterLocator extends ServiceRequestRouterLocator {

@Override
public Router.Location getForcedLocation() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.linyuzai.router.ribbon;
package com.github.linyuzai.router.ribbon.v1;

import com.github.linyuzai.router.core.concept.RouterConcept;
import lombok.AllArgsConstructor;
Expand All @@ -9,7 +9,7 @@
import org.springframework.lang.NonNull;

@AllArgsConstructor
public class LoadBalancerClientFilterEnhancer implements BeanPostProcessor {
public class LoadBalancerClientFilterV1Enhancer implements BeanPostProcessor {

private final ApplicationContext context;

Expand All @@ -18,7 +18,7 @@ public class LoadBalancerClientFilterEnhancer implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException {
if (bean instanceof LoadBalancerClientFilter) {
return new RouterLoadBalancerClientFilter(context, concept);
return new RouterLoadBalancerClientFilterV1(context, concept);
}
return bean;
}
Expand Down
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;
}
}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.linyuzai.router.ribbon;
package com.github.linyuzai.router.ribbon.v2;

import com.github.linyuzai.router.core.concept.RouterConcept;
import com.github.linyuzai.router.ribbon.RouterLoadBalancerClient;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
Expand All @@ -13,13 +14,13 @@

import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;

public class RouterLoadBalancerClientFilter extends LoadBalancerClientFilter {
public class RouterLoadBalancerClientFilterV2 extends LoadBalancerClientFilter {

private final ApplicationContext context;

private final RouterConcept concept;

public RouterLoadBalancerClientFilter(ApplicationContext context, RouterConcept concept) {
public RouterLoadBalancerClientFilterV2(ApplicationContext context, RouterConcept concept) {
super(context.getBean(LoadBalancerClient.class), context.getBean(LoadBalancerProperties.class));
this.context = context;
this.concept = concept;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.github.linyuzai.router.core.event.RouterEventListener;
import com.github.linyuzai.router.core.event.RouterEventPublisher;
import com.github.linyuzai.router.core.locator.RouterLocator;
import com.github.linyuzai.router.core.locator.ForceUnavailableServiceRouterLocator;
import com.github.linyuzai.router.core.locator.DefaultServiceRouterLocator;
import com.github.linyuzai.router.core.matcher.RouterMatcher;
import com.github.linyuzai.router.core.repository.InMemoryRouterRepository;
import com.github.linyuzai.router.core.repository.JacksonLocalRouterRepository;
Expand All @@ -22,7 +22,6 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -83,7 +82,7 @@ public RouterMatcher routerMatcher() {
@Bean
@ConditionalOnMissingBean
public RouterLocator routerLocator() {
return new ForceUnavailableServiceRouterLocator();
return new DefaultServiceRouterLocator();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import org.springframework.context.annotation.Configuration;

@ConditionalOnRouterEnabled
@ConditionalOnClass(LoadBalancerClientFactory.class)
@Configuration(proxyBeanMethods = false)
public class RouterLoadbalancerAutoConfiguration {

@Bean
@ConditionalOnClass(LoadBalancerClientFactory.class)
public LoadBalancerClientFactoryEnhancer loadBalancerClientFactoryEnhancer(RouterConcept concept) {
return new LoadBalancerClientFactoryEnhancer(concept);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,43 @@

import com.github.linyuzai.router.autoconfigure.annotation.ConditionalOnRouterEnabled;
import com.github.linyuzai.router.core.concept.RouterConcept;
import com.github.linyuzai.router.ribbon.LoadBalancerClientFilterEnhancer;
import com.github.linyuzai.router.ribbon.v1.LoadBalancerClientFilterV1Enhancer;
import com.github.linyuzai.router.ribbon.v2.LoadBalancerClientFilterV2Enhancer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ConditionalOnRouterEnabled
@ConditionalOnClass(LoadBalancerClientFilter.class)
@Configuration(proxyBeanMethods = false)
public class RouterRibbonAutoConfiguration {

@ConditionalOnRouterEnabled
@ConditionalOnClass(LoadBalancerClientFilter.class)
@Bean
public LoadBalancerClientFilterEnhancer loadBalancerClientFilterEnhancer(ApplicationContext context,
RouterConcept concept) {
return new LoadBalancerClientFilterEnhancer(context, concept);
@ConditionalOnMissingClass("org.springframework.cloud.gateway.config.LoadBalancerProperties")
@Configuration(proxyBeanMethods = false)
public static class V1RouterRibbonAutoConfiguration {

@Bean
public LoadBalancerClientFilterV1Enhancer loadBalancerClientFilterV1Enhancer(ApplicationContext context,
RouterConcept concept) {
return new LoadBalancerClientFilterV1Enhancer(context, concept);
}
}

@ConditionalOnRouterEnabled
@ConditionalOnClass({LoadBalancerClientFilter.class, LoadBalancerProperties.class})
@Configuration(proxyBeanMethods = false)
public static class V2RouterRibbonAutoConfiguration {

@Bean
public LoadBalancerClientFilterV2Enhancer loadBalancerClientFilterV2Enhancer(ApplicationContext context,
RouterConcept concept) {
return new LoadBalancerClientFilterV2Enhancer(context, concept);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.github.linyuzai.router.autoconfigure.RouterAutoConfiguration,\
com.github.linyuzai.router.autoconfigure.RouterLoadbalancerAutoConfiguration,\
com.github.linyuzai.router.autoconfigure.RouterRibbonAutoConfiguration,\
com.github.linyuzai.router.autoconfigure.RouterRibbonAutoConfiguration.V1RouterRibbonAutoConfiguration,\
com.github.linyuzai.router.autoconfigure.RouterRibbonAutoConfiguration.V2RouterRibbonAutoConfiguration,\
com.github.linyuzai.router.autoconfigure.RouterManagementAutoConfiguration,\
com.github.linyuzai.router.autoconfigure.RouterManagementAutoConfiguration.WebMvcRouterManagementAutoConfiguration,\
com.github.linyuzai.router.autoconfigure.RouterManagementAutoConfiguration.WebFluxRouterManagementAutoConfiguration
2 changes: 1 addition & 1 deletion concept-router/version.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ext.'conceptRouterVersion' = '0.3.0'
ext.'conceptRouterVersion' = '0.5.0'

0 comments on commit bf8bb7a

Please sign in to comment.