参考

https://blog.csdn.net/forezp/article/details/83792388

1.依赖pom.xml

<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>
<parent>
<groupId>com.guo</groupId>
<artifactId>guo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>guo-gateway</artifactId> <dependencies>
<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-gateway</artifactId>
</dependency>
</dependencies>
</project>

  

2.配置application.yml

server:
port: 8081 spring:
application:
name: guo-gateway
cloud:
gateway:
discovery:
locator:
enabled: false
lowerCaseServiceId: true eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/ logging:
level:
org.springframework.cloud.gateway: debug

  

3.启动配置

package com.guo.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean; @SpringBootApplication
@EnableEurekaClient
public class Application { public static void main(String[] args) {
SpringApplication.run( Application.class, args );
} @Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
String httpUri = "http://localhost:8762/hi";
return builder.routes()
.route(p -> p
.path("/demo")
.filters(f -> f.addRequestHeader("Hello", "World"))
.uri(httpUri))
.build();
}
}

  

4.启动演示

启动eureka-server eureka-client gateway

访问gateway接口 http://localhost:8081/demo?name=zs 会自动转发到 http://localhost:8762/hi

5.添加熔断器

pom.xml

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

  添加过滤器

  

package com.guo.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; @SpringBootApplication
@EnableEurekaClient
@RestController
public class Application { public static void main(String[] args) {
SpringApplication.run( Application.class, args );
} @Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
String httpUri = "http://localhost:8762/hi";
return builder.routes()
.route(p -> p
.path("/demo")
.filters(f -> f.addRequestHeader("Hello", "World"))
.uri(httpUri))
.route(p -> p
.path("/hystrix")
.filters(f -> f
.hystrix(config -> config
.setName("mycmd")
.setFallbackUri("forward:/fallback")))
.uri(httpUri))
.build();
} @RequestMapping("/fallback")
public Mono<String> fallback() {
return Mono.just("fallback");
} }

  

停掉 http://localhost:8762/hi 服务,访问熔断成功。

springcloud gateway(hystrix filter)的更多相关文章

  1. 使用springcloud gateway搭建网关(分流,限流,熔断)

    Spring Cloud Gateway Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 ...

  2. SpringCloud gateway (史上最全)

    疯狂创客圈 Java 分布式聊天室[ 亿级流量]实战系列之 -25[ 博客园 总入口 ] 前言 ### 前言 疯狂创客圈(笔者尼恩创建的高并发研习社群)Springcloud 高并发系列文章,将为大家 ...

  3. 基于springcloud gateway + nacos实现灰度发布(reactive版)

    什么是灰度发布? 灰度发布(又名金丝雀发布)是指在黑与白之间,能够平滑过渡的一种发布方式.在其上可以进行A/B testing,即让一部分用户继续用产品特性A,一部分用户开始用产品特性B,如果用户对B ...

  4. SpringCloud gateway 3

    参考博客:https://www.cnblogs.com/crazymakercircle/p/11704077.html 1.1 SpringCloud Gateway 简介 SpringCloud ...

  5. SpringCloud Gateway高阶之Sentinel限流、熔断

    前言 为什么需要服务熔断和降级?微服务是当前业界的一大趋势,原理就是将单一职责的功能模块独立化为子服务,降低服务间的耦合,服务间互相调用.但是这样也会出现一些问题: 上图中大量微服务互相调用,存在大量 ...

  6. SpringCloud Gateway快速入门

    SpringCloud Gateway cloud笔记第一部分 cloud笔记第二部分Hystrix 文章目录 SpringCloud Gateway Zull的工作模式与Gateway的对比 Rou ...

  7. 万字长文:SpringCloud gateway入门学习&实践

    官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/# ...

  8. SpringCloud Gateway 测试问题解决

    本文针对于测试环境SpringCloud Gateway问题解决. 1.背景介绍 本文遇到的问题都是在测试环境真正遇到的问题,不一定试用于所有人,仅做一次记录,便于遇到同样问题的干掉这些问题. 使用版 ...

  9. springcloud gateway nullpointerexception (NettyRoutingFilter)

    最近在做一个下载功能时,发现直接调用服务是可以下载的,但是通过gateway路由下载会报NPE异常,具体如下 java.lang.NullPointerException: null at java. ...

随机推荐

  1. java 实现简单的单点登录

    https://my.oschina.net/leamon/blog/266711 https://serviceturbo-cloud-cn.huawei.com/serviceturbocloud ...

  2. Python对比两个txt文件内容

    difflib模块作为python的标准库模块,无需安装,作用是比对文本之间的差异,且支持输出可读性比较强的html格式.#!coding=utf-8 # 2018-9-19 import sys i ...

  3. 荷小鱼 x mPaaS | 借助 H5 容器改善 App 白屏、浏览器兼容等问题

      随着5G.大数据.人工智能技术的应用,各类传统行业纷纷大力推进数字化转型升级. 而受疫情的影响,教育行业也在大幅加速线上化转型进程,各类在线教育应用也在借助各种力量拓张自己的移动端市场领域. 「荷 ...

  4. 【六】K8s-Pod 水平自动扩缩实践(简称HPA)

    一.概述 Pod 水平自动扩缩(Horizontal Pod Autoscaler)简称 HPA,HPA 可以根据 CPU 利用率进行自动伸缩 Pod 副本数量,除了 CPU 利用率,也可以基于其他应 ...

  5. 利用js判断文件是否为utf-8编码

    常规方案 使用FileReader以utf-8格式读取文件,根据文件内容是否包含乱码字符�,来判断文件是否为utf-8. 如果存在�,即文件编码非utf-8,反之为utf-8. 代码如下: const ...

  6. 摄像头Camera 标定Calibration原理Theory

    摄像头Camera 标定Calibration原理Theory cv2.cameraCalibration Pinhole camera calibration calls camera vision ...

  7. 3D深度估计

    3D深度估计 Consistent Video Depth Estimation 论文地址:https://arxiv.org/pdf/2004.15021.pdf 项目网站:https://roxa ...

  8. MindSpore Lite整体架构介绍

    MindSpore Lite整体架构介绍 MindSpore Lite框架的总体架构如下所示: 前端(Frontend): 负责模型生成,用户可以通过模型构建接口构建模型,将第三方模型和MindSpo ...

  9. GPU编程和流式多处理器

    GPU编程和流式多处理器 流式多处理器(SM)是运行CUDA内核的GPU的一部分.本章重点介绍SM的指令集功能. 流式多处理器(SM)是运行我们的CUDA内核的GPU的一部分.每个SM包含以下内容. ...

  10. 编译原理-DFA与正规式的转化