gateway中记录请求和响应的时间

package com.lvhao.filters;

import org.apache.commons.lang.StringUtils;

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

import org.springframework.cloud.gateway.filter.GatewayFilterChain;

import org.springframework.cloud.gateway.filter.GlobalFilter;

import org.springframework.http.server.reactive.ServerHttpRequest;

import org.springframework.stereotype.Component;

import org.springframework.web.server.ServerWebExchange;

import reactor.core.publisher.Mono;

/**

*/

@Component

public class CommonFilter implements GlobalFilter {

复制代码
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

    ServerHttpRequest request = exchange.getRequest().mutate().

            build();
    System.out.println("请求开始");
    return chain.filter(exchange.mutate().request(request).build()).then(Mono.fromRunnable(()->{
        System.out.println("1111111111111请求结束拉  ");
    }));
}

}

复制代码
https://blog.csdn.net/github_38924695/article/details/105049708

在这里插入代码片
复制代码
项目的基本介绍  
应用微服务
gateway网关
nginx
prometheus 普罗米修斯  应用服务的监控
nacos 注册配置中信
skywalking 做链路追踪  
oss 存储

限流一定是你所有项目开发完毕后 最终要做的事情  而不是一开始做的事情
普罗米修斯  做监控  监控你的服务发现是否有问题 如果有问题及时告警
通知运维或者开发人员去解决问题,业务系统的性能监控

skywalking 一次请求进来经过的所有组件 他全会帮你可视化展示出来
oss 做图片上传
数据库分库分表 使用Mycat  mycat做关系型数据库集群    . (Tidb  分布式数据库 原生的支持分布式场景 并发能力强一些) 
canal监听我们数据的Binlog 然后同步到redis中
		这样我们不用考虑我们数据是否一致的问题,我们吧数据写进mysql 中,然后canal会自动监听mysql的变动 然后吧数据写入redis中
复制代码
gateway 允许跨域的访问  因为前端项目和gateway 不是一个端口了
跨域配置在网关上  配置跨域  网关中配置限流信息, 如何在微服务中拿到真实的ip
复制代码
#########> 网关核心的功能就是过滤器
package cn.wolfcode.filters;

import cn.wolfcode.common.constants.CommonConstants;
import cn.wolfcode.redis.CommonRedisKey;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 定义全局过滤器,功能如下:
 * 1.把客户端真实IP通过请求同的方式传递给微服务
 * 2.在请求头中添加FEIGN_REQUEST的请求头,值为0,标记请求不是Feign调用,而是客户端调用
 * 3.刷新Token的有效时间
 */
@Component
public class CommonFilter implements GlobalFilter {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        /**
         * pre拦截逻辑
         * 在请求去到微服务之前,做了两个处理
         * 1.把客户端真实IP通过请求同的方式传递给微服务
         * 2.在请求头中添加FEIGN_REQUEST的请求头,值为0,标记请求不是Feign调用,而是客户端调用
         */
        ServerHttpRequest request = exchange.getRequest().mutate().
                /*在过滤器中拿到ip*/
                header(CommonConstants.REAL_IP,exchange.getRequest().getRemoteAddress().getHostString()).
                header(CommonConstants.FEIGN_REQUEST_KEY,CommonConstants.FEIGN_REQUEST_FALSE).
                build();
        return chain.filter(exchange.mutate().request(request).build()).then(Mono.fromRunnable(()->{
            /**
             * post拦截逻辑
             * 在请求执行完微服务之后,需要刷新token在redis的时间
             * 判断token不为空 && Redis还存在这个token对于的key,这时候需要延长Redis中对应key的有效时间.
             * 没有带token 不会走这里
             */
            String token,redisKey;
            if(!StringUtils.isEmpty(token =exchange.getRequest().getHeaders().getFirst(CommonConstants.TOKEN_NAME))
                    && redisTemplate.hasKey(CommonRedisKey.USER_TOKEN.getRealKey(token))){
                // 我给key 许时间  每次续30分钟
                String s  = CommonRedisKey.USER_TOKEN.getRealKey(token);
                redisTemplate.expire(s  , CommonRedisKey.USER_TOKEN.getExpireTime(), CommonRedisKey.USER_TOKEN.getUnit());
            }
        }));
    }
}

Springcache的使用 
缓存的加载流程 一般如果我们的数据做缓存的话,怎么做的加载
三层缓存框架
复制代码
加依赖pom
复制代码
注解实现这一套操作,@EnableCaching 来实现
既可以使用redis 也可以采用gava
SpringCache+redis
我第一次@Cacheable 的时候  会走数据库查询 ,第二次就不会走了, 因为我加了缓存
我通过这个方式我存储在redis中就会产生一个key


复制代码
我们存储在了redis 中了,我只需要加一个注解 就可以了  这就是Springcache的基本使用
我们默认redis 中设置30分钟
###################################################
我们还要维护一致性问题  数据库改变了  怎么删除redis
@cacheable 实现数据的缓存  我们看到的效果就是 第一次执行的时候执行sql
  {缓存时间 一致性时间 比方说我们配置了半个小时有效的
//
cacheConfig 配置了缓存默认的有效时间 30分钟
30分钟内 如果用户发生修改了 就会导致数据不一致
mysql和redis数据不一致的场景


双写 更新mysql 就把redis也更新了
删除 先删除redis  再更新数据库  cache
复制代码
呢我如果30分钟不到的话,我执行了updateInfo (delete缓存)
我再执行userInfo  我缓存查不到就的从db中查询了 (select db) 然后放缓存

将用户信息和账号密码信息分开
相关推荐
坐吃山猪11 天前
OpenClaw04_Gateway常见问题
网络·gateway·openclaw
三水不滴12 天前
利用SpringCloud Gateway 重试 + 降级解决第三方接口频繁超时问题,提升性能
经验分享·笔记·后端·spring·spring cloud·gateway
知识即是力量ol12 天前
微服务架构:从入门到进阶完全指南
java·spring cloud·微服务·nacos·架构·gateway·feign
j2001032212 天前
Gateway—— 高级流量路由
gateway·k8s
笨蛋不要掉眼泪13 天前
Spring Cloud Gateway 核心篇:深入解析过滤器(Filter)机制与实战
java·服务器·网络·后端·微服务·gateway
笨蛋不要掉眼泪13 天前
Spring Cloud Gateway 扩展:全局跨域配置
java·分布式·微服务·架构·gateway
love530love15 天前
ZeroClaw Reflex UI完整搭建流程——ZeroClaw Gateway + LM Studio + Reflex 本地 AI 管理面板
人工智能·windows·gateway·lm studio·reflex·openclaw·zeroclaw
利刃大大17 天前
【SpringCloud】Gateway Filter Factories && 过滤器执行顺序 && 自定义过滤器
java·后端·网关·spring cloud·gateway
2401_8341208717 天前
spring-cloud-kubernetes与SpringCloud Gateway
spring cloud·kubernetes·gateway
猫头虎17 天前
web开发常见问题解决方案大全:502/503 Bad Gateway/Connection reset/504 timed out/400 Bad Request/401 Unauthorized
运维·前端·nginx·http·https·gateway·openresty