上海市建设安全协会网站j,做个企业网站的公司,佛山网站建设公司排名榜,莱州网站建设5.springcloud微服务架构搭建 之 《springboot集成Hystrix》 目录 1.gateway介绍
2.项目引入gateway
3.yml配置gateway参数
5.自定义全局Filter
6.测试 1.gateway介绍 服务网关#xff08;Spring Cloud Gateway#xff09;是Spring Cloud官方推出的 第二代网关框架#…5.springcloud微服务架构搭建 之 《springboot集成Hystrix》 目录 1.gateway介绍
2.项目引入gateway
3.yml配置gateway参数
5.自定义全局Filter
6.测试 1.gateway介绍 服务网关Spring Cloud Gateway是Spring Cloud官方推出的 第二代网关框架用于替代第一代网关Netflix Zuul其不仅提供统 一的路由方式并且基于Filter链的方式提供了网关的基本功能。服 务网关建立在Spring Framework 5之上使用非阻塞模式并且支持 长连接Websocket。Netflix Zuul是基于Servlet的采用HttpClient 进行请求转发使用阻塞模式。在性能上服务网关优于Netflix Zuul并且服务网关几乎实现了Netflix Zuul的全部功能。在使用和 功能上用服务网关替换掉Netflix Zuul的成本上是非常低的几乎 可以实现无缝切换。 服务网关作为整个分布式系统的流量入口有着举足轻重的作 用列举如下。 协议转换路由转发。 流量聚合对流量进行监控日志输出。 作为整个系统的前端工程对流量进行控制有限流的作用。 作为系统的前端边界外部流量只能通过网关才能访问系统。 可以在网关层做权限判断。 可以在网关层做缓存。
2.项目引入gateway
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdlilock-modules/artifactIdgroupIdlilock.cn/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdlilock-service-gateway/artifactIddependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactId/dependency!-- getway 配置 loadbalancer 之后lb才会生效--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-loadbalancer/artifactId/dependencydependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build
/project
3.yml配置gateway参数
server:port: 9090spring:application:name: lilock-service-gatewaycloud:nacos:discovery:server-addr: 127.0.0.1:8848namespace: devregister-enabled: truegroup: DEFAULT_GROUPgateway:discovery:locator:lower-case-service-id: trueenabled: trueroutes:- id: lilock-service-useruri: lb://lilock-service-userpredicates:- Path/api-user/**filters:- StripPrefix1 #替换前缀如果是/api-user/访问的地址则会通过/api-user/获取到对应服务lilock-service-user再通过lilock-service-user获取到对应的服务IP和端口完成一次完整请求main:allow-bean-definition-overriding: true
4.测试 5.自定义全局Filter package lilock.cn.gateway.filter;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;Component
public class RequestTimeFilter implements GlobalFilter, Ordered {private static Logger log LoggerFactory.getLogger(RequestTimeFilter.class);private static final String REQUEST_TIME_BEGIN requestBeginTime;Overridepublic MonoVoid filter(ServerWebExchange exchange, GatewayFilterChain chain) {exchange.getAttributes().put(REQUEST_TIME_BEGIN,System.currentTimeMillis());return chain.filter(exchange).then(Mono.fromRunnable(()-{Long startTime (Long) exchange.getAttributes().get(REQUEST_TIME_BEGIN);if(null ! startTime){log.info(exchange.getRequest().getURI().getRawPath() : (System.currentTimeMillis() - startTime) ms);}}));}Overridepublic int getOrder() {return HIGHEST_PRECEDENCE 2;}
}6.测试