赞
踩
1、pom引用:
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
2、启动类 标注@EnableFeignClients(basePackages = "你的feign接口存放包")
- //开启服务调用
- @EnableFeignClients(basePackages = "com.sx.gulimall.member.feign")
- //开启服务注册
- @EnableDiscoveryClient
- @SpringBootApplication
- public class GulimallMemberApplication {
- public static void main(String[] args) {
- SpringApplication.run(GulimallMemberApplication.class, args);
- }
- }
3、在feign接口存放包(com.sx.gulimall.member.feign)下面创建一个接口CouponFeignService
4、feign包下CouponFeignService接口里填写如下内容
- package com.sx.gulimall.member.feign;
-
- import com.sx.gulimall.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();
- }
很好理解,这个@FeignClient的报名指的是服务注册里的gulimall-coupon服务,@RequestMapping里的url指的是gulimall-coupon服务的请求url
下面红框是gulimall-coupon服务的coupon/coupon/member/list 请求接口的执行内容,可以发现该请求接口绑定的方法也是public R memberCoupons(),和client 端CouponFeignService接口的抽象方法相互关联
都把open feign的执行叫做服务调用,其中的原理就是执行了一个路由获取算法,算法多种多样,轮询,hash,权重...
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。