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

  • 相关阅读:
    MFC tab页面中获到其它页面的数据
    sqlite数据库中"Select * From XXX能查到数据,但是Select DISTINCT group From xxx Order By group却查不出来
    关闭程序出现崩溃(exe 已触发了一个断点及未加载ucrtbased.pdb)
    springboot 通用Mapper使用
    springBoot 发布war包
    springCloud Zuul网关
    springboot hystrix turbine 聚合监控
    springBoot Feign Hystrix Dashboard
    springBoot Ribbon Hystrix Dashboard
    springBoot Feign Hystrix
  • 原文地址:https://www.cnblogs.com/yun965861480/p/10844447.html
Copyright © 2011-2022 走看看