溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在SpringCloud中利用Zuul實(shí)現(xiàn)一個(gè)網(wǎng)關(guān)功能

發(fā)布時(shí)間:2021-04-16 17:00:10 來(lái)源:億速云 閱讀:205 作者:Leah 欄目:編程語(yǔ)言

今天就跟大家聊聊有關(guān)怎么在SpringCloud中利用Zuul實(shí)現(xiàn)一個(gè)網(wǎng)關(guān)功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

API Gateway,時(shí)系統(tǒng)的唯一對(duì)外的入口,介于客戶端和服務(wù)端之間的中間層,處理非業(yè)務(wù)功能,

提供路由請(qǐng)求,鑒權(quán),監(jiān)控,緩存,限流等功能

  • 統(tǒng)一接入

    •   智能路由

    •   AB測(cè)試、灰度測(cè)試

    •   負(fù)載均衡、容災(zāi)處理

    •   日志埋點(diǎn)(類似 Nignx日志)

  • 流量監(jiān)控

    •   限流處理

    •   服務(wù)降級(jí)

  • 安全防護(hù)

    •   鑒權(quán)處理

    •   監(jiān)控

    •   機(jī)器網(wǎng)終隔離

1.添加依賴

注意SpringBoot和SpringCloud版本兼容

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- fastjson -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.28</version>
</dependency>

2.添加啟動(dòng)類注解@EnableZuulProxy

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
public class ZuulgatewayApplication {

  public static void main(String[] args) {
    SpringApplication.run(ZuulgatewayApplication.class, args);
  }

}

3.修改application.yml配置

默認(rèn)訪問(wèn)規(guī)則

http://gateway:port/service-id/**

server:
 port: 9000
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/

spring:
 application:
  name: zuul-gateway

#自定義路由映射
#order-service是訂單服務(wù)的名稱,訪問(wèn)路徑為
#舊:  http://localhost:9000/order-serice/api/v1/order/find
#新:  http://localhost:9000/apigateway/order/api/v1/order/find
zuul:
 routes:
#方法一:
#  product-service: /apigateway/product/**
#  order-service: /apigateway/order/**
#方法二:
  product-route:    #路由名稱,可以任意取
   service-id: product-service
   path: /apigateway/product/**
  order-route:
   service-id: order-service
   path: /apigateway/order/**
#忽略整個(gè)服務(wù),不對(duì)外提供接口
#多個(gè)服務(wù)用逗號(hào)隔開product-service,order-service
#即不能用http://localhost:9000/order-serice/api/v1/order/find方式訪問(wèn)
 # ignored-services: product-service
#正則表達(dá)式忽略多個(gè)服務(wù)
 ignored-patterns: /*-service/**
 sensitive-headers:

#zuul使用Ribbon負(fù)載均衡,所以要配置ribbon超時(shí)時(shí)間,否則很短
 host:
  connect-timeout-millis: 15000 #HTTP連接超時(shí)要比Hystrix的大
  socket-timeout-millis: 60000  #socket超時(shí)
ribbon:
 ReadTimeout: 10000
 ConnectTimeout: 10000

4.Zuul網(wǎng)關(guān)注意事項(xiàng)

默認(rèn)情況,請(qǐng)求頭header不會(huì)傳遞Cookie,Set-Cookie,Authorization信息,這些信息會(huì)顯示為空

如果需要傳遞,則修改application.yml配置

zuul: sensitive-headers:

5.訪問(wèn)路徑

http://127.0.0.1:9000/apigateway/product/api/v1/product/find?id=1

http://127.0.0.1:9000/apigateway/order/api/v1/order/test?product_id=1

圖1

怎么在SpringCloud中利用Zuul實(shí)現(xiàn)一個(gè)網(wǎng)關(guān)功能

看完上述內(nèi)容,你們對(duì)怎么在SpringCloud中利用Zuul實(shí)現(xiàn)一個(gè)網(wǎng)關(guān)功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI