关于Feign的使用
1.添加依赖
2.添加注解
在启动类上添加@EnableDiscoveryClient注解
1、可以获取服务信息
2、用于向nacos或者zookeeper作为注册中心的时候提供注册服务
2.1客户端
当我们要使用feign客户端时,启动类上添加注解
@EnableFeignClients(basePackages = "com.xxx.xxx.xxx.feign")
3.创建Feign接口
可以创建一个feign包 ,包里创建一个接口
@FeignClient("gulimall-coupon")
public interface CouponFeignService {
@PostMapping("coupon/spubounds/save")
R saveSpuBounds(@RequestBody SpuBoundsTo spuBoundsTo);
@PostMapping("coupon/skufullreduction/saveInfo")
R saveSkuReduction(@RequestBody SkuReductionTo skuReductionTo);
@FeignClient("gulimall-coupon"):调用nacos服务注册中心里的服务名字,并调用这个服务里的方法。
- @RequestBody将这个对象转为json。
- 找到gulimall-coupon服务,给/coupon/spubounds/save发送请求。
- 将上一步转的json放在请求体位置,发送请求;
- 对方服务收到请求。请求体里有json数据。
- (@RequestBody SpuBoundsEntity spuBounds);将请求体的json转为 SpuBoundsEntity;
- 只要json数据模型是兼容的。双方服务无需使用同一个to