Spring Cloud声明式调用Feign负载均衡FeignClient详解
为了深入理解Feign,下面将从源码的角度来讲解Feign。首先来看看FeignClient注解@FeignClient的源码,代码如下:
FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient的作用目标在接口上。@Retention(RetentionPolicy.RUNTIME)注解表明该注解会在Class字节码中存在,在运行时可以通过反射获取到。@Documented表明该注解被包含在javadoc中。
@FeignClient用于创建声明是API接口,该接口是RESTful风格的。Feign被设计成插拔式的,可注入其他组件和Feign一起使用。最典型的是如果Ribbon可用,Feign会和Ribbon相结合进行负载均衡。
在代码中,value()和name()一样,是被调用的服务的ServiceId。url()直接填写硬编码URL地址。decode404()即404是被解码,还是抛异常。configuration()指明FeignClient的配置类,默认的配置类为FeignClientsConfiguration类,在缺省情况下,这个类注入了默认的Decoder、Encoder和Constant等配置的bean。fallback()为配置熔断器的处理类。
@FeignClient 接口调用
在项目的启动文件加入:@EnableFeignClients 注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class FeignApp { public static void main(String[] args) {
SpringApplication.run(FeignApp.class, args);
}
}
实例结构如下:

那么有实体类: User.java
Fengn客户端:UserFeignClient.java
控制器: MovieController.java调取第三方user接口
User.java

import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private int age;
private BigDecimal balance;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
UserFeign客户端
其中:@FeignClient("spring-boot-user"): spring-boot-user是eureka服务里面user项目的名称,加入此注解,能直接连接user项目接口
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.muyang.bootmovie.entity.User; @FeignClient("spring-boot-user")
public interface UserFeignClient { // 两个坑:1. @GetMapping不支持 2. @PathVariable得设置value
@RequestMapping(value="/simple/{id}", method=RequestMethod.GET)
public User findById(@PathVariable("id") Long id); @RequestMapping(value="/test", method=RequestMethod.POST)
public User postUser(@RequestBody User user);
}
MovieController控制中心,调取UserFeign客户端
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.muyang.bootmovie.entity.User;
import com.muyang.bootmovie.feign.UserFeignClient; @RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @GetMapping("/movie/{id}")
public User findById(@PathVariable("id") Long id) {
return this.userFeignClient.findById(id);
} @RequestMapping(value="/test", method=RequestMethod.GET)
public User userPost(User user)
{
return this.userFeignClient.postUser(user); }
}
Spring Cloud声明式调用Feign负载均衡FeignClient详解的更多相关文章
- Spring Cloud:使用Ribbon实现负载均衡详解(下)
在上一篇文章(Spring Cloud:使用Ribbon实现负载均衡详解(上))中,我对 Ribbon 做了一个介绍,Ribbon 可以实现直接通过服务名称对服务进行访问.这一篇文章我详细分析一下如何 ...
- Nginx 负载均衡原理简介与负载均衡配置详解
Nginx负载均衡原理简介与负载均衡配置详解 by:授客 QQ:1033553122 测试环境 nginx-1.10.0 负载均衡原理 客户端向反向代理发送请求,接着反向代理根据某种负载机制 ...
- Nginx + Tomcat 负载均衡配置详解
Nginx常用操作指南一.Nginx 与 Tomcat 安装.配置及优化1. 检查和安装依赖项 yum -y install gcc pcre pcre-devel zlib zlib-devel o ...
- [转帖]Nginx服务器的六种负载均衡策略详解
Nginx服务器的六种负载均衡策略详解 咔咔侃技术 2019-09-11 17:40:12 一.关于Nginx的负载均衡 在服务器集群中,Nginx起到一个代理服务器的角色(即反向代理),为了避免单独 ...
- Spring Cloud 声明式服务调用 Feign
一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...
- spring cloud 声明式rest客户端feign调用远程http服务
在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.Feign就是Spring Cloud提供的一种声明式R ...
- Spring 声明式事务与编程式事务详解
本文转载自IBM开发者论坛:https://developer.ibm.com/zh/articles/os-cn-spring-trans 根据自己的学习理解有所调整,用于学习备查. 事务管理对于企 ...
- spring boot 2.0.3+spring cloud (Finchley)3、声明式调用Feign
Feign受Retrofix.JAXRS-2.0和WebSocket影响,采用了声明式API接口的风格,将Java Http客户端绑定到他的内部.Feign的首要目标是将Java Http客户端调用过 ...
- spring cloud深入学习(四)-----eureka源码解析、ribbon解析、声明式调用feign
基本概念 1.Registe 一一服务注册当eureka Client向Eureka Server注册时,Eureka Client提供自身的元数据,比如IP地址.端口.运行状况指标的Uri.主页地址 ...
随机推荐
- Piakchu之RCE漏洞
一.Ping(远程系统命令执行) 首先正常输入一个ip,查看页面的返回值.发现有乱码,但是能看出执行了ping命令. 查看源代码,可以看到只是对操作系统进行了判断,而对输入内容是否为ip地址并没有判断 ...
- index 首页
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- KMP算法-字符匹配
字符匹配模式-KMP算法 j直接跳到了2的位置,因为在之前的都相同. 那么就需要求如果不等了之后,j需要回跳的位置next[j] 如果tk'与tj相等,则next [j+1]=k'+1 如果tk'与t ...
- 『学了就忘』Linux基础命令 — 30、find命令详细说明
目录 1.find命令的基本信息 2.find命令基本使用 3.按照文件大小搜索 4.按照修改时间搜索 5.按照权限搜索 6.按照所有者和所属组搜索 7.按照文件类型搜索 8.逻辑运算符 (1)-a: ...
- Rancher 下图形界面 搭建 K8S 集群
首先我们准备4台 2核3G 的 centos 7 温馨提示:先安装好一台 CentOS 的虚拟机,并且安装好 docker,永久关闭防火墙. 再这个基础上我们分别克隆出四台 Rancher.K8S1. ...
- HttpClient用法--这一篇全了解(内含例子)
HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性,它不仅使客户端发送Http请求变得容易,而且也方便开发人员测试接口(基于Http协议的),提高了开发的效率,也 ...
- Emmet快速语法—助力HTML/CSS一行代码一个页面
学会之后牛掰的场景如下 我们的目标就是用一行代码=>写下面这样的长长长长的HTML结构来. 如:table>(thead.text>th{手机1}*4)+(tbody.text$*4 ...
- [第三章]c++学习笔记1(this指针)
this指针作用,其作用就是指向成员函数所作用的对象 使用例 为了返回c1,使用this指针,来指向作用的对象 使用空指针调用hello,调用hello欲使其作用在p指向的对象上,然而p没指向任何对象 ...
- MySQL基础语句(MySQL内置函数 )
MySQL 字符串函数 函数 描述 实例 ASCII(s) 返回字符串 s 的第一个字符的 ASCII 码. 返回 CustomerName 字段第一个字母的 ASCII 码: SELECT ASCI ...
- SpringCloud升级之路2020.0.x版-35. 验证线程隔离正确性
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 上一节我们通过单元测试验证了重试的正确性,这一节我们来验证我们线程隔离的正确性,主要包括: ...