原始的调用客户端的方式是通过注入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的使用的更多相关文章

  1. spring cloud 使用feign 遇到问题

    spring cloud 使用feign 项目的搭建 在这里就不写了,本文主要讲解在使用过程中遇到的问题以及解决办法 1:示例 @RequestMapping(value = "/gener ...

  2. spring cloud(四) feign

    spring cloud 使用feign进行服务间调用 1. 新建boot工程 pom引入依赖 <dependency> <groupId>org.springframewor ...

  3. spring cloud关于feign client的调用对象列表参数、设置header参数、多环境动态参数试配

    spring cloud关于feign client的调用 1.有些场景接口参数需要传对象列表参数 2.有些场景接口设置设置权限等约定header参数 3.有些场景虽然用的是feign调用,但并不会走 ...

  4. Spring Cloud 整合 Feign 的原理

    前言 在 上篇 介绍了 Feign 的核心实现原理,在文末也提到了会再介绍其和 Spring Cloud 的整合原理,Spring 具有很强的扩展性,会把一些常用的解决方案通过 starter 的方式 ...

  5. Spring Cloud 之 Feign

    新建Spring Boot工程,命名为feign 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?& ...

  6. Spring Cloud中Feign如何统一设置验证token

    代码地址:https://github.com/hbbliyong/springcloud.git 原理是通过每个微服务请求之前都从认证服务获取认证之后的token,然后将token放入到请求头中带过 ...

  7. Spring Cloud 组件 —— feign

    feign 作为一个声明式的 Http Client 开源项目.在微服务领域,相比于传统的 apache httpclient 与在 spring 中较为活跃的 RestTemplate 更面向服务化 ...

  8. spring cloud中feign的使用

    我们在进行微服务项目的开发的时候,经常会遇到一个问题,比如A服务是一个针对用户的服务,里面有用户的增删改查的接口和方法,而现在我有一个针对产品的服务B服务中有一个查找用户的需求,这个时候我们可以在B服 ...

  9. spring cloud 之 Feign 使用HTTP请求远程服务

    一.Feign 简介 在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLCo ...

  10. spring cloud 之 Feign的使用

    1.添加依赖 2.创建FeignClient 原理:Spring Cloud应用在启动时,Feign会扫描标有@FeignClient注解的接口,生成代理,并注册到Spring容器中.生成代理时Fei ...

随机推荐

  1. 【QT】Installer requires Xcode Version 5.0.0 for Qt download if toolchain not found

    When I install QT in MacOS 11.3.6, it prompts I need to install xcode then I install xcode but it wa ...

  2. 【Eclipse】-NO.163.Eclipse.1 -【Eclipse springboot 1.x 创建maven工程初始化报错】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  3. OO第一单元总结(表达式求导)

    写在前边:第一次接触面向对象语言,编程思想仍然不可避免的有以前面向过程的影子.从第一次作业的完全面向过程,到第二次学会剥离各个类互不影响到第三次作业的先构思面向对象的基本程序架构再编程.虽然程序有些地 ...

  4. #WEB安全基础 : HTTP协议 | 0x12 MIME多用途邮件扩展以及多部分对象集合

    我们是怎么让邮件里又有图片又有文字的? 文字和图片是两个不同的类型,而邮件又是一个类型. C语言的结构体允许用户定义一个含有多类型的自定义类型 像这样,看不懂没关系,你只要知道邮件里有多个类型就可以了 ...

  5. 福州大学软件工程1916 | W班 作业成绩排名汇总

    评分链接: 第一次作业-准备篇 https://www.cnblogs.com/deerCode/p/10527237.html 第二次作业-结对第一次-文献摘要热词统计原型设计 https://ww ...

  6. JavaScript字符串字节长度

    var txt2="He中!!";var t = txt2.replace(/[^\u0000-\u00ff]/g,"aa").length;//值是7

  7. Elastic-Job 配置介绍

    作业配置 与Spring容器配合使用作业,可以将作业Bean配置为Spring Bean,可在作业中通过依赖注入使用Spring容器管理的数据源等对象.可用placeholder占位符从属性文件中取值 ...

  8. kbmmemtable sorton 报错 : List index out of bounds

    同一数据集,不同的排序条件,有的可以,但某一条件,却能100%重现报错. procedure TkbmIndex.InternalFastQuickSort(const L,R:Integer); v ...

  9. Linux Postfix 服务

    Linux Postfix 服务 postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)开源软件.能够很好地兼容 Sendmail服务程序,可以方便 Sendm ...

  10. 删除zabbix数据库日志

    #!/bin/bashuser="root"passwd="361way"timedate=`date -d $(date -d "-90 day&q ...