使用OpenFeign远程调用别的微服务(member调用coupon中的方法)
原创
©著作权归作者所有:来自51CTO博客作者wx58763b21ae3ed的原创作品,请联系作者获取转载授权,否则将追究法律责任
1、在项目wysmall中创建两个模块
2、在微服务模块中引入openFeign
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3、在mall-coupon中创建被调用的方法
@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
@Autowired
private CouponService couponService;
@RequestMapping("/member/list")
public R memberCoupons(){
CouponEntity couponEntity = new CouponEntity();
couponEntity.setCouponName("满100减10");
return R.ok().put("coupons", Arrays.asList(couponEntity));
}
}
4、在mall-member中创建feign接口
@FeignClient("mall-coupon")
public interface CouponFeignService {
/**
* 调用优惠券微服务中的方法
* @return
*/
@RequestMapping("coupon/coupon/member/list")
public R memberCoupons();
}
5、在mall-member启用feign
6、实现调用
@RequestMapping("coupon")
public R getCoupon(){
MemberEntity memberEntity = new MemberEntity();
memberEntity.setNickname("tom");
R r = couponFeignService.memberCoupons();
return R.ok().put("member", memberEntity).put("coupons", r.get("coupons"));
}