赞
踩
openfeign是一个java的http客户端,用来简化http调用
Feign由五大部分组成,
由于刚开始接触 feign ,比较关注的 clients 跟 encoders/decoders
在Feign中,Client是一个非常重要的组件,Feign最终发送Request请求以及接收Response响应都是由Client组件来完成的。
Client在Feign源码中是一个接口,在默认情况下,Client的实现类是Client.Default。
Client.Default是由HttpURLConnection来实现网络请求的。
另外,Client还支持HttpClient和OkHttp来进行网络请求。
如果想要在Feign中使用OkHttp作为网络请求框架,则只需要在pom文件中加上feign-okhttp的依赖
使用Spring Cloud组件时,微服务之间的调用是通过HTTP实现的,同时也是需要把服务接口在公用模块定义出来,其它微服务就可以直接像使用接口一样调用服务即可。
示例如下:
在微服务A模块(MALL_API)中实现了某个接口服务,需要从微服务B(Third-Party)远程去调用微服务A模块(MALL_API)的接口实现。
使用注解@EnableFeignClients
启用feign客户端;
- @SpringBootApplication
- @EnableBaseFeignClients
- public class ProjectAuthApplication {
- public static void main(String[] args) {
- SpringApplication.run(ProjectAuthApplication.class, args);
- }
- }
看到, @EnableBaseFeignClients
注解是继承自@EnableFeignClients
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @EnableFeignClients
- public @interface EnableBaseFeignClients {
- String[] value() default {};
- String[] basePackages() default {"com.project.cloud"};
- Class<?>[] basePackageClasses() default {};
- Class<?>[] defaultConfiguration() default {};
- Class<?>[] clients() default {};
- }
- <!--feign 依赖-->
- <dependency>
- <groupId>io.github.openfeign</groupId>
- <artifactId>feign-okhttp</artifactId>
- </dependency>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- <version>3.0.3</version>
- </dependency>
- </dependencies>
- private final FeignPointsRecordService feignPointsRecordService;
-
- private void savePointsRecord(Integer newPoints, Integer oldPoints, String userId) {
- R r = feignPointsRecordService.ddCareSavePointsRecord(newPoints, oldPoints, userId, SecurityConstants.FROM_IN);
- if (!r.isOk()) {
- log.error("savePointsRecord error !userId = {}", userId);
- throw new RuntimeException(r.getMsg());
- }
- }
在公用模块定义使用接口,并加入此声明使用注解@FeignClient
定义Feign客户端
- @FeignClient(contextId = "feignPointsRecordService", value = ServiceNameConstants.MALL_API_SERVICE)
- public interface FeignPointsRecordService {
-
- /**积分变动记录 */
- @GetMapping("/pointsrecord/ddCareSavePointsRecord/{newPoints}/{oldPoints}/{userId}")
- R ddCareSavePointsRecord(@PathVariable("newPoints") Integer newPoints, @PathVariable("oldPoints") Integer oldPoints, @PathVariable("userId") String userId, @RequestHeader(SecurityConstants.FROM) String from);
-
- @PostMapping("/pointsrecord/recharge")
- R pointsrecord(@RequestBody @Valid PointsRecord record, @RequestHeader(SecurityConstants.FROM) String from);
- }
- /*积分变动记录 */
- @ApiOperation(value = "积分变动记录-爱关怀积分充值")
- @GetMapping("/ddCareSavePointsRecord/{newPoints}/{oldPoints}/{userId}")
- @Inside
- public R ddCareSavePointsRecord(@PathVariable("newPoints") Integer newPoints, @PathVariable("oldPoints") Integer oldPoints, @PathVariable("userId") String userId) {
- TenantContextHolder.setTenantId(CommonConstants.DESAY_SV_TENANT_ID);
- return R.ok(pointsRecordService.ddCareSavePointsRecord(newPoints, oldPoints, userId));
- }
解決方案: 記得需要帶上{id}
feign method GET must not have a request body.超过2个参数时报Method has too many Body parameters:
https://blog.csdn.net/qq_34412985/article/details/108176425
https://blog.51cto.com/u_15242378/2986822
https://blog.csdn.net/qq_34412985/article/details/108176425
feign method GET must not have a request body.超过2个参数时报Method has too many Body parameters:
解決方案:改爲@PostMapping + @RequestBody
feign method GET must not have a request body.
https://www.cnblogs.com/qq376324789/p/14885327.html
https://www.codeleading.com/article/95383859705/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。