经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
Spring Cloud Gateway 使用示例
来源:cnblogs  作者:Grey Zeng  时间:2022/11/19 17:14:04  对本文有异议

Spring Cloud Gateway 使用示例

作者: Grey

原文地址:

博客园:Spring Cloud Gateway 使用示例

CSDN:Spring Cloud Gateway 使用示例

说明

Spring Cloud Gateway 用于构建 API 网关,基于 Spring WebFlux。

Spring Cloud G 版发布时,

Spring 官方把 Spring Cloud Gateway 作为 Zuul 1 的替代方案

image

本文主要通过一个示例介绍了 Spring Cloud Gateway 的基础使用。

环境

  • JDK 1.8+

  • Maven 3.5+

  • Spring Boot 版本:2.7.5

  • Spring Cloud 版本:2021.0.5

涉及的依赖包

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-gateway</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
  12. <scope>test</scope>
  13. </dependency>

代码说明

第一个场景就是请求转发,例如

请求:http://localhost:8080/jd

直接转到:http://jd.com:80/jd

请求:http://localhost:8080/greyzeng

直接转到:http://www.cnblogs.com/greyzeng

请求:http://localhost:8080/error

直接跳转到自定义的一个错误页面。

只需要做如下配置即可

  1. @Bean
  2. public RouteLocator myRoutes(RouteLocatorBuilder builder, UriConfiguration uriConfiguration) {
  3. return builder.routes()
  4. .route(p -> p.path("/jd").uri("http://jd.com:80/"))
  5. .route(p -> p.path("/greyzeng").uri("http://www.cnblogs.com/"))
  6. .route(p -> p.path("/error").uri("forward:/fallback"))
  7. .build();
  8. }
  9. @RequestMapping("/fallback")
  10. public Mono<String> fallback() {
  11. return Mono.just("fallback");
  12. }

启动服务,运行 GatewayApplication.java

浏览器访问:http://localhost:8080/jdhttp://localhost:8080/greyzeng,会直接跳转到对应的页面。

输入:http://localhost:8080/error,直接跳转到自定义的/fallback请求服务中。
image

在转发过程中,也可以设置一些参数,比如

  1. @Bean
  2. public RouteLocator myRoutes(RouteLocatorBuilder builder, UriConfiguration uriConfiguration) {
  3. final String httpUri = "http://httpbin.org:80";
  4. return builder.routes()
  5. .route(p -> p.path("/get").filters(f -> f.addRequestHeader("Hello", "World")).uri(httpUri))
  6. .build();
  7. }

在请求: http://localhost:8080/get 这个服务过程中,增加一个 Header 参数

image

第二个场景就是结合熔断器的使用,例如:Spring Cloud Circuit Breaker,过滤来自不同的 host 请求,比如,针对来自:*.circuitbreaker.com 的请求,将其转到统一的异常处理页面。

  1. @Bean
  2. public RouteLocator myRoutes(RouteLocatorBuilder builder, UriConfiguration uriConfiguration) {
  3. String httpUri = uriConfiguration.getHttpbin();
  4. return builder.routes()
  5. .route(p -> p.host("*.circuitbreaker.com").filters(f -> f.circuitBreaker(config -> config.setName("mycmd").setFallbackUri("forward:/fallback"))).uri(httpUri))
  6. .build();
  7. }

通过如下代码进行模拟测试,

  1. import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
  2. import static com.github.tomakehurst.wiremock.client.WireMock.get;
  3. import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
  4. import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
  5. import static org.assertj.core.api.Assertions.assertThat;
  6. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"httpbin=http://localhost:${wiremock.server.port}"})
  7. @AutoConfigureWireMock(port = 0)
  8. public class GatewayApplicationTest {
  9. @Autowired
  10. private WebTestClient webClient;
  11. @Test
  12. public void contextLoads() {
  13. //Stubs
  14. stubFor(get(urlEqualTo("/delay/3")).willReturn(aResponse().withBody("no fallback").withFixedDelay(3000)));
  15. webClient.get().uri("/delay/3").header("Host", "www.circuitbreaker.com").exchange().expectStatus().isOk().expectBody().consumeWith(response -> assertThat(response.getResponseBody()).isEqualTo("fallback".getBytes()));
  16. }
  17. }

简单说明一下

访问过程中,如果某个请求的 Host 来自于 www.circuitbreaker.com,会直接返回 /fallback 中。

如果 Host 不是 www.circuitbreaker.com,则直接返回正确结果即可。

完整代码

spring-cloud-gateway-usage

参考资料

Spring Cloud Gateway

Building a Gateway

重新定义 Spring Cloud 实战

原文链接:https://www.cnblogs.com/greyzeng/p/16905869.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号