当前位置: 首页 > news >正文

拦截器配置,FeignClient根据业务规则实现微服务动态路由

文章目录

    • 业务场景
    • 拦截器用法
    • Open Feign介绍

业务场景

  • 我们服务使用Spring Cloud微服务架构,使用Spring Cloud Gateway 作为网关,使用 Spring Cloud OpenFeign 作为服务间通信方式
  • 我们现在做的信控平台,主要功能之一就是对路口信号机进行管控和实时展示,平台通过通信服务与信号机设备连接
  • 当信号机数量比较多时,单个通信服务就会存在性能瓶颈,需要将多个通信服务部署到不同的服务器
  • 基于我们的业务特性,我们需要保证某一台信号机只会和某一个通信服务连接(在线、离线、告警等业务需求)
  • 我们在维护信号机设备时,会给它分配一个通信服务。根据实际的业务需求(例如不同支队),分配到对应的通信服务,这样就有了一个信号机设备编号与通信服务的绑定关系
  • 由于前期已经有了大量的业务编码,这个需求不能大规模改动代码,要统一拦截处理
  • 由于这个是根据具体的业务需求去分配到不同的服务,没有规律,没法在网关里判断和转发处理,而且我们的服务间调用,都是内网,也没有经过网关服务
  • 于是想到FeignClient的特性,在它的拦截器里统一处理,根据业务规则,实现微服务动态路由,路由到需要的服务里

拦截器用法

  • 首先我们看下FeignClient类UnitClient的代码,使用了自定义的一个注解AuthorizedFeignClient
@AuthorizedFeignClient(name = "unit", url = "${microservices.unit}")
@Component
public interface UnitClient {/*** 上传数据*/@GetMapping("/uploadAllUnit/{unitId}")ResponseEntity<ResponseBean> uploadAllUnit(@PathVariable String unitId);/*** 关灯*/@PostMapping("/lampOffControl/{unitId}")ResponseEntity<RequestResult> lampOffControl(@PathVariable String unitId);}
// 其他省略
  • 注解类AuthorizedFeignClient,其实就是FeignClient基础上指定了一个配置类OAuth2InterceptedFeignConfiguration
package com.newatc.core.client;import java.lang.annotation.*;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.core.annotation.AliasFor;@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@FeignClient
public @interface AuthorizedFeignClient {@AliasFor(annotation = FeignClient.class, attribute = "name")String name() default "";/*** A custom {@code @Configuration} for the feign client.** Can contain override {@code @Bean} definition for the pieces that* make up the client, for instance {@link feign.codec.Decoder},* {@link feign.codec.Encoder}, {@link feign.Contract}.** @return the custom {@code @Configuration} for the feign client.* @see FeignClientsConfiguration for the defaults.*/@AliasFor(annotation = FeignClient.class, attribute = "configuration")Class<?>[] configuration() default OAuth2InterceptedFeignConfiguration.class;/*** An absolute URL or resolvable hostname (the protocol is optional).* @return the URL.*/String url() default "";/*** Whether 404s should be decoded instead of throwing FeignExceptions.* @return true if 404s will be decoded; false otherwise.*/boolean decode404() default false;/*** Fallback class for the specified Feign client interface. The fallback class must* implement the interface annotated by this annotation and be a valid Spring bean.* @return the fallback class for the specified Feign client interface.*/Class<?> fallback() default void.class;/*** Path prefix to be used by all method-level mappings.* @return the path prefix to be used by all method-level mappings.*/String path() default "";
}
  • 配置类OAuth2InterceptedFeignConfiguration,这里指定了拦截器TokenRelayRequestInterceptor
package com.newatc.core.client;import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;public class OAuth2InterceptedFeignConfiguration {@Bean(name = "oauth2RequestInterceptor")public RequestInterceptor getOAuth2RequestInterceptor() {return new TokenRelayRequestInterceptor();}
}
  • 找到了open feign接口现在用的拦截器,在拦截器里修改代码即可
  • 通过修改URL,路由到对应的服务,以下两种都可以
  • template.target(host)
  • template.feignTarget(new Target.HardCodedTarget<>(target.type(), target.name(), host);
  • 总结一下,查看下使用@FeignClient注解的类,有没有指定配置和拦截器,找到拦截器,在拦截器里重新指定RequestTemplate的目标路由即可

Open Feign介绍

OpenFeign是一个基于Java的声明式HTTP客户端,它简化了编写基于HTTP的API的代码。它使用了注解来定义和配置HTTP API,并且自动地将这些API转换为相应的HTTP请求。

OpenFeign的特点包括:

  1. 声明式API:使用Java接口和注解定义和配置HTTP API,无需手动拼接URL和参数。

  2. 支持多种编码器:支持多种编码器,包括常见的JSON和XML编码器,以及自定义的编码器。

  3. 内置负载均衡:集成了Ribbon负载均衡器,可以轻松地对请求进行负载均衡。

  4. 支持熔断器:集成了Hystrix熔断器,可以在服务不可用时阻止不稳定的网络请求。

  5. 易于集成:可以轻松地集成到Spring应用中,也可以独立使用。

通过使用OpenFeign,开发人员可以更加简单地与其他服务进行通信,而不需要手动编写大量的HTTP请求代码。这使得开发人员可以更加专注于业务逻辑的实现,提高了开发效率。

http://www.lryc.cn/news/296586.html

相关文章:

  • 预测模型:MATLAB线性回归
  • 【人工智能】神奇的Embedding:文本变向量,大语言模型智慧密码解析(10)
  • Redis + Lua 实现分布式限流器
  • ruoyi若依框架SpringSecurity实现分析
  • Habitat环境学习四:Habitat-sim基础用于导航——使用导航网格NavMesh
  • python学习笔记 -- 字符串
  • 2024年GPT如何发展?
  • 从REPR设计模式看 .NET的新生代类库FastEndpoints的威力
  • 前端入门:(五)JavaScript 续
  • 研究多态恶意软件,探讨网络安全与AI
  • linux驱动工作原理
  • Rust语言入门(第3篇)
  • 电脑服务器离线安装.net framework 3.5解决方案(错误:0x8024402c )(如何确定当前系统是否安装NET Framework 3.5)
  • Three.js学习8:基础贴图
  • 【Linux】进程学习(二):进程状态
  • Spring Boot 笔记 003 Bean注册
  • PCIE 参考时钟架构
  • 【开源】JAVA+Vue.js实现在线课程教学系统
  • 计算机网络(第六版)复习提纲29
  • 有道ai写作,突破免费限制,无限制使用
  • node.js 使用 elementtree 生成思维导图 Freemind 文件
  • Vue中路由守卫的详细应用
  • Flink Checkpoint过程
  • 【Java程序代理与系统代理关系】Java程序代理与系统代理关系优先级及覆盖关系
  • MQ,RabbitMQ,SpringAMQP的原理与实操
  • Vue 3 + Koa2 + MySQL 开发和上线部署个人网站
  • uniapp踩坑之项目:简易版不同角色显示不一样的tabbar和页面
  • 源支付V7开心1.9修复版,非网络上泛滥不能那种
  • Gitlab和Jenkins集成 实现CI (二)
  • Java:内部类、枚举、泛型以及常用API --黑马笔记