@RequestMapping("/member/list") public R membercoupons(){ CouponEntity couponEntity = newCouponEntity(); couponEntity.setCouponName("满一百减十"); return R.ok().put("coupons",Arrays.asList(couponEntity)); }
在调用方编写Feign 接口
1 2 3 4 5 6 7 8 9 10
//标明远程客户端 注解 参数为被调用方的在注册中心中配置的服务名 @Service @FeignClient("gulimall-coupon") public interface CouponFenginService { //与被调用方的请求 路径相同 @RequestMapping("/coupon/coupon/member/list") //与被调用方的请求 方法相同 public R membercoupons(); }
在调用方服务中启动类开启调用功能
1 2 3 4 5 6 7 8 9 10 11 12 13
//参数为fegin的包的全包名 @EnableFeignClients(basePackages = "com.jhj.gulimall.member.fegin") @EnableDiscoveryClient @SpringBootApplication public classGulimallMemberApplication {
public staticvoidmain(String[] args) { SpringApplication.run(GulimallMemberApplication.class, args); }
}
在调用方中 注入CouponFenginService 进行调用
1 2 3 4 5 6 7 8 9 10 11 12
@Autowired CouponFenginService couponFenginService; @RequestMapping("/coupons") public R test(){ MemberEntity memberEntity=newMemberEntity(); memberEntity.setNickname("张三");
R membercoupons = couponFenginService.membercoupons();