中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

springboot~zuul實現網關

網關在微服務里的角色

在微服務架構體系里,網關是非常(chang)重要(yao)的一(yi)個環(huan)節(jie),它主(zhu)要(yao)實(shi)現了一(yi)些功能的統一(yi)處理,包(bao)括(kuo)了:

  1. 統一授權
  2. 統一異常處理
  3. 路由導向
  4. 跨域處理
  5. 限流

實踐一下

1 添加依賴

dependencies {
	implementation('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
	implementation('org.springframework.cloud:spring-cloud-starter-netflix-zuul')
	testImplementation('org.springframework.boot:spring-boot-starter-test')
    implementation('com.marcosbarbero.cloud:spring-cloud-zuul-ratelimit:1.3.2.RELEASE')
}

2 添加yml

server:
  port: 8300
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: //localhost:6761/eureka
  instance:
    ip-address: true

zuul:
 routes:
    users:
       path: /lind/** #以lind開頭的路徑被重定向到lind服務
       serviceId: lind
 add-host-header: true #顯示真實的http頭
 retryable: false #關閉Hystrix的重試功能
 ratelimit:
    enabled: true
   #  repository: REDIS
    behind-proxy: true
    policies:
       users:
         limit: 5 #限流,每分鐘請求5次
         refresh-interval: 60
         type:
           - user
           - origin
           - url
         #       url類型的限流就是通過請求路徑區分
         #       origin是通過客戶端IP地址區分
         #       user是通過授權用戶進行區分,也包括匿名用戶

3 添加實現代碼
http攔(lan)截器,獲取用(yong)戶ID,為(wei)子服務進行(xing)傳遞(di)

public class PreRequestLogFilter extends ZuulFilter {
  private static final Logger logger = LoggerFactory.getLogger(PreRequestLogFilter.class);
  private final RateLimiter rateLimiter = RateLimiter.create(1000.0);

  @Override
  public Object run() {

    try {
      RequestContext currentContext = RequestContext.getCurrentContext();
      HttpServletResponse response = currentContext.getResponse();
      HttpServletRequest reqeust = currentContext.getRequest();

      currentContext.addZuulRequestHeader("userId","123");//向子系統http頭寫數據
      currentContext.addZuulRequestHeader("userName","test");

      PreRequestLogFilter.logger.info(
          String.format("send %s request to %s",
              reqeust.getMethod(),
              reqeust.getRequestURL().toString()));

      if (!rateLimiter.tryAcquire()) {
        HttpStatus httpStatus = HttpStatus.TOO_MANY_REQUESTS;
        response.setContentType(MediaType.TEXT_PLAIN_VALUE);
        response.setStatus(httpStatus.value());
        response.getWriter().append(httpStatus.getReasonPhrase());
        currentContext.setSendZuulResponse(false);
        throw new ZuulException(
            httpStatus.getReasonPhrase(),
            httpStatus.value(),
            httpStatus.getReasonPhrase()
        );
      }
    } catch (java.lang.Exception e) {
      ReflectionUtils.rethrowRuntimeException(e);
    }
    return null;

  }

  @Override
  public boolean shouldFilter() {

    // 判斷是否需要過濾
    return true;

  }


  @Override

  public String filterType() {

    return FilterConstants.PRE_TYPE;

  }


  @Override

  public int filterOrder() {

    return Ordered.HIGHEST_PRECEDENCE;

  }


}

在主程中(zhong)注入(ru)這(zhe)個(ge)過(guo)濾器

  @Bean
  public PreRequestLogFilter preRequestLogFilter() {
    return new PreRequestLogFilter();
  }

4 使用它
在URL上(shang)通過(guo)localhost:8300/users/home 將進行lind服務里的(de)home控制(zhi)器(qi)下(xia),并在http頭(tou)上(shang)寫入了(le)userid和(he)username這個鍵值對!

posted @ 2018-10-18 16:52  張占嶺  閱讀(2271)  評論(0)    收藏  舉報