SpringCloud Feign用法

1.在目标应用的启动类上添加开启远程fein调用注解:

2.添加一个feign调用的interface

@FeignClient("gulimall-coupon")
public interface CouponFeignService {

    @PostMapping("/coupon/spubounds/save")
     R save(@RequestBody SpuBondTo spuBounds);
}

 接口上添加注解@FeignClient,括号内填入要远程调用的微服务应用名application name; 

3.添加要调用远程服务controller里对应的方法声明,注意是方法声明,没有方法体,这一步需确保:

1.http请求方法要一致,源方法用get请求,调用处也要用get请求;

2.http请求方法路径要完整,@GetMapping和@PostMapping里的路径地址

3.传参方式要一致,比如@RequestBody,@RequestParam,@PathVariable; 

4.返回值要一致;

需要特别指出的是像@RequestBody传参,源方法和feignClient调用处可用不同的对象,只要保证这2个对象有业务需要的部分相同属性即可。

下面贴上代码方便理解

Controller里的方法:

@RestController
@RequestMapping("coupon/spubounds")
public class SpuBoundsController {
    
    @Autowired
    private SpuBoundsService spuBoundsService;
    
    @PostMapping("/save")
    public R save(@RequestBody SpuBoundsEntity spuBounds){
		spuBoundsService.save(spuBounds);
        return R.ok();
    }

SpuBondTo 实体类(前端传参对象)

@Data
public class SpuBondTo {

    private Long spuId;
    private BigDecimal buyBounds;
    private BigDecimal growBounds;
}

controller里的传参,对应数据库表的实体类

@Data
@TableName("sms_spu_bounds")
public class SpuBoundsEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * id
	 */
	@TableId
	private Long id;
	/**
	 * 
	 */
	private Long spuId;
	/**
	 * 成长积分
	 */
	private BigDecimal growBounds;
	/**
	 * 购物积分
	 */
	private BigDecimal buyBounds;
	/**
	 * 优惠生效情况
     */
	private Integer work;

}

这2个对象中有3个属性是一致的,如此部分属性相同即可实现Feign远程调用伟参,当然传同一类型的实体更不用说了。

相关推荐

  1. new Promise

    2024-06-08 03:50:02       26 阅读
  2. qt 定时器

    2024-06-08 03:50:02       41 阅读
  3. fmt

    2024-06-08 03:50:02       37 阅读
  4. not exists

    2024-06-08 03:50:02       41 阅读
  5. 详解WebMvcConfigurer

    2024-06-08 03:50:02       26 阅读
  6. Tinyxml基本

    2024-06-08 03:50:02       39 阅读
  7. man

    2024-06-08 03:50:02       32 阅读
  8. mybatisPlus 常见

    2024-06-08 03:50:02       31 阅读
  9. v-show

    2024-06-08 03:50:02       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-08 03:50:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-08 03:50:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-08 03:50:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-08 03:50:02       18 阅读

热门阅读

  1. AI初识--LLM、ollama、llama都是些个啥?

    2024-06-08 03:50:02       8 阅读
  2. 富格林:总结阻挠欺诈解决措施

    2024-06-08 03:50:02       9 阅读
  3. 【加密与解密】【01】网络安全体系

    2024-06-08 03:50:02       7 阅读
  4. 007.卷积网络-FashionMNIST-正确率90.180

    2024-06-08 03:50:02       11 阅读
  5. C#面:解释什么是扩展方法

    2024-06-08 03:50:02       8 阅读
  6. html 添加元素如何能提升速度

    2024-06-08 03:50:02       8 阅读
  7. 致远OA(A8) REST接口 python版

    2024-06-08 03:50:02       8 阅读
  8. Git 保留空文件夹结构

    2024-06-08 03:50:02       9 阅读