成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

如何在springboot中使用zuul實(shí)現(xiàn)網(wǎng)關(guān)

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

創(chuàng)新互聯(lián)于2013年開(kāi)始,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元墾利做網(wǎng)站,已為上家服務(wù),為墾利各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575

1 添加依賴(lài)

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開(kāi)頭的路徑被重定向到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類(lèi)型的限流就是通過(guò)請(qǐng)求路徑區(qū)分
     #    origin是通過(guò)客戶(hù)端IP地址區(qū)分
     #    user是通過(guò)授權(quán)用戶(hù)進(jìn)行區(qū)分,也包括匿名用戶(hù)

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

http攔截器,獲取用戶(hù)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頭寫(xiě)數(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() {
  // 判斷是否需要過(guò)濾
  return true;
 }
 @Override
 public String filterType() {
  return FilterConstants.PRE_TYPE;
 }
 @Override
 public int filterOrder() {
  return Ordered.HIGHEST_PRECEDENCE;
 }
}

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

@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)的支持。

分享標(biāo)題:如何在springboot中使用zuul實(shí)現(xiàn)網(wǎng)關(guān)
新聞來(lái)源:http://jinyejixie.com/article46/jjjieg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站內(nèi)鏈、手機(jī)網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、全網(wǎng)營(yíng)銷(xiāo)推廣微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
方山县| 西畴县| 伊通| 莱西市| 扎鲁特旗| 文山县| 昌图县| 尼木县| 辽源市| 合川市| 三都| 宁安市| 会同县| 饶阳县| 富川| 来安县| 乡宁县| 富川| 泸溪县| 和平县| 龙海市| 拜泉县| 肃北| 洛川县| 淮南市| 贵德县| 布拖县| 合肥市| 达州市| 锦州市| 威远县| 克拉玛依市| 七台河市| 凤翔县| 鄂托克旗| 五寨县| 乐平市| 黎川县| 仙桃市| 蕲春县| 荥经县|