每个方法都配备一个fallback方法

不利于开发的

用类的方式

并且整个方法都是在同一个线程池里面的

主要对于client的修改:

pom:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.toov5</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>toov5-api-order-service-impl</artifactId> <dependencies>
<dependency>
<groupId>com.toov5</groupId>
<artifactId>toov5-api-order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> <dependency>
<groupId>com.toov5</groupId>
<artifactId>toov5-api-member-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> </dependencies> </project>

 实现类

package com.toov5.api.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.toov5.api.entity.UserEntity;
import com.toov5.api.feign.MemberServiceFeign;
import com.toov5.api.service.IOrderService;
import com.toov5.base.BaseApiService;
import com.toov5.base.ResponseBase; @RestController
public class OrderServiceImpl extends BaseApiService implements IOrderService {
@Autowired
private MemberServiceFeign memberServiceFeign; @RequestMapping("/orderToMmeber")
public String orderToMember(String name) {
UserEntity user = memberServiceFeign.getMember(name);
return user == null ? "没有" : user.toString();
} @RequestMapping("/orderToMemberUserInfo")
public ResponseBase orderToMemberUserInfo() {
return memberServiceFeign.getUserInfo();
} @HystrixCommand(fallbackMethod = "orderToMemberUserInfoHystrixFallback")
@RequestMapping("/orderToMemberUserInfoHystrix")
public ResponseBase orderToMemberUserInfoHystrix() {
System.out.println("orderToMemberUserInfoHystrix" + "线程池名称" + Thread.currentThread().getName());
return memberServiceFeign.getUserInfo();
} public ResponseBase orderToMemberUserInfoHystrixFallback() {
return setResultSuccess("请稍后再试~");
} @RequestMapping("/orderToMemberUserInfoHystrixdSecond")
public ResponseBase orderToMemberUserInfoHystrix_demo02() {
System.out.println("orderToMemberUserInfoHystrix" + "线程池名称" + Thread.currentThread().getName());
return memberServiceFeign.getUserInfo();
} @RequestMapping("/orderInfo")
public ResponseBase orderInfo() {
System.out.println("orderInfo" + "线程池名称" + Thread.currentThread().getName());
return setResultSuccess();
} }

feign

package com.toov5.api.feign;

import org.springframework.cloud.openfeign.FeignClient;

import com.toov5.api.fallback.MemberServiceFallback;
import com.toov5.api.service.IMemberService;
@FeignClient(value="app-toov5-member",fallback = MemberServiceFallback.class)
public interface MemberServiceFeign extends IMemberService { }

fallback类

package com.toov5.api.fallback;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping; import com.toov5.api.entity.UserEntity;
import com.toov5.api.feign.MemberServiceFeign;
import com.toov5.base.BaseApiService;
import com.toov5.base.ResponseBase; @Component
@RequestMapping("fallback/") //这个可以防止 容器中有与父类重复的 requestMapping!!!
public class MemberServiceFallback extends BaseApiService implements MemberServiceFeign { @Override
public ResponseBase getUserInfo() {
return setResultError("来自类的提示:系统错误,请稍后重试!");
} @Override
public UserEntity getMember(String name) {
// TODO Auto-generated method stub
return null;
} }

启动类:

package com.toov5.api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication(scanBasePackages={"com.toov5.*"})
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class AppOrder {
public static void main(String[] args) {
SpringApplication.run(AppOrder.class, args);
}
}

 yml

###服务启动端口号
server:
port: 8020
###服务名称(服务注册到eureka名称)
spring:
application:
name: app-toov5-order
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka ###因为该应用为注册中心,不会注册自己
register-with-eureka: true
###是否需要从eureka上获取注册信息
fetch-registry: true ###设置feign客户端超时时间
ribbon:
###指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。
ReadTimeout: 5000
###指的是建立连接后从服务器读取到可用资源所用的时间。
ConnectTimeout: 5000 #spring cloud 默认开启ribbon ##开启Hystrix断路器
feign:
hystrix:
enabled: true #### hystrix禁止服务超时时间
#hystrix:
# command:
# default:
# execution:
# timeout:
# enabled: false

  启动后:

感谢:https://www.jb51.net/article/138758.htm 这篇文章的参考!

对于报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'com.toov5.api.feign.MemberServiceFeign' method
public abstract com.toov5.base.ResponseBase com.toov5.api.service.IMemberService.getUserInfo()
to {[/getUserInfo]}: There is already 'memberServiceFallback' bean metho

的解决办法在 fallback的类上面加上

@RequestMapping("fallback/")   //这个可以防止 容器中有与父类重复的 requestMapping!!!

Spring Cloud之统一fallback接口的更多相关文章

  1. Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,输出异常

    Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,出现请求异常时,会进入熔断处理,但是不会抛出异常信息. 经过以下配置,可以抛出异常: 将原有ErrorEncoder ...

  2. 9.Spring Cloud Config统一管理微服务配置

    Spring Cloud Config统一管理微服务配置 9.1. 为什么要统一管理微服务配置 9.2. Spring Cloud Config简介 Spring Cloud Config为分布式系统 ...

  3. 使用Spring Cloud Config统一管理配置,别再到处放配置文件了

    1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! 可配置是一个成熟软件系统应该提供的特性,而配置管理对于大型系统就显得十分重要,特别是对于拥有多个应用的微服务系统.可喜的是, ...

  4. Spring Cloud开发实践 - 03 - 接口实现和下游调用

    接口实现 Scot Commons Impl 接口实现模块 scot-commons-impl, 一方面实现了 scot-commons-api 的接口, 一方面将自己暴露为 REST 服务. 有4个 ...

  5. .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 =>  Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...

  6. 【SpringCloud构建微服务系列】使用Spring Cloud Config统一管理服务配置

    一.为什么要统一管理微服务配置 对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护 ...

  7. Bug集锦-Spring Cloud Feign调用其它接口报错

    问题描述 Spring Cloud Feign调用其它服务报错,错误提示如下:Failed to instantiate [java.util.List]: Specified class is an ...

  8. Spring Cloud之Swagger2 API接口管理

    随着微服务架构体系的发展和应用, 为了前后端能够更好的集成与对接,同时为了项目的方便交付,每个项目都需要提供相应的API文档. 来源:PC端.微信端.H5端.移动端(安卓和IOS端) 传统的API文档 ...

  9. Spring Cloud:统一异常处理

    在启动应用时会发现在控制台打印的日志中出现了两个路径为 {[/error]} 的访问地址,当系统中发送异常错误时,Spring Boot 会根据请求方式分别跳转到以 JSON 格式或以界面显示的 /e ...

随机推荐

  1. Godaddy域名 绑定ip 服务器

    比如我的域名是wmxl.info 第一个红框代表wmxl.info 绑定的 211.83.110.216 第一个代表www.wmxl.info 绑定的 211.83.110.216, 你也可以换一个服 ...

  2. 简洁的BP及RBF神经网络代码

    BP神经网络 function [W,err]=BPTrain(data,label,hiddenlayers,nodes,type) %Train the bp artial nueral net ...

  3. python函数形参中的*args和**kwargs

    转载:https://www.cnblogs.com/xuyuanyuan123/p/6674645.html 多个实参,放到一个元组里面,以*开头,可以传多个参数:**是形参中按照关键字传值把多余的 ...

  4. ssh不检查server变化

    嵌入式linux开发时经常需要远程登录到板上,但由于开发过程还经常会重新下载内核和文件系统,导致登录时总提示host变了,blablabla,解决方案是在.ssh/config对应的Host项下面加上 ...

  5. centos6.5安装Apache+MySQL+PHP

    一.安装 MySQL 首先来进行 MySQL 的安装.打开超级终端,输入: [root@localhost ~]# yum install mysql mysql-server 安装完毕,让 MySQ ...

  6. Unity3D引擎之渲染技术系列一

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者.国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...

  7. dedecms增加自定义表单管理员

    打开\dede\inc\grouplist.txt 添加 >>自定义表单 >f_List>列出表单 >f_New>新建表单 >f_Edit>编辑表单 & ...

  8. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps Office新的App模型

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps  Office新的App模型         Office 2 ...

  9. apple-touch-icon-precomposed 和 apple-touch-icon属性区别

    苹果safari浏览器当中apple-touch-icon-precomposed 和 apple-touch-icon属性是有区别的,之前在网上查了下相关的资料和苹果的开发文档手册,对这两中属性区别 ...

  10. KPI、KPA、OKR三者的区别

    KPI.KPA或者OKR并不是水火不相容有你无我的概念,针对不对的业务状态.管理模式应该有所选择.以下是介绍它们之间的区别. 什么是KPI关键绩效指标 KPI(key performance indi ...