赞
踩
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.1.0.RELEASE</version> </dependency>
- 1
- 2
- 3
- 4
- 5
2.提供方的接口
@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
@RequestMapping("/member")
public R memberCoupons() {
CouponEntity couponEntity = new CouponEntity();
couponEntity.setAmount(new BigDecimal(12));
couponEntity.setCode("you");
couponEntity.setCouponName("一张优惠券");
return R.ok().put("coupons", Arrays.asList(couponEntity));
}
}
3.在调用方创建一个feign 接口类,里面放入需要调用其他微服务调用的所有接口签名
@FeignClient("mall-coupon")
public interface CouponFeignService {
@RequestMapping("/coupon/coupon/member")
public R memberCoupons();
}
//mall-coupon为微服务注册时的application Name
//@RequetMapping里的注解必须为接口详细请求地址
4.使用注解@EnableFeignClients开启远程调用功能
@EnableFeignClients(basePackages = "com.sz.mall.member.feign")
@SpringBootApplication
public class MallMemberApplication {
public static void main(String[] args) {
SpringApplication.run(MallMemberApplication.class, args);
}
}
@RestController @RequestMapping("member/member") public class MemberController { @Autowired private CouponFeignService couponFeignService; @RequestMapping("/coupons") public R getCouponByMember() { MemberEntity memberEntity = new MemberEntity(); memberEntity.setNickname("小怪"); R memberCoupons = couponFeignService.memberCoupons(); return R.ok().put("member", memberEntity).put("coupons", memberCoupons.get("coupons")); }
6.测试结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。