(一)
现在common模块的pom.xml文件中引入依赖:
<!-- 负载均衡--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> <version>2.2.7.RELEASE</version> </dependency> <!-- 服务注册/发现--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <exclusions> <!-- 将ribbon排除 --> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </exclusion> </exclusions> </dependency>
注意还得加上个依赖管理:
<dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(二)在被调用的服务中的controller的某一个类中加入方法:例如:
@RequestMapping("/member/list1") public R membercoupons(){ System.out.println("membercoupons被执行"); CouponEntity couponEntity=new CouponEntity(); couponEntity.setCouponName("满100减10"); return R.ok().put("coupons",Arrays.asList(couponEntity)); }
(三)在调用者服务中的member包下新建一个包(和dao包以及controller包平级)
feign用于写接口:例如:
@FeignClient("gulimall-coupon")
public interface CouponFeignService {
@RequestMapping("/coupon/coupon/member/list1")
public R membercoupons();
}
(四)编写调用服务的代码:例如:
@Autowired
CouponFeignService couponFeignService;
@RequestMapping("/coupons")
public R test(){
System.out.println("test被执行");
MemberEntity memberEntity=new MemberEntity();
memberEntity.setNickname("张三");
R membercoupons=couponFeignService.membercoupons();
return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
}
(五)在主启动类中加入这个:
@EnableFeignClients(basePackages = "com.atguigu.gulimall.member.feign")
主启动类如下:
@EnableFeignClients(basePackages = "com.atguigu.gulimall.member.feign")
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallMemberApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallMemberApplication.class, args);
}
}
本文含有隐藏内容,请 开通VIP 后查看