springcloudfeign的调用过程

编程

1. 这两个服务要同时注册到nacos中。

2.引入open-feign。

3.创建feign包编写接口CouponFeignService

package com.atguigu.gulimall.member.feign;

import com.atguigu.common.utils.R;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("gulimall-coupon")

public interface CouponFeignService {

@RequestMapping("coupon/coupon/member/list")

public R membercoupons();

}

注意:@RequestMapping("coupon/coupon/member/list")中的地址

是在coupon服务中的congtroller的方法

@RestController

@RequestMapping("coupon/coupon")

public class CouponController {

...

@RequestMapping("/member/list")

public R membercoupons(){

CouponEntity couponEntity = new CouponEntity();

couponEntity.setCouponName("满100减50");

return R.ok().put("coupons",Arrays.asList(couponEntity));

}

...

}

4.@EnableFeignClients 加入到启动项目代码中

@EnableFeignClients(basePackages = "com.atguigu.gulimall.member.feign")

@SpringBootApplication

@EnableDiscoveryClient

public class GulimallMemberApplication {

public static void main(String[] args) {

SpringApplication.run(GulimallMemberApplication.class, args);

}

}

5.方法中调用feign接口的方法

@RestController

@RequestMapping("member/member")

public class MemberController {

...

//注入feign的接口

@Autowired

private CouponFeignService couponFeignService;

@RequestMapping("/coupons")

public R test(){

MemberEntity memberEntity = new MemberEntity();

memberEntity.setNickname("张山");

R membercoupons = couponFeignService.membercoupons();//调用feign的方法

return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));

}

...

}

 

以上是 springcloudfeign的调用过程 的全部内容, 来源链接: utcz.com/z/517958.html

回到顶部