forked from yinjihuan/spring-cloud
-
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
尹吉欢
committed
Jan 28, 2019
1 parent
4500bf2
commit 853cc11
Showing
3 changed files
with
73 additions
and
0 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
64 changes: 64 additions & 0 deletions
64
...ode-2/ch-15/zuul-extend/src/main/java/com/cxytiandi/zuul_demo/filter/DownGradeFilter.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,64 @@ | ||
package com.cxytiandi.zuul_demo.filter; | ||
|
||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import com.cxytiandi.auth.common.ResponseCode; | ||
import com.cxytiandi.auth.common.ResponseData; | ||
import com.cxytiandi.auth.util.JsonUtils; | ||
import com.cxytiandi.zuul_demo.config.BasicConf; | ||
import com.netflix.zuul.ZuulFilter; | ||
import com.netflix.zuul.context.RequestContext; | ||
|
||
/** | ||
* 服务降级过滤器 | ||
* | ||
* @author yinjihuan | ||
**/ | ||
public class DownGradeFilter extends ZuulFilter { | ||
|
||
@Autowired | ||
private BasicConf basicConf; | ||
|
||
public DownGradeFilter() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public boolean shouldFilter() { | ||
RequestContext ctx = RequestContext.getCurrentContext(); | ||
Object success = ctx.get("isSuccess"); | ||
return success == null ? true : Boolean.parseBoolean(success.toString()); | ||
} | ||
|
||
@Override | ||
public String filterType() { | ||
return "route"; | ||
} | ||
|
||
@Override | ||
public int filterOrder() { | ||
return 4; | ||
} | ||
|
||
@Override | ||
public Object run() { | ||
RequestContext ctx = RequestContext.getCurrentContext(); | ||
Object serviceId = ctx.get("serviceId"); | ||
if (serviceId != null && basicConf != null) { | ||
List<String> serviceIds = Arrays.asList(basicConf.getDownGradeServiceStr().split(",")); | ||
if (serviceIds.contains(serviceId.toString())) { | ||
ctx.setSendZuulResponse(false); | ||
ctx.set("isSuccess", false); | ||
ResponseData data = ResponseData.fail("服务降级中", ResponseCode.DOWNGRADE.getCode()); | ||
ctx.setResponseBody(JsonUtils.toJson(data)); | ||
ctx.getResponse().setContentType("application/json; charset=utf-8"); | ||
return null; | ||
} | ||
} | ||
return null; | ||
} | ||
} |