Sentinel icon indicating copy to clipboard operation
Sentinel copied to clipboard

[Feature] Integration with Spring Cloud Gateway

Open kimmking opened this issue 8 years ago • 8 comments

Issue Description

Integration with Spring Cloud Gateway

Describe what happened (or what feature you want)

In my recent tests, Spring Cloud Gateway is a bit better than Zuul&Zuul2 Gateway. Community can Integrate it.

kimmking avatar Aug 24 '18 10:08 kimmking

refer: https://github.com/spring-cloud/spring-cloud-gateway/blob/86489b6d7747aee3ff72b5bbdeb81a07a1ab489e/spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/ThrottleGatewayFilter.java

kimmking avatar Aug 29 '18 09:08 kimmking

大神,钉钉搞个专项的群呀,组织下,正好有这方面的需求

westonlv avatar Feb 13 '19 07:02 westonlv

大神,钉钉搞个专项的群呀,组织下,正好有这方面的需求

和你的钉钉账号联系不起来。你能在首页的钉钉群里联系一下"宿何"吗?

jialianglinjl avatar Feb 21 '19 02:02 jialianglinjl

Contributions are welcomed :)

sczyh30 avatar Feb 28 '19 04:02 sczyh30

you can FYI this code and i has implements spring-cloud- gateway for sentinel by gatewayfilter `@Component public class SentinelGatewayFilter implements GlobalFilter, Ordered {

@Override
public int getOrder() {
	// TODO Auto-generated method stub
	return -1;
}

@Override
public Mono<Void> filter(ServerWebExchange serverWebExchange, GatewayFilterChain chain) {
	String resource = serverWebExchange.getRequest().getPath().toString();
	this.initFlowRules(resource);
	Entry entry = null;
	
	try {
		ContextUtil.enter(resource);
		entry = SphU.entry(resource, EntryType.IN, 1, serverWebExchange.getRequest().getQueryParams().values());
		return chain.filter(serverWebExchange);
	} catch (BlockException e) {
		if (!BlockException.isBlockException(e)) {
			Tracer.trace(e);
		}
		e.printStackTrace();
	} finally {
		if (entry != null) {
			entry.exit();
		}
		ContextUtil.exit();
	}
	serverWebExchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
	return serverWebExchange.getResponse().setComplete();
}

private  void initFlowRules(final String resource){
	List<FlowRule> rules = new ArrayList<FlowRule>();
	// 限流
	FlowRule rule = new FlowRule();
	rule.setResource(resource);
	rule.setCount(5);
	rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
	rule.setLimitApp("default");
	
	rules.add(rule);
	FlowRuleManager.loadRules(rules);
}

} `

management.endpoints.web.exposure.include=* #Sentinel 控制台地址 spring.cloud.sentinel.transport.dashboard=localhost:8080 #取消Sentinel控制台懒加载 spring.cloud.sentinel.eager=true Because sentinel need to support by servlet, but the spring-cloud-gateway does't need servlet. so ,you can import servlet dependency into your pom.xml

vikenlove avatar Mar 13 '19 08:03 vikenlove

This is the current way, or look forward to the sentinel support for spring cloud gateway.

vikenlove avatar Mar 13 '19 08:03 vikenlove

Hi folks, since 1.5.0 Sentinel has provided integration module for Reactor and Spring WebFlux. If you're interested in contributing this feature, you can refer to the two modules :)

sczyh30 avatar Mar 15 '19 01:03 sczyh30

This will be supported in 1.6.0. See #695 for details.

sczyh30 avatar Apr 23 '19 06:04 sczyh30