SpringCloud学习(五)Feign的使用方式(二)——服务提供者实现Feign接口的形式

上一篇博客介绍了Feign能帮我们解决什么问题以及Feign的第一种使用方式
SpringCloud学习(四)Feign的使用方式(一)——服务提供者RequestMapping一致形式

现在介绍如何通过实现Feign接口的形式,更简单的使用Feign

使用的版本:spring-cloud-openfeign 2.1.0.RELEASE
SpringCloud版本:Greenwich.RELEASE

  1. 新建一个Maven模块,该模块只存放定义的Feign接口(因为消费者和提供者都需要使用该接口,所以将它们抽取出来单独部署成一个模块),添加依赖feign依赖

    	<dependencies>
            <!-- feign依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
                <!-- 这里会自动引入版本,类似parent标签继承 -->
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <!-- <scope>import</scope>解决单继承问题,类似parent标签, -->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Greenwich.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
    
  2. 在该模块下编写相应的服务接口

    package feign.webservice;
    
    import com.fei.common.constant.ServerContant;
    import com.fei.common.model.employee.EmployeeModel;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import java.util.List;
    
    /**
     * @Author: xiaoshijiu
     * @Date: 2019/7/4
     * @Description: 员工feign接口
     * @FeignClient: name表示为哪个服务配置feign申明式服务调用
     */
    @FeignClient(name = ServerContant.SERVER_PROVIDER)
    @RequestMapping("/feign/empl")
    public interface EmployeeWebService {
    
        /**
         * 查询所有员工
         * @return 员工模型列表
         */
        @RequestMapping(value = "/list", method = RequestMethod.GET)
        List<EmployeeModel> list();
    
    }
    
    

    在接口上添加注解@FeignClient(name = ServerContant.SERVER_PROVIDER),表明这是一个Feign客户端,name属性表示具体为哪个服务配置
    然后接口的具体方法可以填写@RequestMapping,请求路径映射

  3. 服务提供者,与第一种方法不同的就是在这里
    第一种方法:服务提供者Controller中方法的@RequestMapping路径映射要和Feign接口中方法的路径映射一样
    现在:服务提供者实现Feign接口

    package com.fei.springcloudprovide8001.webservice.feign;
    
    import com.alibaba.fastjson.JSON;
    import com.fei.common.model.employee.EmployeeModel;
    import com.fei.springcloudprovide8001.employee.EmployeeService;
    import feign.webservice.EmployeeWebService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * @Author: xiaoshijiu
     * @Date: 2019/7/4
     * @Description: 员工feign接口实现类
     */
    @RestController
    public class EmployeeWebServiceImpl implements EmployeeWebService {
    
        @Autowired
        private EmployeeService employeeService;
    
        @Override
        public List<EmployeeModel> list() {
            // 使用阿里巴巴的fastjson进行集合中对象的模型转换
            return JSON.parseArray(JSON.toJSONString(employeeService.list()), EmployeeModel.class);
        }
    }
    
    
  4. 服务消费者,与第一种方法一致,就像调用本地方法一样去调用定义的feign接口就可以了

    package com.fei.springcloudconsumer.consumer.employee.feign;
    
    import com.fei.common.model.employee.EmployeeModel;
    import feign.webservice.EmployeeWebService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Author: xiaoshijiu
     * @Date: 2019/7/4
     * @Description: feign组件,消费服务接口
     */
    @RestController
    @RequestMapping("/consume/feign/empl")
    public class EmployeeFeignController {
    
    	/**
         * 使用了Feign,就像调用本地方法一样,调用远程HTTP请求
         */
        @Autowired
        private EmployeeWebService employeeWebService;
    
        /**
         * 获取员工列表,使用feign组件
         */
        @GetMapping("/list")
        public List<EmployeeModel> list2() {
            List<EmployeeModel> employeeModels = employeeWebService.list();   	     
            return webEmployeeModels;
        }
    }
    
    

    有一点需要注意的是:这里的RequestMapping路径不能和Feign接口定义RequestMapping路径重复

  5. 最后要在服务消费者的主启动类上加上@EnableFeignClients(basePackages = "feign.webservice")注解
    需要注意的是,如果你定义的Feign接口不在主启动类包或其子包下(即不在@ComponentScan扫描范围内),则需添加basePackages属性去指明Feign接口的包位置。
    这一点很重要,不然会报 不能@Autowired EmployeeWebService 的错。

  6. 测试,成功调用,并自带了Ribbon负载均衡的作用

  7. 一点感悟:
    不知道为什么,网上千篇一律的使用Feign实现接口的方法都是在消费者端再重写一个feign接口继承公共接口,然后提供者端实现的是公共接口,然后就说这种方法存在消费者和提供者过度耦合的缺点,但是怎么不去想一下,直接将公共接口变成Feign不就行了吗?为什么还需要在消费者端再定义一层没有必要的接口呢?
    网上千篇一律这样的文章,相互借鉴,用来用去,难道没有一点创新改造的能力吗?
    感悟,勿喷!!

  • 10
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
在Spring Cloud微服务架构中,Nacos是一个注册中心和配置中心。Feign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得更加容易。 使用Feign调用接口需要以下步骤: 1. 在pom.xml中添加Feign依赖 ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 在启动类上添加@EnableFeignClients注解启用Feign客户端 ```java @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 3. 创建接口,并使用@FeignClient注解指定调用的服务名称和服务路径 ```java @FeignClient(name = "service-provider") public interface UserService { @GetMapping("/user/{id}") String getUserById(@PathVariable("id") Long id); } ``` 其中,name属性指定服务名称,GetMapping注解指定服务路径。 4. 在需要使用服务的地方注入UserService并调用方法即可 ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/user/{id}") public String getUserById(@PathVariable("id") Long id) { return userService.getUserById(id); } } ``` 在这个例子中,我们定义了一个名为UserService的Feign客户端,指定了调用的服务名称和服务路径。然后在UserController中注入了UserService并调用了其方法。最终,Feign会自动将该请求转发到名为service-provider的微服务,并返回结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值