SpringCloudGateway【Finchley 版】上的监控
摘要:
上以篇文章讲解了分布式链路跟踪 Sleuth 与 Zipkin【Finchley 版】,这篇文章将继续按照我的计划进行小demo的开发,主要讲解SpringCloudGateway和健康监控,account服务的swagger2的简单使用。
idea还是spring 官网的问题?
健康健康必须导入hystrix依赖,SpringCloud【Finchley 版】中依赖为
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
由于我的idea是最新版本,下图左侧是我的项目结构
这样的话导致了我的依赖无法导入并且很多netfix的依赖都将无法使用,找遍了很多网上资料都没有好的解释,于是在微信社区群里的一番聊天,朋友从spring官网https://start.spring.io 下载了文件进行导入就完全没有问题,后续找到了原因是,目前应该不能在子项目中直接导入,可以单独弄一个项目导入一些不能使用的依赖。
SpringCloudGateway配置文件
server:
port: 8764
spring:
application:
name: gateway
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
probability: 1.0
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: hystrix_route
uri: lb://account:8765
predicates:
- Path=/api/**
filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/fallback
eureka:
client:
service-url:
defaultZone: http://localhost:8763/eureka/
hystrix:
command:
fallbackcm:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
management:
endpoints:
web:
exposure:
include: hystrix.stream
提示: uri:lb://account:8765
这个地方集合下面的 -Path=/api/**
,举个列子:当你访问http://api/v1/users 的时候它相当于是http://account:8765/api/v1/users, account
是你的其中一个服务
回退路由设置
package com.qzj.gateway;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
/**
* @author qinzhongjian
* @date created in 2018/6/28 20:56
* @since 1.0.0
*/
@Service
public class FallbackHandler {
public Mono<ServerResponse> fallback(ServerRequest request){
return ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromObject("fallback"));
}
}
主函数配置如下:
package com.qzj.gateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
@SpringCloudApplication
public class GatewayApplication {
@Autowired
FallbackHandler fallbackHandler;
@Bean
public RouterFunction<?> routerFunction(){
return RouterFunctions.route(RequestPredicates.GET("/fallback").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),fallbackHandler::fallback);
}
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
健康监控效果图
ping
的情况,具体的使用请大家去网上博客学习。
account服务的wagger2效果图
总结:
最近时间比较仓促,没有很认真的详细写出来很多细节地方,请大家原谅,不过我会继续坚持完善整个最新的版本的小demo,tcc事务应该是放在最后整合的,敬请期待!
参考资料:
https://github.com/dqqzj/docker (不需要做任何修改哦,直接能够运行起来的)