springcloud gateway(hystrix filter)
参考
https://blog.csdn.net/forezp/article/details/83792388
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
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 服务,访问熔断成功。
更多精彩

