spring cloud之Feign的使用
原始的调用客户端的方式是通过注入restTemplate的方式
restTemplate.getForObject("http://CLIENT/hello", String.class)
通过feign的方式
配置消费者项目cloud-consume
pom.xml
依赖jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
application.yml
添加启动feign 可实现错误回调
feign:
hystrix:
enabled: true
启动应用类
ClondConsumeApplication.java
添加注解@EnableFeignClients
HelloService.java接口
package com.tp.soft.cloudconsume.service; import com.tp.soft.cloudconsume.service.impl.HelloServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; @FeignClient(value = "CLIENT", fallback = HelloServiceFallback.class)
public interface HelloService {
@GetMapping("/hello")
String hello();
}
其中CLIENT则为注册中心被调用的应用名,/hello完全和客户端业务接口一样,fallback则为错误回调的方法,同时可以防止应用雪崩效应.
HelloServiceFallback.java接口
package com.tp.soft.cloudconsume.service.impl; import com.tp.soft.cloudconsume.service.HelloService;
import org.springframework.stereotype.Component; @Component
public class HelloServiceFallback implements HelloService {
@Override
public String hello() { return "request error";
}
}
HelloController.java
package com.tp.soft.cloudconsume.controller; import com.tp.soft.cloudconsume.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController
public class HelloController { // @Autowired
// RestTemplate restTemplate; @Resource
private HelloService helloService; @GetMapping("hi")
public String hi(){
//return restTemplate.getForObject("http://CLIENT/hello", String.class);
return helloService.hello();
}
}
和原来在controller调用接口一模一样的去调用就可以了
最后的效果:

spring cloud之Feign的使用的更多相关文章
- spring cloud 使用feign 遇到问题
spring cloud 使用feign 项目的搭建 在这里就不写了,本文主要讲解在使用过程中遇到的问题以及解决办法 1:示例 @RequestMapping(value = "/gener ...
- spring cloud(四) feign
spring cloud 使用feign进行服务间调用 1. 新建boot工程 pom引入依赖 <dependency> <groupId>org.springframewor ...
- spring cloud关于feign client的调用对象列表参数、设置header参数、多环境动态参数试配
spring cloud关于feign client的调用 1.有些场景接口参数需要传对象列表参数 2.有些场景接口设置设置权限等约定header参数 3.有些场景虽然用的是feign调用,但并不会走 ...
- Spring Cloud 整合 Feign 的原理
前言 在 上篇 介绍了 Feign 的核心实现原理,在文末也提到了会再介绍其和 Spring Cloud 的整合原理,Spring 具有很强的扩展性,会把一些常用的解决方案通过 starter 的方式 ...
- Spring Cloud 之 Feign
新建Spring Boot工程,命名为feign 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?& ...
- Spring Cloud中Feign如何统一设置验证token
代码地址:https://github.com/hbbliyong/springcloud.git 原理是通过每个微服务请求之前都从认证服务获取认证之后的token,然后将token放入到请求头中带过 ...
- Spring Cloud 组件 —— feign
feign 作为一个声明式的 Http Client 开源项目.在微服务领域,相比于传统的 apache httpclient 与在 spring 中较为活跃的 RestTemplate 更面向服务化 ...
- spring cloud中feign的使用
我们在进行微服务项目的开发的时候,经常会遇到一个问题,比如A服务是一个针对用户的服务,里面有用户的增删改查的接口和方法,而现在我有一个针对产品的服务B服务中有一个查找用户的需求,这个时候我们可以在B服 ...
- spring cloud 之 Feign 使用HTTP请求远程服务
一.Feign 简介 在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLCo ...
- spring cloud 之 Feign的使用
1.添加依赖 2.创建FeignClient 原理:Spring Cloud应用在启动时,Feign会扫描标有@FeignClient注解的接口,生成代理,并注册到Spring容器中.生成代理时Fei ...
随机推荐
- tar打包
进入到目录里,执行下面的命令,可以把目录内的所有内容打包 tar -cvf 111.tar *
- IDEA使用技巧
1,导入原Eclipse Web项目 由于使用 PowerDesign连接MySql时只能用32位 Jdk,原Eclipse项目依赖于64位Jdk,导致在eclipse打不开工程,把工程导入IDEA后 ...
- Adaboost总结
一.简介 Boosting 是一类算法的总称,这类算法的特点是通过训练若干弱分类器,然后将弱分类器组合成强分类器进行分类.为什么要这样做呢?因为弱分类器训练起来很容易,将弱分类器集成起来,往往可以得到 ...
- OpenGL入门之入门
programs on the GPU-------shader 顶点着色器-->形状(图元)装配-->几何着色器-->光栅化-->片段着色器-->测试与混合 图形渲染管 ...
- web自动化测试python+selenium学习总结----python编辑器pycharm环境安装
下载安装文件 下载最新文件路径:https://www.jetbrains.com/pycharm/ 安装: 一直点击下一步即可 破解: 配置hosts文件.C:\Windows\System32\d ...
- 【转】Oracle EBS中查询Profile的各种SQL
参考 http://blog.csdn.net/pan_tian/article/details/7652968#t0 Using API FND_PROFILE.save to update pro ...
- poj1456
题目大意:给定一些物品以及他们的价值和时间w.t,表示物品在时间t内售出能得到w 的价值,一天只能出售一件物品,求最大利润: 非常有意思的一道并查集的思想题 首先以价值为关键字排序,类似于贪心的思想, ...
- 关于MySQL中的自联结的通俗理解
关于MySQL中的自联结的通俗理解 前言:最近在通过SQL必知必会这本书学习MySQL的基本使用,在学习中也或多或少遇到了点问题,我也正好分享给大家,我的这篇博客用到的所有表格的代码都是来自SQL必知 ...
- Javascript基础语法(三)
数组 1.什么是数组 有许多变量,它们的名称和数据类型都是一致的. 2. 数组声明 var arr = [元素,元素,元素...]; var arr = new Array(元素,元素,元素...); ...
- 帮助Bsny(乱搞做法)
帮助Bsny 题目描述 Bsny的书架乱成一团了,帮他一下吧! 他的书架上一共有n本书,我们定义混乱值是连续相同高度书本的段数.例如,如果书的高度是30,30,31,31,32,那么混乱值为3:30, ...