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 ...
随机推荐
- Android 完整开源应用大全,完整开源项目
(Antox)聊天的 (new) (OpenKeychain)OpenPGP在android上的实现 (new) (Flock)提供同步服务 (OpenFlappyBird)以前火爆的坑爹鸟 (F ...
- RTM-DSP项目总结
1. 项目介绍 在NINJA设备上支持RTM-ISDN卡 RTM-ISDN卡硬件组成 主要组成单元 C6415: DSP PEB383(上图中的PEX8112改为PEB383,由于后者具有NT功能) ...
- linux命令的别名alias,unalias
1. 别名 linux别名alias的作用: 1. 简化特别长得命令和參数 2. 对一些命令添加默认选项.提高安全性. 2. alias使用 [www@work sh]$ alias lm='ls - ...
- Python内置函数之range()
range(stop)range(start,stop[,step]) 返回一个range对象,第三个参数的含义为:间隔的个数. range对象同时也是可迭代对象. >>> isin ...
- nginx反向代理带路径访问问题
nginx的配置为192.168.0.219:80分别映射到upstream组192.168.0.55:8080和192.168.0.206:8080,那如何配置做到访问192.168.0.219:8 ...
- struts2中配置文件加载的顺序是什么?
struts2的StrutsPrepareAndExecuteFilter拦截器中对Dispatcher进行了初始化 在Dispatcher类的init方法中定义了配置文件的加载顺序(下面是源码) p ...
- 【转】【Python + selenium】linux和mac环境,驱动器chromedriver和测试报告HTMLTestRunner放置的位置
感谢: 作者:gz_tester,文章:<linux和mac环境,chromedriver和HTMLTestRunner放置的位置> 使用场景 配置python selenium 环境 使 ...
- linux 上拷贝文件到windows 上 文件出现锁的文件
要在linux上拷贝出文件到windows上,那么文件必须是777的最高权限. chmod wb_redis -R
- Android Studio中常用设置
参考资料: 1.http://blog.csdn.net/itdada/article/details/43375893 常用设置: 1.Tab不用4个空格Code Style->Java-&g ...
- saltstack之软件管理
1.installed安装软件包 例: 安装NFS /srv/salt/pkg/nfs.sls nfs: pkg.installed: - pkgs: - nfs-utils 在命令行执行如下 sal ...