在项目的启动文件加入:@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); }
}

  

Feign二: @FeignClient 接口调用的更多相关文章

  1. Spring Cloud07: Feign 声明式接口调用

    一.什么是Feign Feign也是去实现负载均衡,但是它的使用要比Ribbon更加简化,它实际上是基于Ribbon进行了封装,让我们可以通过调用接口的方式实现负载均衡.Feign和Ribbon都是由 ...

  2. springcloud费话之Eureka接口调用(feign)

    目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...

  3. (二)通过JAVA调用SAP接口 (增加一二级参数)

    (二)通过JAVA调用SAP接口 (增加一二级参数) 一.建立sap连接 请参考我的上一篇博客 JAVA连接SAP 二.测试项目环境准备 在上一篇操作下已经建好的环境后,在上面的基础上新增类即可 三. ...

  4. Feign二:复写Feign的默认配置

    Feign二:复写Feign的默认配置 1.在启动文件加入feign注解:@EnableFeignClients FeignApp.java import org.springframework.bo ...

  5. Spring Cloud Feign 声明式服务调用

    目录 一.Feign是什么? 二.Feign的快速搭建 三.Feign的几种姿态 参数绑定 继承特性 四.其他配置 Ribbon 配置 Hystrix 配置 一.Feign是什么? ​ 通过对前面Sp ...

  6. spring cloud 系列第4篇 —— feign 声明式服务调用 (F版本)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.feign 简介 在上一个用例中,我们使用ribbon+restTem ...

  7. Spring Cloud Feign声明式服务调用(转载)+遇到的问题

    转载:原文 总结: 1.pom添加依赖 2.application中填写正确的eureka配置 3.启动项中增加注解 @EnableFeignClients 4.填写正确的调用接口 通过原文使用Fei ...

  8. Feign声明式服务调用

    Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. Spring Clo ...

  9. SpringCloud实战-Feign声明式服务调用

    在前面的文章中可以发现当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率 ...

随机推荐

  1. 使用SSH框架遇到的错误总结

    1.org.hibernate.exception.ConstraintViolationException: could not insert: 如果是主键是自增序列,映射文件 指定主键生成器< ...

  2. makefile如果没有符合的显式规则将会使用隐式规则

    举例: 当前目录下有个Makefile和jello.c文件,其中有这样的规则jello.o:%.c %.h Makefile (静态模式规则),表明的含义为:要生成的jello.o目标依赖jello. ...

  3. MySQL删除数据库时无响应解决办法

    删除远程主机上MySQL中的一个数据库时,远程主机一直在响应,无法正常删除. 这个问题的解决办法如下:在远程主机上登录MySQL,执行show full processlist;观察state和inf ...

  4. Java程序运行机制及开发环境

    Java既是编译型语言,又是解释型语言 java源文件首先需要通过javac编译生成后缀名为.class的字节码文件(与平台无关,只面向JVM),然后使用Java虚拟机将字节码解释成特定平台上的机器码 ...

  5. P2475 [SCOI2008]斜堆(递归模拟)

    思路 可并堆真是一种神奇的东西 不得不说这道题是道好题,虽然并不需要可并堆,但是能加深对可并堆的理解 首先考虑斜堆的性质,斜堆和左偏树相似,有如下的性质 一个节点如果有右子树,就一定有左子树 最后插入 ...

  6. 论文笔记:Attention Is All You Need

    Attention Is All You Need 2018-04-17 10:35:25  Paper:http://papers.nips.cc/paper/7181-attention-is-a ...

  7. springmvc异步上传图片并回调页面函数插入图片url代码示例

    <tr> <td class="search_td">属性值图片值:</td> <td> <input type=" ...

  8. 程序修改图标后显示未更新——强制刷新windows图标缓存

    http://blog.csdn.net/vvlowkey/article/details/51133486 20160412 问题:修改兴迪局放测量软件图标后,release文件夹中生成文件的小图标 ...

  9. git pull 提示 There is no tracking information for the current branch

    在执行git pull的时候,提示当前branch没有跟踪信息: git pull There is no tracking information for the current branch. P ...

  10. 用Github做一个静态网页(GithubPages)

    一.新建一个仓库(new). 二.命名Repository name为:(名字).github.io(一定要有.github.io). 三.勾选Initialize this repository w ...