zuul网关熔断简单使用
在Edgware版本之前,zuul提供ZuulFallbackProvider实现熔断,在Edgware版本开始,zuul提供fallbackProvider实现熔断
zuul网关熔断只针对timeout异常处理(因为对于错误没法进行准确判断)
演示过程可以不启动商品服务测试或者只写eureka注册中心和zuul项目,不写商品服务项目
http://127.0.0.1:1002/api/product-service/product/getProduct/1992?token=6666
关键代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | package com.zuul.config; import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.Component; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; /** * @author yourheart * @Description * @create 2022-05-19 15:59 */ @Component public class ProductProviderFallback implements FallbackProvider { @Override public String getRoute() { return "product-service" ; } @Override public ClientHttpResponse fallbackResponse(String route, Throwable cause) { ClientHttpResponse response = new ClientHttpResponse() { @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); MediaType mediaType = new MediaType( "application" , "json" , Charset.forName( "utf-8" )); headers.setContentType(mediaType); return headers; } @Override public InputStream getBody() throws IOException { ByteArrayInputStream inputStream = new ByteArrayInputStream( "商品服务不可用" .getBytes()); return inputStream; } @Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.INTERNAL_SERVER_ERROR; } @Override public int getRawStatusCode() throws IOException { return this .getStatusCode().value(); } @Override public String getStatusText() throws IOException { return this .getStatusCode().getReasonPhrase(); } @Override public void close() { } }; return response; } } |
zuul项目pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <?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>zuul-aprent</artifactId> <groupId>org.example</groupId> <version> 1.0 -SNAPSHOT</version> </parent> <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.zuul</groupId> <artifactId>zuul-service</artifactId> <dependencies> <!--zuul依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <!--lombok依赖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version> 1.18 . 16 </version> </dependency> <!--客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies> </project> |
父类pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <?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>org.example</groupId> <artifactId>zuul-aprent</artifactId> <packaging>pom</packaging> <version> 1.0 -SNAPSHOT</version> <modules> <module>eureka-service</module> <module>prodoct-service</module> <module>order-service</module> <module>zuul-service</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.2 . 5 .RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR3</version> <type>pom</type> <scope> import </scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version> 2.1 . 0 .RELEASE</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!--编译插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source> 1.8 </source> <target> 1.8 </target> <encoding>utf- 8 </encoding> </configuration> </plugin> <!--打包插件--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
配置文件
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | server: port: 1002 spring: application: name: zuul-service zuul: routes: product-service: path: /product-service/** serviceId: product-service prefix: /api eureka: client: service-url: defaultZone: http: //localhost:8761/eureka/ management: endpoints: web: exposure: include: '*' |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.zuul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; /** * @author yourheart * @Description * @create 2022-04-20 22:05 */ @SpringBootApplication //开启zuul注解 @EnableZuulProxy @EnableDiscoveryClient @EnableHystrixDashboard @EnableCircuitBreaker public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication. class ,args); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异