1. 什么是Feign?

  Feign是一种声明式、模板化的HTTP客户端,在SpringCloud中使用Feign。可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到是一个远程方法,更感知不到这是一个HTTP请求

  Feign的灵感来自于Retrofit,JAXRS-2.0和WebSocket,它使得Java HTTP客户端编写更方便,旨在通过最少的资源和代码来实现和HTTP API的连接。

2. 引入依赖

  在需要调用HTTP api的工程加入下面依赖

 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

3. 代码实现

  1. 在启动类上添加@EnableFeignClients注解开启Feign。

  2. 使用接口实现

package com.wangx.cloud.biz;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; @FeignClient("spring-cloud-provider")
public interface IUserBiz { @RequestMapping(value = "/api/user/{id}")
String getUser(@PathVariable(value = "id") Integer id);
}

  3. Controller中直接像调用本地方法一样调用

  

package com.wangx.cloud.controller;

import com.wangx.cloud.biz.IUserBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping(value = "/user", method = RequestMethod.POST)
public class UserController { @Autowired
private IUserBiz iUserBiz; @RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String get(@PathVariable(value = "id") int id) {
return iUserBiz.getUser(id);
}
}

  本示例是在消费者中调用消费者的服务,将会轮询的去调用消费者服务接口。

  注意:负载均衡的策略与Ribbon中的配置方式一样,因为Feign依赖Ribbon,使用的是Ribbon的负载均衡。

3. 抽取接口

  在实际的开发过程中,为了方便管理,通常是将所有的接口和bean都抽取到一个工程中,在消费者中使用的使用,只需要依赖维护接口的jar即可。跟直接在消费者中使用方式和原理一样,只是将接口和传输的实体类单独维护起来,使得开发更简单,明了。在引入的工程中需要让Spring扫描到@FeignClient注解,只需要@EnableFeignClients(basePackages = "包名")即可。

4. 参数说明

  

SpringCloud学习笔记(9)----Spring Cloud Netflix之声明式 REST客户端 -Feign的使用的更多相关文章

  1. SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性

    1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...

  2. 3.【Spring Cloud Alibaba】声明式HTTP客户端-Feign

    使用Feign实现远程HTTP调用 什么是Feign Feign是Netflix开源的声明式HTTP客户端 GitHub地址:https://github.com/openfeign/feign 实现 ...

  3. Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务

    首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...

  4. spring cloud 声明式rest客户端feign调用远程http服务

    在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.Feign就是Spring Cloud提供的一种声明式R ...

  5. springCloud学习-消息总线(Spring Cloud Bus)

    1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...

  6. Spring Cloud(三):声明式调用

    声明式服务调用 前面在使用spring cloud时,通常都会利用它对RestTemplate的请求拦截来实现对依赖服务的接口调用,RestTemplate实现了对http的请求封装处理,形成了一套模 ...

  7. Spring Cloud探路(三)REST 客户端Feign

    Declarative REST Client: Feign Feign is a declarative web service client. It makes writing web servi ...

  8. SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据

    简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...

  9. Spring Cloud Netflix项目进入维护模式

    任何项目都有其生命周期,Spring Could Netflix也不例外,官宣已进入维护模式,如果在新项目开始考虑技术选型时要考虑到这点风险,并考虑绕道的可能性. 原创: itmuch  IT牧场 这 ...

随机推荐

  1. Android 自定义简单控件--星级评价

    效果图 实现 package com.easypass.carstong.view; import android.content.Context; import android.content.re ...

  2. [Reading] Asking while Reading

    Asking while Reading ——读Java垃圾收集器与内存分配策略 Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的“高墙”,墙外面的人想进去,墙里面的人却想出来. 为什么 ...

  3. Android ListView getView()方法重复调用导致position错位

    问题现状:Android ListView getView()方法重复调用导致position错位 解决办法:把ListView布局文件的layout_height属性改为fill_parent或者m ...

  4. 问题请教:关于同一个POD中多容器的广播信息问题

    广大博友好,最近在K8S集群中遇到一个问题,贴出来同大家分享一下 同一个POD中多个容器 如何处理广播信息? 经测试 同一个POD中当先启动的容器占用广播端口后,其他的容器启动就会报bind erro ...

  5. EM_LGH CF965D Single-use Stones 思维_推理

    Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 1000000 ...

  6. 模板 NTT 快速数论变换

    NTT裸模板,没什么好解释的 这种高深算法其实也没那么必要知道原理 #include <cstdio> #include <cstring> #include <algo ...

  7. Android开发进度02

    1,今日:目标:创建第一个android项目,创建android虚拟机 2,昨天:完成eclipseandroid环境的搭建 3,收获:修改.xml文件,将出错地方解决 4,问题:版本问题

  8. SpringBoot下支付宝接口的使用

    SpringBoot下支付宝接口的使用 前期准备: 参考之前写过的 支付宝接口引入servlet版本 Jar包引入: <!-- 支付宝 --> <dependency> < ...

  9. Android Touch事件传递机制全面解析(从WMS到View树)

    转眼间近一年没更新博客了,工作一忙起来.非常难有时间来写博客了,因为如今也在从事Android开发相关的工作,因此以后的博文也会很多其它地专注于这一块. 这篇文章准备从源代码层面为大家带来Touch事 ...

  10. Spring boot 使用@Value注入属性

    Spring boot 使用@Value注入属性 学习了:http://blog.csdn.net/hry2015/article/details/72353994 如果启动的时候报错: spring ...