国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

springboot zuul實(shí)現(xiàn)網(wǎng)關(guān)的代碼

2024-07-14 08:42:37
字體:
供稿:網(wǎng)友

網(wǎng)關(guān)在微服務(wù)里的角色

在微服務(wù)架構(gòu)體系里,網(wǎng)關(guān)是非常重要的一個(gè)環(huán)節(jié),它主要實(shí)現(xiàn)了一些功能的統(tǒng)一處理,包括了:

  1. 統(tǒng)一授權(quán)
  2. 統(tǒng)一異常處理
  3. 路由導(dǎo)向
  4. 跨域處理
  5. 限流

實(shí)踐一下

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: 8300spring: application:  name: microservice-gateway-zuuleureka: client:  register-with-eureka: true  fetch-registry: true  service-url:   defaultZone: http://localhost:6761/eureka instance:  ip-address: truezuul: 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 #限流,每分鐘請求5次     refresh-interval: 60     type:      - user      - origin      - url     #    url類型的限流就是通過請求路徑區(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(); }

4 使用它

在URL上通過localhost:8300/users/home 將進(jìn)行l(wèi)ind服務(wù)里的home控制器下,并在http頭上寫入了userid和username這個(gè)鍵值對!

總結(jié)

以上所述是小編給大家介紹的springboot zuul實(shí)現(xiàn)網(wǎng)關(guān),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對VeVb武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大港区| 普安县| 会宁县| 镇坪县| 新竹县| 随州市| 贵州省| 康保县| 长垣县| 聂拉木县| 昭平县| 弥渡县| 常州市| 邵东县| 恩施市| 盐山县| 民乐县| 通许县| 长春市| 庆城县| 桂阳县| 昆明市| 蛟河市| 慈利县| 斗六市| 濮阳县| 延津县| 阳信县| 冕宁县| 河东区| 开原市| 康平县| 绍兴县| 东辽县| 兴文县| 无锡市| 读书| 额敏县| 禹城市| 渝中区| 哈密市|