Commit e20534a9 by Nepxion

提交Gateway扩展代码

parent 6a29d036
......@@ -19,5 +19,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-strategy</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.nepxion.discovery.plugin.strategy.extension.gateway.configuration;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.nepxion.discovery.plugin.strategy.constant.StrategyConstant;
import com.nepxion.discovery.plugin.strategy.extension.gateway.filter.GatewayStrategyFilter;
@Configuration
@AutoConfigureBefore(RibbonClientConfiguration.class)
@ConditionalOnProperty(value = StrategyConstant.SPRING_APPLICATION_STRATEGY_CONTROL_ENABLED, matchIfMissing = true)
public class GatewayStrategyAutoConfiguration {
@Bean
public GatewayStrategyFilter gatewayStrategyFilter() {
return new GatewayStrategyFilter();
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.strategy.extension.gateway.context;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.web.server.ServerWebExchange;
public class GatewayStrategyContext {
private static final ThreadLocal<GatewayStrategyContext> THREAD_LOCAL = new InheritableThreadLocal<GatewayStrategyContext>() {
@Override
protected GatewayStrategyContext initialValue() {
return new GatewayStrategyContext();
}
};
public static GatewayStrategyContext getCurrentContext() {
return THREAD_LOCAL.get();
}
public static void clearCurrentContext() {
THREAD_LOCAL.remove();
}
private ServerWebExchange exchange;
public ServerWebExchange getExchange() {
return exchange;
}
public void setExchange(ServerWebExchange exchange) {
this.exchange = exchange;
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public boolean equals(Object object) {
return EqualsBuilder.reflectionEquals(this, object);
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.strategy.extension.gateway.filter;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import reactor.core.publisher.Mono;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import com.nepxion.discovery.plugin.strategy.extension.gateway.context.GatewayStrategyContext;
public class GatewayStrategyFilter implements GlobalFilter, Ordered {
private static final Logger LOG = LoggerFactory.getLogger(GatewayStrategyFilter.class);
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
LOG.debug("Gateway strategy context is set with {}", exchange);
GatewayStrategyContext.getCurrentContext().setExchange(exchange);
return chain.filter(exchange);
}
@Override
public int getOrder() {
return -400;
}
}
\ No newline at end of file
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nepxion.discovery.plugin.strategy.configuration.StrategyAutoConfiguration
\ No newline at end of file
com.nepxion.discovery.plugin.strategy.configuration.StrategyAutoConfiguration,\
com.nepxion.discovery.plugin.strategy.extension.gateway.configuration.GatewayStrategyAutoConfiguration
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment