为了深入理解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); }
}

最新文章

  1. [解决方案]CREATE DATABASE statement not allowed within multi-statement transaction.
  2. 在CHROME里安装 VIMIUM 插件, 方便操作
  3. ANdroid Studio查看debug SHA1
  4. Fragment:关于Avoid non-default constructors in fragments的错误
  5. Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)
  6. Mybatis中 sequence不能自增长
  7. CF# Educational Codeforces Round 3 F. Frogs and mosquitoes
  8. [2015hdu多校联赛补题]hdu5348 MZL's endless loop
  9. C++矢量图形库系列(1)——矢量图形库乱谈(转)
  10. ubuntu下c/c++开发环境配置
  11. android中文件操作的四种枚举
  12. 如何把rtf、doc文件转换为HTML文件
  13. spring framework 4 源码阅读
  14. Android热补丁动态修复
  15. Android Studio参考在线文章
  16. springboot整合mybatis使用阿里(阿里连接池)和xml方式
  17. SpringMVC源码情操陶冶-InterceptorsBeanDefinitionParser拦截器解析器
  18. matlab2017b
  19. 莫烦scikit-learn学习自修第二天【算法地图】
  20. 20155208徐子涵 2016-2017-2 《Java程序设计》第9周学习总结

热门文章

  1. Jmeter下载安装(一)
  2. mysql8版本以上重置密码
  3. 在 Node.js 中处理大 JSON 文件
  4. Oracle Error while trying to retrieve text for error ORA-01804
  5. 2017final英文语句格式简单检查
  6. 大爽Python入门教程 总目录
  7. Django 小实例S1 简易学生选课管理系统 9 创建课程模型(model)
  8. psutil模块详解
  9. python实现调用摄像头或打开视频文件
  10. [nowcoder5668I]Sorting the Array