SpringCoud-OpenFeign测试远程调用
程序员文章站
2022-03-15 14:04:54
...
一 引入OpenFeign
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
二 编写一个远程接口,告诉SpringCLoud这个接口需要被远程调用
修改“com.bigdata.gulimall.coupon.controller.CouponController”,添加以下controller方法:
@RequestMapping("/member/list")
public R membercoupons(){
CouponEntity couponEntity = new CouponEntity();
couponEntity.setCouponName("满100减10");
return R.ok().put("coupons",Arrays.asList(couponEntity));
}
新建“com.bigdata.gulimall.member.feign.CouponFeignService”接口
/**
* 这是一个声明式的远程调用
*/
@FeignClient("gulimall-coupon")
public interface CouponFeignService {
@RequestMapping("/coupon/coupon/member/list")
public R membercoupons();
}
修改“com.bigdata.gulimall.member.GulimallMemberApplication”类,添加上"@EnableFeignClients":开启远程调用功能
@EnableFeignClients(basePackages = "com.atguigu.gulimall.member.feign")
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallMemberApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallMemberApplication.class, args);
}
}
三 远程调用的使用
@RequestMapping("/coupons")
public R test(){
MemberEntity memberEntity = new MemberEntity();
memberEntity.setNickname("张三");
R membercoupons = couponFeignService.membercoupons();
return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
}
四 启动 GulimallMemberApplication 和 GulimallMemberApplication微服务进行测试
1 浏览器输入 http://localhost:8000/member/member/coupons
2 输出结果