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 ...
随机推荐
- Redis Sentinel实现的机制与原理详解
序言 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案.实际上这意味着你可以使用Sentinel模式创建一个可以不用人为干预而应对各种故障的Redis部署. 它的主要功能有以 ...
- 一小时上手Java 8新特性
一小时上手Java 8新特性 本文摘译自 https://www.journaldev.com/2389/java-8-features-with-examples,并做了适当增补. Iterable ...
- 2018-2019-2 网络对抗技术 20165321 Exp5 MSF基础应用
1. 实践内容(3.5分) 1.1一个主动攻击实践: (1分) 首先攻击Windows XP: 在攻击机kali输入msfconsole进入控制台,依次输入以下指令: msf > use exp ...
- cacheline基本理论
一.cacheline 1.cache:解决cpu频率与内存访问之间速度差距越来越大的问题 2.cacheline:cpu cache的最小单位,主流为64B 3.指导:访问数组数据在同一个cache ...
- 控制反转IOC
IOC-Inversion of Control 控制反转,这是spring的核心.对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系. 1:控制反转不是一种技术,而是 ...
- 对int类型的数据,如何让获取长度
下面为大家写一个列子 int a = 124;<br> Integer a1 = a;//转换为包装类Integer<br> System.out.println(a1.t ...
- 安装mysql时出现应用程序无法正常启动(0xc000007b)
在重装mysql数据库(解压版)遇到了无法正常启动(0xc000007b)问题解决方案 问题描述: 在cmd控制台,mysql的安装路径下使用mysql install命令出现以下无法正常启动(0xc ...
- C# Unity依赖注入
简介: 控制反转:我们向IOC容器发出获取一个对象实例的一个请求,IOC容器便把这个对象实例“注入”到我们的手中,在这个过程中你不是一个控制者而是一个请求者,依赖于容器提供给你的资源,控制权落到了容器 ...
- intellij idea 官方帮助文档翻译(一)安装
安装需求: 硬件需求: 最低2GB内存,推荐4GB内存 最少要有1.5GB的硬盘空间,以及1GB空间用于缓存 最低1024*768的分辨率 软件需求: intellij idea中包含了jre 1.8 ...
- PAT (Advanced Level) Practice 1001 A+B Format (20 分)
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a+b and ...