Spring Cloud之统一fallback接口
每个方法都配备一个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接口的更多相关文章
- Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,输出异常
Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,出现请求异常时,会进入熔断处理,但是不会抛出异常信息. 经过以下配置,可以抛出异常: 将原有ErrorEncoder ...
- 9.Spring Cloud Config统一管理微服务配置
Spring Cloud Config统一管理微服务配置 9.1. 为什么要统一管理微服务配置 9.2. Spring Cloud Config简介 Spring Cloud Config为分布式系统 ...
- 使用Spring Cloud Config统一管理配置,别再到处放配置文件了
1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! 可配置是一个成熟软件系统应该提供的特性,而配置管理对于大型系统就显得十分重要,特别是对于拥有多个应用的微服务系统.可喜的是, ...
- Spring Cloud开发实践 - 03 - 接口实现和下游调用
接口实现 Scot Commons Impl 接口实现模块 scot-commons-impl, 一方面实现了 scot-commons-api 的接口, 一方面将自己暴露为 REST 服务. 有4个 ...
- .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
- 【SpringCloud构建微服务系列】使用Spring Cloud Config统一管理服务配置
一.为什么要统一管理微服务配置 对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护 ...
- Bug集锦-Spring Cloud Feign调用其它接口报错
问题描述 Spring Cloud Feign调用其它服务报错,错误提示如下:Failed to instantiate [java.util.List]: Specified class is an ...
- Spring Cloud之Swagger2 API接口管理
随着微服务架构体系的发展和应用, 为了前后端能够更好的集成与对接,同时为了项目的方便交付,每个项目都需要提供相应的API文档. 来源:PC端.微信端.H5端.移动端(安卓和IOS端) 传统的API文档 ...
- Spring Cloud:统一异常处理
在启动应用时会发现在控制台打印的日志中出现了两个路径为 {[/error]} 的访问地址,当系统中发送异常错误时,Spring Boot 会根据请求方式分别跳转到以 JSON 格式或以界面显示的 /e ...
随机推荐
- laravel处理ajax的post提交
Html页面(laravel表单提交必须token)所以 头部要加入: <meta name="csrf-token" content="{{ csrf_token ...
- Java中的equals方法和自定义比较器
Object中的equals()方法默认是按地址比较,而不按内容进行比较, public boolean equals(Object obj) { return (this == obj); } 在S ...
- Maven的配置地址
http://howtodoinjava.com/tools/eclipse/how-to-import-maven-remote-archetype-catalogs-in-eclipse/ htt ...
- NHibernate 组件基础 (第六篇)
NHibernate 组件基础 (第六篇) 一.组件简介 组件(Component)可以理解为被一个对象所包含的对象而持久化,而并非一个实体.简单说来,假如数据库有FirstName,LastName ...
- Oracle的substr函数简单用法与substring区别
substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串 subst ...
- CentOS7 安装 Node.js
1.首先安装node.js 的版本管理工具 NVM,执行以下命令: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/ ...
- JWT简介json web token bear token
, "exp": 1502360328,(相差3600s) "nbf": , "jti": "R0Gd00AvOW259LGo&q ...
- Android动画详解
一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XML配置文件中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画 ...
- FineUIPro中如何支持多语言(全局资源文件和本地资源文件)
一个客户在邮件中问到了FineUIPro的多语言实现问题,其实 FineUIPro 并没有对此做特殊处理,因此直接使用 ASP.NET 原生支持的资源文件就能实现. 下面我们就以FineUIPro的空 ...
- 数据处理 数据入数据库 与 Excel
Python 数据处理 中间数据 Excel 团队交流分工 低的沟通成本 数据入数据库 如postgresql