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. [转载]ASP.NET Error – Adding the specified count to the semaphore would cause it to exceed its maximum count

    http://jwcooney.com/2012/08/13/asp-net-error-adding-the-specified-count-to-the-semaphore-would-cause ...

  2. 基于序列化技术(Protobuf)的socket文件传输

    好像好久都没更博文了,没办法,最近各种倒霉事情,搞到最近真的没什么心情,希望之后能够转运吧. 言归正传,这次我要做的是基于序列化技术的socket文件传输来无聊练一下手. 一.socket文件传输 之 ...

  3. PHP中GET和POST区别

    1. get是从服务器上获取数据,post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过H ...

  4. RabbitMq简单应用

    服务端: <?php //配置信息 $conn_args = array( 'host' => '127.0.0.1', 'port' => '5672', 'login' => ...

  5. python 入门基础22 --复习 面向对象

    面向过程编程思想: 核心:过程 过程指的是解决问题的具体步骤,即先干什么再干什么. 基于该编程思想编写程序,相当于一条流水线,一种机械式的思维方式. 面向对象编程思想: 核心:对象 对象指的是数据与方 ...

  6. [转]python ctypes 探究 ---- python 与 c 的交互

    近几天使用 python 与 c/c++ 程序交互,网上有推荐swig但效果都不理想,所以琢磨琢磨了 python 的 ctypes 模块.同时,虽然网上有这方面的内容,但是感觉还是没说清楚.这里记录 ...

  7. 编写灵活、稳定、高质量的 HTML 代码的规范

    不管有多少人共同参与同一项目,一定要确保每一行代码都像是同一个人编写的. 语法 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一致展现的方法. 嵌套元素应当缩进一次(即两个空格 ...

  8. Explain EV in /proc/bus/input/devices data【转】

    转自:https://unix.stackexchange.com/questions/74903/explain-ev-in-proc-bus-input-devices-data It repre ...

  9. 浅谈js设计模式 — 命令模式

    命令模式最常见的应用场景是:有时候需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是什么.此时希望用一种松耦合的方式来设计程序,使得请求发送者和请求接收者能够消除彼此之间的耦 ...

  10. CSS3实现各种表情

    CSS3实现各种表情 效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html> <head> <title></title ...