查看原文
其他

SpringCloudGateway【Finchley 版】上的监控

dqqzj SpringForAll社区 2020-10-17

摘要:

上以篇文章讲解了分布式链路跟踪 Sleuth 与 Zipkin【Finchley 版】,这篇文章将继续按照我的计划进行小demo的开发,主要讲解SpringCloudGateway和健康监控,account服务的swagger2的简单使用。

idea还是spring 官网的问题?

健康健康必须导入hystrix依赖,SpringCloud【Finchley 版】中依赖为

  1. <dependency>

  2.            <groupId>org.springframework.cloud</groupId>

  3.            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

  4. </dependency>

由于我的idea是最新版本,下图左侧是我的项目结构

这样的话导致了我的依赖无法导入并且很多netfix的依赖都将无法使用,找遍了很多网上资料都没有好的解释,于是在微信社区群里的一番聊天,朋友从spring官网https://start.spring.io 下载了文件进行导入就完全没有问题,后续找到了原因是,目前应该不能在子项目中直接导入,可以单独弄一个项目导入一些不能使用的依赖。

SpringCloudGateway配置文件

  1. server:

  2.  port: 8764

  3. spring:

  4.  application:

  5.    name: gateway

  6.  zipkin:

  7.    base-url: http://localhost:9411

  8.  sleuth:

  9.    sampler:

  10.      probability: 1.0

  11.  cloud:

  12.    gateway:

  13.      discovery:

  14.        locator:

  15.          enabled: true

  16.      routes:

  17.      - id: hystrix_route

  18.        uri: lb://account:8765

  19.        predicates:

  20.        - Path=/api/**

  21.        filters:

  22.        - name: Hystrix

  23.          args:

  24.            name: fallbackcmd

  25.            fallbackUri: forward:/fallback

  26. eureka:

  27.  client:

  28.    service-url:

  29.      defaultZone: http://localhost:8763/eureka/

  30. hystrix:

  31.  command:

  32.    fallbackcm:

  33.      execution:

  34.        isolation:

  35.          thread:

  36.            timeoutInMilliseconds: 5000

  37. management:

  38.  endpoints:

  39.    web:

  40.      exposure:

  41.        include: hystrix.stream

提示: uri:lb://account:8765这个地方集合下面的 -Path=/api/**,举个列子:当你访问http://api/v1/users 的时候它相当于是http://account:8765/api/v1/users, account是你的其中一个服务

回退路由设置

  1. package com.qzj.gateway;

  2. import org.springframework.http.MediaType;

  3. import org.springframework.stereotype.Service;

  4. import org.springframework.web.reactive.function.BodyInserters;

  5. import org.springframework.web.reactive.function.server.ServerRequest;

  6. import org.springframework.web.reactive.function.server.ServerResponse;

  7. import reactor.core.publisher.Mono;

  8. /**

  9. * @author qinzhongjian

  10. * @date created in 2018/6/28 20:56

  11. * @since 1.0.0

  12. */

  13. @Service

  14. public class FallbackHandler {

  15.    public Mono<ServerResponse> fallback(ServerRequest request){

  16.        return ServerResponse.ok()

  17.                .contentType(MediaType.TEXT_PLAIN)

  18.                .body(BodyInserters.fromObject("fallback"));

  19.    }

  20. }

主函数配置如下:

  1. package com.qzj.gateway;

  2. import org.springframework.beans.factory.annotation.Autowired;

  3. import org.springframework.boot.SpringApplication;

  4. import org.springframework.cloud.client.SpringCloudApplication;

  5. import org.springframework.context.annotation.Bean;

  6. import org.springframework.http.MediaType;

  7. import org.springframework.web.reactive.function.server.RequestPredicates;

  8. import org.springframework.web.reactive.function.server.RouterFunction;

  9. import org.springframework.web.reactive.function.server.RouterFunctions;

  10. @SpringCloudApplication

  11. public class GatewayApplication {

  12.    @Autowired

  13.    FallbackHandler fallbackHandler;

  14.    @Bean

  15.    public RouterFunction<?> routerFunction(){

  16.        return RouterFunctions.route(RequestPredicates.GET("/fallback").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),fallbackHandler::fallback);

  17.    }

  18.    public static void main(String[] args) {

  19.        SpringApplication.run(GatewayApplication.class, args);

  20.    }

  21. }

健康监控效果图

注意:你应该首先访问一次接口,否则会出现一直 ping的情况,具体的使用请大家去网上博客学习。

account服务的wagger2效果图

总结:

最近时间比较仓促,没有很认真的详细写出来很多细节地方,请大家原谅,不过我会继续坚持完善整个最新的版本的小demo,tcc事务应该是放在最后整合的,敬请期待!

参考资料:

https://github.com/dqqzj/docker (不需要做任何修改哦,直接能够运行起来的)

推荐:Dubbo x Cloud Native 服务架构长文总结(很全)

上一篇:利用OAuth2.0来构建一个基于Spring Boot的安全的端到端通信应用

关注公众号


    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存