feign 是一个让rest服务调用更简洁的开源项目,很多介绍文章或书也称它为声明式REST调用。传统的web service中,通过引用wsdl来自动生成一些client的代理类(或stub代码),feign跟这个有点类似,但是更灵活。

先回顾一下,上节中service-consumer对服务的调用代码:

    @GetMapping("/order/{userId}/{orderNo}")
public String findOrder(@PathVariable Integer userId, @PathVariable String orderNo) {
UserDTO user = restTemplate.getForEntity("http://SERVICE-PROVIDER-DEMO/user/" + userId, UserDTO.class).getBody();
if (user != null) {
return user.getUserName() + " 的订单" + orderNo + " 找到啦!";
} return "用户不存在!";
}

如果调用的参数比较多,调用的代码会充斥着很多拼装参数这样的代码,不太优雅。另外还有一个问题,通常为了安全起见,一些服务(或服务中的某些方法)可能要求认证后,才能调用,如果每个调用的地方,都要调用登录之类的服务来处理,这类与业务无关的代码就会大量侵入业务逻辑中,不好维护。

下面看看用feign如何改进:

一、添加依赖引用

compile 'org.springframework.cloud:spring-cloud-starter-feign'

二、定义feignClient接口

package com.cnblogs.yjmyzz.spring.cloud.study.service.client;

import com.cnblogs.yjmyzz.spring.cloud.study.dto.UserDTO;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(name = "service-provider-demo", configuration = BasicAuthConfiguration.class)
public interface UserFeignClient { @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
UserDTO findUser(@PathVariable("id") Integer userId); }

这里面一个BasicAuthConfiguration类,也是自己写的

package com.cnblogs.yjmyzz.spring.cloud.study.service.client;

import feign.auth.BasicAuthRequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class BasicAuthConfiguration { @Bean
public BasicAuthRequestInterceptor basicAuthorizationInterceptor() {
return new BasicAuthRequestInterceptor("app01", "passwd01");
}
}

这上面的app01, passwd01,就是服务端分配的用户名及密码

附:服务提供方的application.yml中可参考下面这样设置

security:
basic:
enabled: true
user:
name: app01
password: passwd01

三、feignClient的使用

package com.cnblogs.yjmyzz.spring.cloud.study.service.controller;

import com.cnblogs.yjmyzz.spring.cloud.study.dto.UserDTO;
import com.cnblogs.yjmyzz.spring.cloud.study.service.client.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; @RestController
public class OrderController { @Autowired
private UserFeignClient userFeignClient; @GetMapping("/order/{userId}/{orderNo}")
public String findOrder(@PathVariable Integer userId, @PathVariable String orderNo) {
UserDTO user = userFeignClient.findUser(userId);
if (user != null) {
return user.getUserName() + " 的订单" + orderNo + " 找到啦!";
}
return "用户不存在!";
} }  

  

最后,main入口类上要增加一个注解

package com.cnblogs.yjmyzz.spring.cloud.study.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients; @EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class ServiceConsumer { public static void main(String[] args) {
SpringApplication.run(ServiceConsumer.class, args);
}
}

起作用的主要是@EnableFeignClients这个注解。

spring cloud 学习(3) - feign入门的更多相关文章

  1. Spring Cloud 学习 (三) Feign

    新建 spring-cloud-eureka-feign-client Module pom <parent> <artifactId>spring-cloud-parent& ...

  2. Feign详细使用-Spring Cloud学习第四天(非原创)

    文章大纲 一.Feign是什么二.Feign的基本实现三.Feign的继承特性四.Feign配置详解五.项目源码与参考资料下载六.参考文章   一.Feign是什么 前面几篇文章我们详细的介绍了Rib ...

  3. Spring Boot和Spring Cloud学习资源推荐

    Spring Boot和Spring Cloud学习资源推荐   比较好的学习资源,分享一下. 1.Spring Boot官方文档:http://projects.spring.io/spring-b ...

  4. Spring Cloud 学习笔记(二)——Netflix

    4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...

  5. Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端

    Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...

  6. spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法

    turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...

  7. Spring Cloud实战之初级入门(六)— 服务网关zuul

    目录 1.环境介绍 2.api网关服务 2.1 创建工程 2.3 api网关中使用token机制 2.4 测试 2.5 小结 3.一点点重要的事情 1.环境介绍 好了,不知不觉中我们已经来到了最后一篇 ...

  8. Spring Cloud实战之初级入门(五)— 配置中心服务化与配置实时刷新

    目录 1.环境介绍 2.配置中心服务化 2.1 改造mirco-service-spring-config 2.2 改造mirco-service-provider.mirco-service-con ...

  9. Spring Cloud实战之初级入门(四)— 利用Hystrix实现服务熔断与服务监控

    目录 1.环境介绍 2.服务监控 2.1 加入依赖 2.2 修改配置文件 2.3 修改启动文件 2.4 监控服务 2.5 小结 3. 利用hystrix实现消费服务熔断 3.1 加入服务熔断 3.2 ...

随机推荐

  1. 有关楼层滚动且对应楼层Nav导航高亮显示

    $(document).ready(function(e) { //定义数组,储存楼层距离顶部的高度(floorsTop) var floorsTop=[]; function floorsTopF( ...

  2. 在vue-cli下读取模拟数据请求服务器

    写此记录时vue脚手架的webpack是3.6.0 此文章方法亦可用于vue-cli3,直接在vue.config.js里面添加 本记录使用vue-resource,先安装: cnpm install ...

  3. nodejs 配置服务自启动

    1安装包 输入以下命令,安装需要的包 npm install node-windows -g 2编写自启动js 在目标server.js目录下新建auto_start_nodejs.js文件,将以下j ...

  4. python3之模块psutil系统性能信息

    psutil是个跨平台库,能够轻松实现获取系统运行的进程和系统利用率,包括CPU.内存.磁盘.网络等信息. 它主要应用于信息监控,分析和限制系统资源及进程的管理.它实现了同等命令命令行工具提供的功能, ...

  5. Socket心跳包机制总结【转】

    转自:https://blog.csdn.net/qq_23167527/article/details/54290726 跳包之所以叫心跳包是因为:它像心跳一样每隔固定时间发一次,以此来告诉服务器, ...

  6. IE浏览器如何调试Asp.net的 js代码

    不管我们开发什么项目,都需要使用调试.后端的调试比较简单.前端js调试稍微复杂了一点,但是也别怕,因为我们有很多调试前端js代码的浏览器工具.比如IE浏览器.firefox浏览器.chrome浏览器等 ...

  7. Codeforces 600E - Lomsat gelral 「$Dsu \ on \ tree$模板」

    With $Dsu \ on \ tree$ we can answer queries of this type: How many vertices in the subtree of verte ...

  8. poj1033

    模拟题,注意不需要移动的情况要特殊输出 #include <cstdio> #include <cstring> #include <cstdlib> using ...

  9. Windows安装pycrypto失败记录

    Windows 10家庭中文版,Python 3.6.4, 180824测试前端加密文本在后台揭秘,查询后发现,可以使用pycrypto模块实现,那么,安装它(pip),结果安装失败了. 本文暂时记录 ...

  10. URL传递的参数是UTF-8编码,在打开的页面正常显示(GB2312)的方法

    URL传递的参数采用的是UTF-8编码,在打开的子页面中显示乱码, URL传递的地址形如:http://localhost/test.aspx?orgname=%E5%8B%**%**%**%**&a ...