您当前的位置: 首页 > 

java持续实践

暂无认证

  • 2浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

谷粒商城13-使用openfen测试远程调用

java持续实践 发布时间:2020-08-01 19:16:35 ,浏览量:2

使用openfen测试远程调用

测试gulimall-member 调用gulimall-coupon 在gulimall-member引入open-feign

     
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

编写一个接口,告诉SpringCLoud这个接口需要调用远程服务 修改gulimall-coupon 模块的 “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));
}

在gulimall- member模块中, 新建立包feign , 新建“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


//开启feign的远程调用, 并指定远程调用的包路径
@EnableFeignClients(basePackages = "com.atguigu.gulimall.member.feign")
//开启服务注册与发现
@EnableDiscoveryClient
//dao层接口扫描
@MapperScan("com.atguigu.gulimall.member.dao")
@SpringBootApplication
public class GulimallMemberApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallMemberApplication.class, args);
    }

}

3)、测试远程调用功能 在member模块的, 业务controller中, 进行接口的编写 com.bigdata.gulimall.member.controller.MemberController

   @RequestMapping("/coupons")
    public R test(){
        MemberEntity memberEntity=new MemberEntity();
        memberEntity.setNickname("zhangsan");
        R memberCoupons = couponFeignService.memberCoupons();
        return memberCoupons.put("member",memberEntity).put("coupons",memberCoupons.get("coupons"));
    }

4)、访问http://localhost:8000/member/member/coupons 返回的json数据如下, 有优惠券的信息, 代表远程调用成功! 停止“gulimall-coupon”服务,能够看到注册中心显示该服务的健康值为0: 再次访问:http://localhost:8000/member/member/coupons 远程调用失败. 启动“gulimall-coupon”服务,再次访问,又恢复了正常。

远程调用总结步骤

调用服务的一方的操作. 被调用方是不需要任何操作的, 只需要写好接口就行 1.引入open-feign的依赖 2.编写一个feign的接口, 该接口的用@FeignClient注解标明, 表示这个接口需要进行远程调用. 并注明调用服务的名称 在接口中进行方法的编写, 直接复制被调用方的方法即可, 只需要方法名称 ,修饰符,返回值. 并写上接口的完整的请求路径 3.在调用方的主启动类上开启远程调用的注解,并标明feign接口的包路径.

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0392s