OpenFiegn
具体调用方法及过程见本章节统计目录 SpringCloudAlibaba下的 远程调用OpenFeign 章节.
此处以gulimall-product模块中,调用gulimall-coupon的功能来演示.
首先,要保证两个模块都注册到了nacos注册中心里.
创建feign包,feign包下创建接口CouponFeignService,用来调用gulimall-coupon的功能:
package com.atguigu.gulimall.product.feign;import org.springframework.cloud.openfeign.FeignClient;/*** 本类说明:** @author yuanhai* @date 2022年02月18日*/@FeignClient("gulimall-coupon") // 声明 是调用 gulimall-coupon 服务public interface CouponFeignService {}
gulimall-product要在启动类上开启远程调用功能:
// 想要远程调用别的服务,就要在启动类上标注@EnableFeignClients注解,开启远程调用功能// basePackages = "com.atguigu.gulimall.product.feign 可以不写,也可以写上去显式指定feign接口都在哪个为位置@EnableFeignClients(basePackages = "com.atguigu.gulimall.product.feign")@EnableDiscoveryClient@MapperScan("com.atguigu.gulimall.product.dao")@SpringBootApplicationpublic class GulimallProductApplication {public static void main(String[] args) {SpringApplication.run(GulimallProductApplication.class, args);}}
在CouponFeignService中,写好要调用的功能后,
在gulimall-product的类中,注入CouponFeignService后就可以使用CouponFeignService的对象来调用功能了.
TO领域对象模型
两个微服务模块,都要用到的对象.
比如gulimall-product服务向guliamll-coupon服务传递这个对象,
guliamll-coupon服务需要接收这个对象做处理.这种设为TO.
这时gulimall-product,guliamll-coupon都要用,我们把这个TO对象放在gulimall-common中(AB两个服务都会引入gulimall-common模块)
fiegn接口案例以及细节(远程调用过程)
/*
1、CouponFeignService.saveSpuBounds(spuBoundTo);
1)、@RequestBody将这个对象转为json。
2)、找到gulimall-coupon服务,给/coupon/spubounds/save发送请求。
将上一步转的json放在请求体位置,发送请求;
3)、对方服务收到请求。请求体里有json数据。
(@RequestBody SpuBoundsEntity spuBounds);将请求体的json转为SpuBoundsEntity;
只要json数据模型是兼容的。双方服务无需使用同一个to** */
package com.atguigu.gulimall.product.feign;import com.atguigu.common.to.SpuBoundTO;import com.atguigu.common.utils.R;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.ResponseBody;/*** 本类说明:** @author yuanhai* @date 2022年02月18日*/@FeignClient("gulimall-coupon") // 声明 是调用 gulimall-coupon 服务public interface CouponFeignService {/*** 1、CouponFeignService.saveSpuBounds(spuBoundTo);* 1)、@RequestBody将这个对象转为json。* 2)、找到gulimall-coupon服务,给/coupon/spubounds/save发送请求。* 将上一步转的json放在请求体位置,发送请求;* 3)、对方服务收到请求。请求体里有json数据。* (@RequestBody SpuBoundsEntity spuBounds);将请求体的json转为SpuBoundsEntity;* 只要json数据模型是兼容的。双方服务无需使用同一个to* @param spuBoundTO* @return*/@PostMapping("/coupon/spubounds/save")R saveBounds(@RequestBody SpuBoundTO spuBoundTO);}
远程调用是否经过网关
package com.atguigu.gulimall.ware.feign;import com.atguigu.common.utils.R;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;@FeignClient("gulimall-product")public interface ProductFeignService {/*** /product/skuinfo/info/{skuId}*** 1)、让所有请求过网关;* 1、@FeignClient("gulimall-gateway"):给gulimall-gateway所在的机器发请求* 2、/api/product/skuinfo/info/{skuId}* 2)、直接让后台指定服务处理* 1、@FeignClient("gulimall-gateway")* 2、/product/skuinfo/info/{skuId}** @return*/@RequestMapping("/product/skuinfo/info/{skuId}")public R info(@PathVariable("skuId") Long skuId);}
