• springcloud gateway(hystrix filter)


    参考

    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 服务,访问熔断成功。

  • 相关阅读:
    手写Promise——基于es6的Promise实现(含详细注释)
    手写promise
    package.json里面配置的啥
    package.json配置详解
    package.json的所有配置项及其用法,你都熟悉么
    sass语法进阶小结
    [转]利用vue-cli3快速搭建vue项目详细过程
    vue的接口封装和状态管理
    Vue项目封装请求数据的接口总结
    JSDoc入门使用指南 -- 手摸手教你用JSDoc(超好用的js文档生成工具)
  • 原文地址:https://www.cnblogs.com/yun965861480/p/10844447.html
Copyright © 2020-2023  润新知