内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

如何在springboot中使用zuul實(shí)現(xiàn)網(wǎng)關(guān)-創(chuàng)新互聯(lián)

如何在springboot中使用zuul實(shí)現(xiàn)網(wǎng)關(guān)?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括沙坪壩網(wǎng)站建設(shè)、沙坪壩網(wǎng)站制作、沙坪壩網(wǎng)頁制作以及沙坪壩網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,沙坪壩網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到沙坪壩省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

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: http://localhost:6761/eureka
 instance:
  ip-address: true
zuul:
 routes:
  users:
    path: /lind/** #以lind開頭的路徑被重定向到lind服務(wù)
    serviceId: lind
 add-host-header: true #顯示真實(shí)的http頭
 retryable: false #關(guān)閉Hystrix的重試功能
 ratelimit:
  enabled: true
  # repository: REDIS
  behind-proxy: true
  policies:
    users:
     limit: 5 #限流,每分鐘請(qǐng)求5次
     refresh-interval: 60
     type:
      - user
      - origin
      - url
     #    url類型的限流就是通過請(qǐng)求路徑區(qū)分
     #    origin是通過客戶端IP地址區(qū)分
     #    user是通過授權(quán)用戶進(jìn)行區(qū)分,也包括匿名用戶

3 添加實(shí)現(xiàn)代碼

http攔截器,獲取用戶ID,為子服務(wù)進(jìn)行傳遞

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");//向子系統(tǒng)http頭寫數(shù)據(jù)
   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;
 }
}

在主程中注入這個(gè)過濾器

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

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

網(wǎng)站名稱:如何在springboot中使用zuul實(shí)現(xiàn)網(wǎng)關(guān)-創(chuàng)新互聯(lián)
本文鏈接:http://www.rwnh.cn/article10/ceipdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、營銷型網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)ChatGPT、微信小程序、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
临泽县| 武夷山市| 屯留县| 杭锦后旗| 达孜县| 五常市| 社旗县| 兰西县| 永仁县| 长乐市| 尚志市| 宾阳县| 陆良县| 柯坪县| 大同县| 济阳县| 田阳县| 紫阳县| 札达县| 额济纳旗| 新闻| 桂林市| 恩施市| 宁南县| 永泰县| 宣威市| 长治市| 丰镇市| 剑川县| 班玛县| 玛沁县| 宜良县| 芦溪县| 莎车县| SHOW| 淄博市| 彰化市| 南城县| 河北省| 台北县| 固安县|