前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCloud 2.x学习笔记:13、Spring Cloud Gateway+Hystrix简单例子(Greenwich版本)

SpringCloud 2.x学习笔记:13、Spring Cloud Gateway+Hystrix简单例子(Greenwich版本)

作者头像
程裕强
发布2019-07-02 10:45:45
8650
发布2019-07-02 10:45:45
举报

在spring cloud gateway中可以使用Hystrix。Hystrix是 spring cloud中一个服务熔断降级的组件,在微服务系统有着十分重要的作用。

1、pom.xml

代码语言:javascript
复制
<?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.cntaiping.tpa</groupId>
    <artifactId>hystrix-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>hystrix-gateway</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.cntaiping.tpa</groupId>
        <artifactId>gateway</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

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

</project>

2、application.properties

代码语言:javascript
复制
server.port= 7011

3、Application类

代码语言:javascript
复制
package com.cntaiping.tpa.hystrixgateway;

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.context.annotation.Bean;

@SpringBootApplication
public class HystrixGatewayApplication {

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

    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("path-route",p -> p
                        //断言为“/get”,凡是访问路由网关中的“/get”路径
                        .path("/get")
                        //在请求头中增加“Hello”—“World”键值对
                        .filters(f -> f.addRequestHeader("Hello", "World"))
                        //转发到http://httpbin.org
                        .uri("http://httpbin.org"))
                //使用hystrix
                .route("hystrix-route",p -> p
                        //当请求头中的Host为*.hystrix.com,进入此路由
                        .host("*.hystrix.com")
                        //设置Hystrix熔断,当请求(转发到http://httpbin.org)超时时,请求转发到Gateway中的“/fallback”
                        .filters(f -> f.hystrix(
                                config -> config.setName("cmd").setFallbackUri("forward:/fallback")))
                        //转发到http://httpbin.org
                        .uri("http://httpbin.org"))
                .build();
    }
}

4、控制器

代码语言:javascript
复制
package com.cntaiping.tpa.hystrixgateway.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class MessageController {

    @RequestMapping("/fallback")
    public Mono<String> fallback() {
        return Mono.just("fallback");
    }
}

5、执行效果

http://httpbin.org/delay/3表示3秒后请求超时。

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
 curl --dump-header - --header Host:www.hystrix.com http://localhost:7011/delay/3
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年06月20日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、pom.xml
  • 2、application.properties
  • 3、Application类
  • 4、控制器
  • 5、执行效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档