SpringCloud服务网关zuul(五)

阅读更多

1.基础服务代理使用

1.1新建gateway项目,pom文件如下

  1. "1.0" encoding="UTF-8"?>
  2. "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. 4.0.0
  5. org.springframework.boot
  6. spring-boot-starter-parent
  7. 2.1.4.RELEASE
  8. net.chengp
  9. gateway
  10. 0.0.1-SNAPSHOT
  11. gateway
  12. Demo project for Spring Boot
  13. 1.8
  14. Greenwich.SR1
  15. org.springframework.cloud
  16. spring-cloud-starter-netflix-eureka-client
  17. org.springframework.cloud
  18. spring-cloud-starter-netflix-zuul
  19. org.springframework.boot
  20. spring-boot-starter-test
  21. test
  22. org.springframework.cloud
  23. spring-cloud-dependencies
  24. ${spring-cloud.version}
  25. pom
  26. import
  27. org.springframework.boot
  28. spring-boot-maven-plugin

1.2启动类开启zuul代理

  1. @EnableZuulProxy

1.3启动测试

依次启动注册中心、provider、consumer以及新建的gateway项目,通过gateway访问consumer测试 
http://127.0.0.1:40010/consumer/testHystrix

2.自定义路由规则

2.1修改配置文件指定路由规则

  1. #转发规则
  2. zuul:
  3. #忽略所有服务直接访问
  4. ignored-services: '*'
  5. #路由设置:服务名对应请求路径,注意路由路径不能相同,否则会被覆盖
  6. routes:
  7. provider: /gateway/provider/**
  8. consumer: /gateway/consumer/**

2.2重启访问

通过两次访问我们可以看到忽略规则以及转发规则都生效了

3.使用zuul做登录拦截及认证

3.1添加登录拦截类

  1. package net.chengp.gateway.filters;
  2. import javax.servlet.http.HttpServletRequest;
  3. import org.apache.commons.lang.StringUtils;
  4. import org.apache.http.HttpStatus;
  5. import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
  6. import org.springframework.stereotype.Component;
  7. import com.netflix.zuul.ZuulFilter;
  8. import com.netflix.zuul.context.RequestContext;
  9. import com.netflix.zuul.exception.ZuulException;
  10. /**
  11. * 登录拦截
  12. *
  13. */
  14. @Component
  15. public class LoginFilter extends ZuulFilter {
  16. @Override
  17. public boolean shouldFilter(){
  18. //ACL 访问控制列表
  19. RequestContext ctx = RequestContext.getCurrentContext();
  20. HttpServletRequest request = ctx.getRequest();
  21. System.out.println("请求地址:"+request.getRequestURI());
  22. if(request.getRequestURI().isEmpty() || !request.getRequestURI().startsWith("/gateway/login/")){
  23. //请求地址为空或者不是login服务下面的地址就进行拦截
  24. return true;
  25. }else {
  26. return false;
  27. }
  28. }
  29. @Override
  30. public Object run() throws ZuulException {
  31. //这里执行被拦截请求的业务逻辑,可使用JWT进行认证 json web token
  32. RequestContext ctx = RequestContext.getCurrentContext();
  33. HttpServletRequest request = ctx.getRequest();
  34. String token = request.getHeader("token");
  35. // token为空则在请求参数中拿
  36. if(StringUtils.isBlank(token)){
  37. token = request.getParameter("token");
  38. }
  39. if(StringUtils.isBlank(token)){
  40. ctx.setResponseStatusCode(HttpStatus.SC_UNAUTHORIZED);//设置为401未认证
  41. ctx.setSendZuulResponse(false);
  42. }
  43. return null;
  44. }
  45. @Override
  46. public String filterType(){
  47. /**
  48. * PRE_TYPE 前置
  49. * POST_TYPE 后置
  50. * ROUTE_TYPE 执行路由
  51. * ERROR_TYPE 异常
  52. */
  53. return FilterConstants.PRE_TYPE;
  54. }
  55. @Override
  56. public int filterOrder(){
  57. //放在解码前执行
  58. return FilterConstants.PRE_DECORATION_FILTER_ORDER;
  59. }
  60. }

3.2重启测试

带上token后可正常访问

4.使用zuul做服务限流

4.1添加服务限流拦截类

  1. package net.chengp.gateway.filters;
  2. import org.apache.http.HttpStatus;
  3. import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
  4. import org.springframework.stereotype.Component;
  5. import com.google.common.util.concurrent.RateLimiter;
  6. import com.netflix.zuul.ZuulFilter;
  7. import com.netflix.zuul.context.RequestContext;
  8. import com.netflix.zuul.exception.ZuulException;
  9. @Component
  10. public class RateLimitFilter extends ZuulFilter{
  11. private static final RateLimiter rateLimit = RateLimiter.create(1);//多实例下该值需要除以实例数
  12. @Override
  13. public boolean shouldFilter(){
  14. //这里可以引入ACL
  15. return true;
  16. }
  17. @Override
  18. public Object run() throws ZuulException {
  19. if(!rateLimit.tryAcquire()){//没拿到令牌就不继续执行
  20. RequestContext.getCurrentContext().setSendZuulResponse(false);
  21. RequestContext.getCurrentContext().setResponseStatusCode(HttpStatus.SC_BAD_REQUEST);
  22. }
  23. return null;
  24. }
  25. @Override
  26. public String filterType(){
  27. return FilterConstants.PRE_TYPE;
  28. }
  29. @Override
  30. public int filterOrder(){
  31. return -4;//最先执行
  32. }
  33. }

4.2重启测试

尝试快速刷新页面访问则返回400

你可能感兴趣的:(SpringCloud)