溫馨提示×

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

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

spring cloud gateway整合sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流的方法

發(fā)布時(shí)間:2021-05-27 14:45:11 來(lái)源:億速云 閱讀:979 作者:小新 欄目:編程語(yǔ)言

這篇文章給大家分享的是有關(guān)spring cloud gateway整合sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流的方法的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

說(shuō)明: sentinel可以作為各微服務(wù)的限流,也可以作為gateway網(wǎng)關(guān)的限流組件。 spring cloud gateway有限流功能,但此處用sentinel來(lái)作為替待。

說(shuō)明:sentinel流控可以放在gateway網(wǎng)關(guān)端,也可以放在各微服務(wù)端。

1,以父工程為基礎(chǔ),創(chuàng)建子工程

2,添加pom依賴

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

2,添加配置項(xiàng)

server:
 port: 9092
spring:
 cloud:
  nacos:
   discovery:
    register-enabled: false
    server-addr: localhost:8848
    namespace: c22e5019-0bee-43b1-b80b-fc0b9d847501
  sentinel:
   transport:
    dashboard: localhost:8080
    port: 8719
   scg:
    fallback:
     mode: response
     response-status: 455
     response-body: error!
  gateway:
   routes:
    - id: demo_route
     uri: lb://demo
     predicates:
      - Path=/demo/**
    - id: demo2_test
     uri: lb://demo2
     predicates:
      - Path=/user/**
 application:
  name: gateway-sentinel

scg.fallback為sentinel限流后的響應(yīng)配置

3,啟動(dòng)類

@SpringBootApplication
@EnableDiscoveryClient
public class GatewaySentinelApplication {
  public static void main(String[] args) {
    
    SpringApplication.run(GatewaySentinelApplication.class, args);
  }
}

4,啟動(dòng)后,在sentinel控制臺(tái)可以看到 gateway-sentinel 應(yīng)用,可以通過(guò)控制臺(tái)設(shè)置流控規(guī)則。

感謝各位的閱讀!關(guān)于“spring cloud gateway整合sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向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