javascript
SpringCloud微服务架构之,Hystrix 熔断器,Gateway 网关
Hystrix 概述
Hystix 是 Netflix 開源的一個延遲和容錯庫,用于隔離訪問遠程服務、第三方庫,防止出現級聯失敗(雪崩)。
pom依耐
<!-- hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>? 雪崩:一個服務失敗,導致整條鏈路的服務都失敗的情形。
? Hystix 主要功能
- 隔離:用于隔離不同調用鏈之間相互不受影響
- 降級:封裝友好的錯誤提示并返回
- 熔斷:當請求錯誤率較高時,自動降級熔斷
- 限流:合理分配每個調用的并發請求數量
隔離
1. 線程池隔離
將多個請求線程按照每個服務的配置,分發到不同的微服務去調用,這樣就解決的,線程一窩蜂的同一微服務中,影響的雪崩
2. 信號量隔離
降級
異常,超時
在A調用C的時候,C服務發生了異常,或者執行時間過長,為了避免請求回不友好的錯誤信息或者線程不一直等待,我們提供一個友好的方法,該方法往往返回一個失敗請求的響應結果集,如:服務器繁忙等等,使得請求響應能夠快速獲得,而且異常后是友好的提示,超時后能快速的返回響應,不占用線程.防止服務雪崩.
這個降級不但可以在服務提供方編寫一個降級方法,也可以在服務消費方編寫.
熔斷
使用服務熔斷,首先要導入pom依耐
然后在主啟動開啟熔斷:@EnableCircuitBreaker//開啟hystrix的斷路器
在服務業務類上書寫:
注意的是:不編寫那些超時,請求次數,hystrix都會有默認的值,都默認支持熔斷
限流
服務限流:(flowLimit)
就好比秒殺商品等高并發操作,禁止一窩蜂的請求過來擁擠,大家排隊,一秒鐘處理N個,有序的進行
測試Hystrix的代碼
搭建一個父工程
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.fs</groupId><artifactId>study-springcloud</artifactId><version>1.0-SNAPSHOT</version><modules><module>fs-server-eureka-7001</module><module>fs-provider-hystrix-8001</module><module>fs-consumer-hystrix-80</module></modules><!-- 作為父工程--><packaging>pom</packaging><dependencyManagement><dependencies><!-- spring boot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.3.2.RELEASE</version><type>pom</type> <!-- import 導入父工程的配置--><scope>import</scope></dependency><!-- spring cloud --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR6</version><type>pom</type> <!-- import 導入父工程的配置--><scope>import</scope></dependency><!-- spring-cloud-alibaba-dependencies 2.2.1.RELEASE --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.1.RELEASE</version><type>pom</type><scope>import</scope></dependency><!-- eureka-server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId><version>2.2.4.RELEASE</version></dependency><!-- eureka-client --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.2.4.RELEASE</version></dependency><!-- 整合MyBatis--><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.20</version><scope>runtime</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.20</version></dependency><!-- MyBatisPlus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.2</version></dependency> <!-- lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version></dependency></dependencies></dependencyManagement><dependencies></dependencies></project>fs-server-eureka-7001(省略,請查閱上一偏博客,服務治理)
fs-provider-hystrix-8001 服務提供者
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>study-springcloud</artifactId><groupId>com.fs</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>fs-provider-hystrix-8001</artifactId><dependencies><!-- hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><!-- spring-boot-starter-web spring-boot-starter-actuator綁定在一塊 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- eureka-Client--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><!--第一種是:如果你的應用不會再需要返回xml的系列化格式,那么直接在pom.xml文件中將jackson-dataformat-xml這外包排除即可(如果其他包也進行了jackson-dataformat-xml的依賴引用也要視情況排除):第二種是:不排除jackson-dataformat-xml包,而是直接在相應接口方法或Controller上明確指定將返回JSON格式的值:@GetMapping(value = "/user-instance", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)--><!-- 排除controller返回的格式為xml--><exclusions><exclusion><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId></exclusion></exclusions></dependency><!-- mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- jdbc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- druid--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId></dependency><!-- MyBatis-puls--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- 自己的實體類--><dependency><groupId>com.fs</groupId><artifactId>fs-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>application.yml
server:port: 8001 spring:application:name: fs-provider-hystrixdatasource:username: rootpassword: rooturl: jdbc:mysql://192.168.93.132:3306/fs_springclouddriver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource #自定義數據源# 配置eureka eureka:instance:hostname: localhost # 主機名,寫的是域名,本機在host文件中映射prefer-ip-address: true # 將當前實例的ip注冊到eureka server中.默認是false 注冊主機名ip-address: 127.0.0.1 # 修改instance-id顯示 # # 修改instance-id顯示,在eureka中的顯示名稱 # instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} # lease-renewal-interval-in-seconds: 30 # 每一次eureka client 向 eureka server發送心跳的時間間隔 # lease-expiration-duration-in-seconds: 90 # 如果90秒內eureka server沒有收到eureka client的心跳包,則剔除該服務client:register-with-eureka: true # 將提供注冊到注冊eureka中心fetch-registry: true # 從eureka上抓取已有的注冊信息service-url:defaultZone: http://localhost:7001/eureka #,http://localhost2:7002/eureka,http://localhost3:7003/eureka # 注冊中心地址# 配置MyBatis-plus日志 mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplFsProviderHystrix8001 主啟動
注意:@EnableCircuitBreaker//開啟hystrix的斷路器
package com.fs;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication //開啟Eureka客戶端 @EnableEurekaClient @EnableCircuitBreaker//開啟hystrix的斷路器 public class FsProviderHystrix8001 {public static void main(String[] args) {SpringApplication.run(FsProviderHystrix8001.class,args);} }PaymentServiceImpl 業務接口實現類
主要注解:@HystrixCommand
package com.fs.service.impl;import com.fs.dao.PaymentDao; import com.fs.pojo.Payment; import com.fs.result.Result; import com.fs.service.PaymentService; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.PathVariable;import java.util.List;@Service public class PaymentServiceImpl implements PaymentService {//注入dao@Autowiredprivate PaymentDao paymentDao;@Overridepublic List<Payment> findAll() {//使用MyBatis提供的方法return paymentDao.selectList(null);}/*測試消費端配置的服務降級方法*/@Overridepublic Result testConsumer(Integer id) {//制作異常,然后調用這個方法就會去調用配置的降級方法if (id==0){//id==0.就異常int i = 1/0;}return new Result(true,"testConsumer調用成功");}/*降級:1.服務出現異常2.服務調用超時,默認為1秒*/@Override//@HystrixCommand一旦調用服務方法失敗并拋出了錯誤信息后,會自動調用標注好的fallbackMethod()指定降級后調用的方法名稱@HystrixCommand(fallbackMethod = "providerFallbackMethod",commandProperties = {//HystrixCommandProperties:這個類中有HystrixCommand注解的所有配置//timeoutInMilliseconds規定這個方法在3秒內沒有正常執行,就服務降級,方法異常會立馬進行降級,不配置默認1秒@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")//熔斷不配置默認是 默認失敗次數20次 失敗率默認百分之50 默認5000毫秒 就是打開狀態與半開狀態之間的休眠時間})public Result testFallbackMethod(Integer id){//制作異常,然后調用這個方法就會去調用配置的降級方法if (id==0){//id==0.就異常int i = 1/0;}return new Result(true,"testFallbackMethod調用成功");}//服務對應的降級方法/*1.方法的返回值需要和原方法一樣2,方法的參數需要和原方法一樣*/@Overridepublic Result providerFallbackMethod(Integer id){return new Result(false,"小二開小差中,請稍候再試~~~服務降級方法");}//======服務熔斷,providerCircuitBreakerMethod異常或者超時調用的方法@HystrixCommand(fallbackMethod = "providerCircuitBreakerMethod",commandProperties = {@HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//是否開啟斷路器//sleepWindowInMilliseconds請求量閾值 默認失敗次數20次@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),//請求次數//監控時間,默認5000毫秒 就是打開狀態與半開狀態之間的休眠時間@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000"),//時間窗口日期//誤差閾值百分比errorThresholdPercentage 失敗率默認百分之50@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),//失敗率60%達到多少跳閘(進行服務熔斷)})public Result providerCircuitBreaker(@PathVariable("id") Integer id){//制作異常,然后調用這個方法就會去調用配置的降級方法if (id==0){//id==0.就異常int i = 1/0;}return new Result(true,"providerCircuitBreaker調用成功");}//熔斷友好提示方法@Overridepublic Result providerCircuitBreakerMethod(Integer id){return new Result(false,"服務器繁忙,請稍候再試~~~熔斷方法");}}fs-consumer-hystrix-80 使用feign遠程調用 服務消費端
pom.xml
== hystrix 因為OpenFeign集成了hystrix,所以不用導入hystrix的依耐==
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>study-springcloud</artifactId><groupId>com.fs</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>fs-consumer-hystrix-80</artifactId><dependencies> <!-- hystrix 因為OpenFeign集成了hystrix,所以不用導入hystrix的依耐--><!-- spring-boot-starter-web spring-boot-starter-actuator綁定在一塊 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- openFeign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- eureka-Client--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><!-- 排除controller返回的格式為xml--><exclusions><exclusion><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId></exclusion></exclusions></dependency><!-- 自己的實體類--><dependency><groupId>com.fs</groupId><artifactId>fs-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>application.yml
主要的是加上:
因為feign默認是不開啟hystrix的
application.yml
server:port: 80spring:application:name: fs-consumer-hystrix-80 eureka:client:register-with-eureka: false # 消費者我目前的用途不需要將消費者注冊到注冊中心fetch-registry: true # 從注冊中心拉取服務registry-fetch-interval-seconds: 30 # 默認30秒定時去注冊中心拉取服務service-url:defaultZone: http://localhost:7001/eureka #,http://localhost2:7002/eureka,http://localhost3:7003/eureka # 注冊中心地址# 設置feign客戶端超時時間(OpenFeign默認支持Ribbon) 不配置,默認1秒,因為feign底層是使用的Ribbon,Ribbon默認超時是1秒 ribbon:#指的是建立鏈接后從服務器讀取到可用資源所用的時間,等5秒ReadTimeout: 5000#指的是建立連接所用的時間,適用于網絡狀態正常的情況下,兩端連接所用的時間,等5秒ConnectTimeout: 5000 logging:level:# root: debug # 開啟springboot的debug的日志信息,不配置springboot默認是info# com.fs: debug # 開啟部分springboot的debug的日志信息# feign日志以什么級別監控那個feign組件功能使用的接口,使用debug級別(只能記錄debug級別),然后調用服務方法,在控制臺就能看到詳細debug信息com.fs.springcloud.server.PaymentOpenFeignService: debug# 開啟feign對hystrix的支持,因為feign默認是不開啟hystrix的 feign:hystrix:enabled: trueFsConsumerHystrix80 主啟動
package com.fs;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication //使用openFeign,激活并開啟,feign集成了robbin同時集成了hystrix,自動默認輪詢的負載均衡規則 @EnableFeignClients @EnableEurekaClient public class FsConsumerHystrix80 {public static void main(String[] args) {SpringApplication.run(FsConsumerHystrix80.class,args);} }PaymentOpenFeignHystrix 定義 OpenFeign的接口
指定服務降級友好方法類:fallback = PaymentHystrixFallbackServiceImpl.class
package com.fs.feign;import com.fs.config.OpenFeignConfig; import com.fs.pojo.Payment; import com.fs.result.Result; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping;import java.util.List; /*** feign的聲明式接口,發起遠程調用的,簡化restTemplate** 1.定義接口* 2.接口上添加注解@FeignClient(value = "注冊中心服務提供者名",configuration=定義的OpenFeign的日志類,fallback定義服務降級方法)* 3.編寫調用接口,接口的聲明規則和提供方接口保持一致* 4.去controller注入改接口對象,調用接口方法來完成遠程調用*/ @FeignClient(value = "fs-provider-hystrix",configuration = OpenFeignConfig.class,fallback = PaymentHystrixFallbackServiceImpl.class) public interface PaymentOpenFeignHystrix {//復制服務提供的controller方法,路徑記得加上controller類上的路徑@RequestMapping("/payment/findAll")Result<List<Payment>> findAll();//測試time超時,由于我們在服務提供方的這個方法制作了sleep2秒,由于feign底層基于Ribbon,//Ribbon默認超時時間為1秒,所以報錯java.net.SocketTimeoutException: Read timed out//解決辦法在配置文件中配置Ribbon的超時時間@RequestMapping("/payment/testTimeOut/{id}")Result test(@PathVariable("id") Integer id); }PaymentHystrixFallbackServiceImpl 遠程調用異常或者超時的服務降級友好方法類
package com.fs.feign;import com.fs.pojo.Payment; import com.fs.result.Result; import org.springframework.stereotype.Component;import java.util.List;/*** Feign 客戶端的降級處理類(feign自動集成hystrix,但是默認關閉,需要在yml中開啟)* 1. 定義類 實現 Feign 客戶端接口* 2. 使用@Component注解將該類的Bean加入SpringIOC容器* 默認異常降級,默認等待超時1秒,默認異常占比百分之50,默認的半開時間5秒,默認10秒20次失敗請求*/ @Component public class PaymentHystrixFallbackServiceImpl implements PaymentOpenFeignHystrix {@Overridepublic Result<List<Payment>> findAll() {return new Result(false,"fs-consumer-hystrix-80-findAll服務降級");}@Overridepublic Result test(Integer id) {return new Result(false,"fs-consumer-hystrix-80-test服務降級");} }OpenFeignConfig
package com.fs.config;import feign.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/* OpenFeign的日志配置類*/ @Configuration public class OpenFeignConfig {@BeanLogger.Level feignLoggerLevel(){//表示開啟的是詳細的feign日志,FULL表示最為詳細的,點進去有4個return Logger.Level.FULL;}//還需要在yml中配置feign日志已什么級別監控那個接口 }PaymentController 消費端controller
package com.fs.controller;import com.fs.feign.PaymentOpenFeignHystrix; import com.fs.pojo.Payment; import com.fs.result.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController @RequestMapping("/consumer") public class PaymentController {//注入OpenFeign接口@Autowiredprivate PaymentOpenFeignHystrix paymentOpenFeign;@RequestMapping("/payment/findAll")public Result<List<Payment>> findAll(){//調用OpenFeign接口Result<List<Payment>> all = paymentOpenFeign.findAll();return all;}//測試OpenFeign遠程調用,服務降級方法@RequestMapping("/payment/testTimeOut/{id}")Result test(@PathVariable("id")Integer id){Result test = paymentOpenFeign.test(id);return test;}}啟動項目測試
先啟動eureka注冊中心:fs-server-eureka-7001
在啟動服務提供端:fs-provider-hystrix-8001
在啟動服務消費端:fs-consumer-hystrix-80
首先瀏覽器輸入:http://localhost:7001/
然后測試服務提供方的業務是否能正常訪問,我們直接訪問被指定降級的業務方法
然后測試服務消費端配置的服務降級接口
Hystrix 熔斷監控
Gateway 網關
網關概述
? 網關旨在為微服務架構提供一種簡單而有效的統一的API路由管理方式。
? 在微服務架構中,不同的微服務可以有不同的網絡地址,各個微服務之間通過互相調用完成用戶請求,客戶端可能通過調用N個微服務的接口完成一個用戶請求。
? 存在的問題:
? 客戶端多次請求不同的微服務,增加客戶端的復雜性
? 認證復雜,每個服務都要進行認證
? http請求不同服務次數增加,性能不高
? 網關就是系統的入口,封裝了應用程序的內部結構,為客戶端提供統一服務,一些與業務本身功能無關的公共邏輯可以在這里實現,諸如認證、鑒權、監控、緩存、負載均衡、流量管控、路由轉發等
? 在目前的網關解決方案里,有Nginx+ Lua、Netflix Zuul 、Spring Cloud Gateway等等
什么是Gateway
- 定義:前端統一訪問微服務的人口
- 功能:
- 認證
- 鑒權
- 日志
- 監控
- 緩存
- 負載均衡
- 流量控制
- 事實:
- Gateway自動集成Ribbon,且默認開啟相關功能
Gateway 網關路由配置
Gateway 網關路由配置 – 靜態路由
# 網關配置gateway:discovery:locator:enabled: true #使用情況少,不常用(了解) 開啟從注冊中心動態創建路由的功能,利用微服務進行路由,請求路徑前可以添加微服務名稱lower-case-service-id: true #(了解) 允許請求路徑的微服務名稱為小寫routes:- id: payment_routh #payment_routh 路由的id,若不配置,默認是UUID,沒有固定規則但要求唯一uri: http://localhost:8001 # 靜態路由 這樣配置是死的,不好 匹配后提供服務的路由地址Gateway 網關路由配置 – 動態路由
? 引入eureka-client配置
? 修改uri屬性:uri: lb://服務名稱
Gateway 網關路由配置 – 微服務名稱配置
# 網關配置gateway:discovery:locator:enabled: true #使用情況少,不常用(了解) 開啟從注冊中心動態創建路由的功能,利用微服務進行路由,請求路徑前可以添加微服務名稱lower-case-service-id: true #(了解) 允許請求路徑的微服務名稱為小寫Gateway 過濾器
局部過濾器配置
predicates 斷言
全局過濾器在下面的案列中
使用Gateway
1.導入依耐
2.編寫application.yml配置
搭建Gateway工程
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>shangguiguSpringCloud</artifactId><groupId>com.fs</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>fs_gateway_gateway9527</artifactId><dependencies><!-- gateway--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!-- eureka-client--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency> <!-- 基礎配置--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency> <!-- test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies></project>application.yml
配置
- 路由配置
- id:自定義服務名稱(唯一)
- uri:請求轉發的目的地
- 靜態路由:http://localhost:8080/
- 動態路由:lb://服務名稱
- predicates:那些請求需要使用該路由配置進行轉發
- Path=/Result/** 依據請求路徑來匹配轉發的請求
- filter
- 可以使用官方定義的過濾器,但一般使用我們自己定義的
主啟動
package com.fs.springcloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication @EnableEurekaClient public class GatewayMain9527 {public static void main(String[] args) {SpringApplication.run(GatewayMain9527.class,args);} }自定義網關過濾器MyGatewayFilter(全局過濾器)
對請求進行功能的增強(認證、設置憑證信息等)
- 局部過濾器:只對配置的路由進行生效
- org.springframework.cloud.gateway.filter.factory.過濾器名稱GatewayFilterFactory
- 全局過濾器:所有請求都生效
- GlobalFilter
- Ordered指定過濾器執行的順序,越小越先執行)
總結
以上是生活随笔為你收集整理的SpringCloud微服务架构之,Hystrix 熔断器,Gateway 网关的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringCloud微服务架构,Spr
- 下一篇: SpringCloud微服务架构,Con