驻马店网站建设zmdsem,邢台柏乡县建设局网站,网站建设与网页设计论述题,如何提高你的网站的粘性#x1f49d;#x1f49d;#x1f49d;欢迎来到我的博客#xff0c;很高兴能够在这里和您见面#xff01;希望您在这里可以感受到一份轻松愉快的氛围#xff0c;不仅可以获得有趣的内容和知识#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学… 欢迎来到我的博客很高兴能够在这里和您见面希望您在这里可以感受到一份轻松愉快的氛围不仅可以获得有趣的内容和知识也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学习,不断总结,共同进步,活到老学到老导航 檀越剑指大厂系列:全面总结 java 核心技术点,如集合,jvm,并发编程 redis,kafka,Spring,微服务,Netty 等常用开发工具系列:罗列常用的开发工具,如 IDEA,Mac,Alfred,electerm,Git,typora,apifox 等数据库系列:详细总结了常用数据库 mysql 技术点,以及工作中遇到的 mysql 问题等懒人运维系列:总结好用的命令,解放双手不香吗?能用一个命令完成绝不用两个操作数据结构与算法系列:总结数据结构和算法,不同类型针对性训练,提升编程思维,剑指大厂 非常期待和您一起在这个小小的网络世界里共同探索、学习和成长。 ✨✨ 欢迎订阅本专栏 ✨✨ 博客目录 一.简单介绍1.什么是 FeignClient?2.FeignClient 优点? 二.基本使用1.引入依赖2.获取 token 接口3.RequestParam4.Interceptor5.配置文件类6.业务接口7.yaml 配置文件8.使用总结 三.原理解析1.value/name2.url3.path4.configuration5.fallback6.fallbackFactory7.decode404 一.简单介绍
1.什么是 FeignClient?
FeignClient 是 Spring Cloud 中的一个注解用于创建基于接口的声明式服务客户端。它是在微服务架构中用于进行服务之间通信的一种方式。通过 FeignClient您可以定义一个接口该接口包含要调用的远程服务的方法而 Feign 将自动处理底层的 HTTP 请求和负载均衡。
2.FeignClient 优点?
FeignClient 在微服务架构中的使用具有多个优点和特点这使得它成为一个方便且强大的工具 声明式 REST 客户端 FeignClient 允许您使用声明式的方式定义服务间的 HTTP 调用。通过简单地定义一个接口而不需要手动编写 HTTP 请求您可以将服务调用抽象为接口的方法。 集成了 Ribbon 负载均衡 FeignClient 集成了 Netflix 的 Ribbon 负载均衡器自动处理了服务的负载均衡。这使得微服务之间的调用更加健壮和可靠。 支持服务发现 默认情况下FeignClient 集成了 Eureka 服务发现允许您使用服务的逻辑名称而不是直接的 IP 地址进行服务调用。这增加了服务调用的灵活性。 支持多种注解 除了 FeignClientFeign 还支持一系列其他的注解如 RequestMapping、RequestParam 等使得您可以在接口上使用这些注解定义 HTTP 请求的各个方面。 集成了 Hystrix 进行服务降级 FeignClient 支持集成 Hystrix可以通过设置 fallback 或 fallbackFactory 属性定义服务降级的逻辑。当远程服务不可用时可以提供备选方案防止整个系统崩溃。 支持自定义配置 通过 configuration 属性您可以自定义 Feign 客户端的配置包括连接超时、读取超时、重试策略等。这使得您可以根据实际需求进行灵活的配置。 集成了 Spring Cloud Contract Spring Cloud Contract 可以通过测试契约来确保服务之间的契约一致性。FeignClient 集成了 Spring Cloud Contract使得可以通过契约来测试和验证服务之间的通信。 简化代码 使用 FeignClient 可以大大简化微服务之间的通信代码。由于 Feign 处理了底层的 HTTP 请求和负载均衡开发者只需要关注业务逻辑使得代码更加清晰简洁。
FeignClient 是一个强大的工具它简化了微服务之间的通信提高了开发效率同时集成了一系列的微服务治理功能使得服务调用更加可靠和灵活。
二.基本使用
1.引入依赖
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactIdversion2.2.2.RELEASE/version
/dependency2.获取 token 接口
使用 FeignClient 封装获取 token 的接口
FeignClient(url ${xxx.open-api.host},value xxx-open-api,contextId oauth,path /cas/oauth
)
public interface AuthOpenApi {Log(xxx-open-api获取token)GetMapping(/token)TokenResponse getToken(SpringQueryMap GetTokenRequest request);
}3.RequestParam
请求参数类
Data
public class GetTokenRequest {private String scope xxx;private String grant_type xxx;private String client_id;private String client_secret;public GetTokenRequest() {this.client_id SpringContextHolder.getBean(OpenApiProperties.class).getClientId();this.client_secret SpringContextHolder.getBean(OpenApiProperties.class).getSecret();}
}4.Interceptor
拦截器,用于给 FeignClient 添加自定义拦截器
Slf4j
public class OpenApiRequestInterceptor implements RequestInterceptor {Autowiredprivate JedisClient JedisClient;Autowiredprivate AuthOpenApi AuthOpenApi;Autowiredprivate OpenApiProperties OpenApiProperties;Overridepublic void apply(RequestTemplate requestTemplate) {String accessToken JedisClient.get(CommonConstant.REDIS__KEY_PREFIX accessToken);if (StringUtils.isEmpty(accessToken)) {TokenResponse response AuthOpenApi.getToken(new GetTokenRequest());if (response.getCode() ! 200) {throw new ApplicationException(获取 accessToken失败);}accessToken response.getData().getAccessToken();final int expiresIn response.getData().getExpiresIn();log.info(apply() called with: expiresIn [ expiresIn ]);JedisClient.set(CommonConstant.REDIS__KEY_PREFIX accessToken, accessToken, Math.max(expiresIn - 10, 1));}long timestamp System.currentTimeMillis();String requestId UUID.randomUUID().toString();String str OpenApiProperties.getClientId() timestamp requestId accessToken this.OpenApiProperties.getSecret();String signature DigestUtils.md5Hex(str).toUpperCase();requestTemplate.header(clientId, this.OpenApiProperties.getClientId());requestTemplate.header(timestamp, timestamp );requestTemplate.header(requestId, requestId);requestTemplate.header(signatureMethod, MD5);requestTemplate.header(accessToken, accessToken);requestTemplate.header(signature, signature);}
}5.配置文件类
鉴权需要的参数配置信息
Configuration
ConfigurationProperties(prefix open-api)
Data
public class OpenApiProperties {private String url;private String clientId;private String secret;private String aesKey;private String aesIv;
}开启 FeignClients 的功能
EnableFeignClients
Configuration
public class OpenFeignConfig {
}6.业务接口
获取图片的权限
FeignClient(url ${open-api.host},value open-api,configuration {OpenApiRequestInterceptor.class},contextId picAuth,path /permission-portal/picture/auth
)
public interface PicOpenApi {Log(open-api图片授权模块)GetMappingPicResultDto auth(RequestParam(value userNo) String userNo);
}7.yaml 配置文件
open-api:host: xxxxxclientId: insightsecret: xxxxaesKey: xxxxaesIv: xxxx8.使用总结
首先用FeignClient 定义了 2 个接口一个是获取 token 的接口一个是获取图片权限的接口在获取图片权限的接口中添加了 configuration 属性该属性指向 OpenApiRequestInterceptor 类,在请求获取图片接口的时候会前置处理拦截器中的逻辑在拦截器中我们调用的是获取 token 的接口并解析返回结果放入到了 http 的请求头中非常方便的给获取图片的接口添加了 header 鉴权信息。
三.原理解析
1.value/name
value/name: 用于指定目标服务的名称。value 和 name 都可以用来设置服务的名称它们是互换的。这是 FeignClient 唯一需要指定的参数。
FeignClient(value example-service)
public interface ExampleFeignClient {// ...
}2.url
url: 如果不想使用服务发现可以使用 url 参数指定目标服务的 URL。这个 URL 可以是完整的服务地址。
FeignClient(url http://example.com)
public interface ExampleFeignClient {// ...
}3.path
path: 用于为 Feign 客户端的所有请求添加一个基本路径。
FeignClient(value example-service, path /api)
public interface ExampleFeignClient {// ...
}4.configuration
configuration: 用于指定 Feign 客户端的配置类可以配置连接超时、重试策略、拦截器等。可以指定一个也可以指定多个非常给力的功能参数。
FeignClient(value example-service, configuration MyFeignConfig.class)
public interface ExampleFeignClient {// ...
}5.fallback
fallback: 用于指定一个降级处理的类当调用失败时会执行该类的方法。
FeignClient(value example-service, fallback ExampleFallback.class)
public interface ExampleFeignClient {// ...
}6.fallbackFactory
fallbackFactory: 与 fallback 类似用于指定一个降级处理的工厂类可以在工厂类中对异常进行更详细的处理。
FeignClient(value example-service, fallbackFactory ExampleFallbackFactory.class)
public interface ExampleFeignClient {// ...
}7.decode404
decode404: 默认情况下Feign 不会解码 404 响应。通过设置 decode404 true可以让 Feign 解码 404 响应。
FeignClient(value example-service, decode404 true)
public interface ExampleFeignClient {// ...
}FeignClient 注解的参数提供了灵活性和配置选项以适应不同的微服务调用场景。Feign 会根据这些参数配置底层的 HTTP 请求实现服务之间的通信。 觉得有用的话点个赞 呗。 ❤️❤️❤️本人水平有限如有纰漏欢迎各位大佬评论批评指正 如果觉得这篇文对你有帮助的话也请给个点赞、收藏下吧非常感谢! Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧