zoukankan      html  css  js  c++  java
  • 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 服务,访问熔断成功。

  • 相关阅读:
    IKAnalyzer兼容Lucene 5.4.0版本抛出异常?
    Lucene--FuzzyQuery与WildCardQuery(通配符)
    Lucene之模糊、精确、匹配、范围、多条件查询
    CentOS6.5中使用 iperf 检测主机间网络带宽
    文件切割
    CURL命令测试网站打开速度
    不限定访问,支持跨域
    Mysql错误: ERROR 1205: Lock wait timeout exceeded try restarting transaction解决办法
    tomcat链接mysql时超时报错java.io.EOFException: Can not read response from server. Expected to read 4 bytes,
    分享一个很好的工具网址
  • 原文地址:https://www.cnblogs.com/yun965861480/p/10844447.html
Copyright © 2011-2022 走看看