springcloud9----feign-client-without-hystrix
package com.itmuch.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerMovieFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerMovieFeignApplication.class, args);
}
}
package com.itmuch.cloud.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.itmuch.cloud.entity.User;
import com.itmuch.cloud.feign.FeignClient2;
import com.itmuch.cloud.feign.UserFeignClient; @RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; //禁用FeignClient2的Hystrix,就要在Configuration2里面配置。
//Feign对Hystrix的禁用可以到某一个Client,如果在yml文件配置feign.hystrix.enabled=flase则所有的feign client都禁用了。
@Autowired
private FeignClient2 feignClient2; @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
} @GetMapping("/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {
return this.feignClient2.findServiceInfoFromEurekaByServiceName(serviceName);
}
}
package com.itmuch.cloud.feign; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import com.itmuch.config.Configuration2; //禁用FeignClient2的Hystrix,就要在Configuration2里面配置。
@FeignClient(name = "xxxx", url = "http://localhost:8761/", configuration = Configuration2.class, fallback = FeignClient2Fallback.class)
public interface FeignClient2 {
@RequestMapping(value = "/eureka/apps/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}
package com.itmuch.cloud.feign; public class FeignClient2Fallback implements FeignClient2 { @Override
public String findServiceInfoFromEurekaByServiceName(String serviceName) {
return "haha";
} }
package com.itmuch.cloud.feign; import org.springframework.stereotype.Component; import com.itmuch.cloud.entity.User; @Component
public class HystrixClientFallback implements UserFeignClient { @Override
public User findById(Long id) {
User user = new User();
user.setId(0L);
return user;
}
}
package com.itmuch.cloud.feign; import org.springframework.cloud.netflix.feign.FeignClient; import com.itmuch.cloud.entity.User;
import com.itmuch.config.Configuration1; import feign.Param;
import feign.RequestLine; @FeignClient(name = "microservice-provider-user", configuration = Configuration1.class, fallback = HystrixClientFallback.class)
public interface UserFeignClient {
@RequestLine("GET /simple/{id}")
public User findById(@Param("id") Long id);
}
package com.itmuch.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import feign.Contract;
import feign.Logger; @Configuration
public class Configuration1 {
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
} @Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
package com.itmuch.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor; @Configuration
public class Configuration2 {
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "password123");
} //Configuration2里面加上这个就禁用了FeignClient2的Hystrix
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
//feignBuilder方法默认返回HystrixFeign.Builder也就是说Feign默认支持Hystrix
//现在改成Feign.Builder就禁用了Hystrix的支持
return Feign.builder();
}
}
spring:
application:
name: microservice-consumer-movie-feign-customizing-without-hystrix
server:
port: 7901
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
logging:
level:
com.itmuch.cloud.feign.UserFeignClient: DEBUG # 解决第一次请求报超时异常的方案:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
# 或者:
# hystrix.command.default.execution.timeout.enabled: false
# 或者: # 超时的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
# 超时的解决方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
# hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
<?xml version="1.0" encoding="UTF-8"?>
<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> <artifactId>microservice-consumer-movie-feign-customizing-without-hystrix</artifactId>
<packaging>jar</packaging> <parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
</project>
springcloud9----feign-client-without-hystrix的更多相关文章
- No fallback instance of type class found for feign client user-service(转)
1.错误日志 在 feign 开启熔断,配置 fallback 类,实现当前接口的实现类时,报错信息如下: Error starting ApplicationContext. To display ...
- Feign Client 原理和使用
Feign Client 原理和使用 一块石头 公众号:好奇心森林 关注他 创作声明:内容包含虚构创作 6 人赞同了该文章 最近一个新项目在做后端HTTP库技术选型的时候对比了Spring We ...
- SpringCloud 在Feign上使用Hystrix(断路由)
SpringCloud 在Feign上使用Hystrix(断路由) 第一步:由于Feign的起步依赖中已经引入了Hystrix的依赖,所以只需要开启Hystrix的功能,在properties文件中 ...
- spring cloud feign覆写默认配置级feign client的日志打印
一.覆写fegin的默认配置 1.新增配置类FeignConfiguration.java package com.config; import org.springframework.context ...
- feign client 的简单使用(1)
依赖: <properties> <java.version>1.8</java.version> <feign-core.version>10.2.0 ...
- No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-netflix-ribbon?
... 60 common frames omittedCaused by: java.lang.IllegalStateException: No Feign Client for loadBala ...
- spring cloud关于feign client的调用对象列表参数、设置header参数、多环境动态参数试配
spring cloud关于feign client的调用 1.有些场景接口参数需要传对象列表参数 2.有些场景接口设置设置权限等约定header参数 3.有些场景虽然用的是feign调用,但并不会走 ...
- Feign中使用hystrix
Feign中使用hystrix 一.在Order工程中的bootstrap.yml中增加配置 feign: hystrix: enabled: true
- spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?
Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢? 造成该问题的原因 Hystrix默认的超时时间是1秒,如果超过这个时间 ...
- Feign 报错:No fallback instance of type class xxx found for feign client xxx
通常需要确认配置内容: 开启 Hystrix:feign.hystrix.enabled=true Fallback类需要注解@Component 出处:https://www.jianshu.com ...
随机推荐
- Unity和虚幻的比较
很多人从Unity开始转向虚幻4了,我目前则相反,从研究使用虚幻4,回到了Unity 5上. 前端总结的Unity和Unreal 4的一些优缺点,自己做的对比图.就先放这里了. 其实,作为引擎,各有优 ...
- Android的读写文件及权限设置
drwx read write excute openFileOutput(name,drwx); 用系统api读取文件 设置文件生成的权限: public static boolean sav ...
- Android英文文档翻译系列(1)——AlarmManager
原文:个人翻译,水平有限,欢迎看官指正. public class Ala ...
- office2010如何使用excel冻结窗格
当我们在制作一个Excel表格时,如果列数较多,行数也较多时,一旦向下滚屏,则上面的标题行也跟着滚动,在处理数据时往往难以分清各列数据对应的标题,事实上利用"冻结窗格"功能可以很好 ...
- Rsync匿名访问漏洞
前言 前两天总结了互联网或者说IT公司内网常见的漏洞,然后决定针对还没学习过不了解的漏洞进行学习了解,所以准备一一针对来研习,今天是第一篇,立一个Flag,争取今年搞定,为啥说的这么艰难,因为平时工作 ...
- windows下的mysql迁移到linux下
最近做毕业设计,需要把windows下的mysql移植到linux下 曾经有过在window下移植mysql数据库的经验,只需要把msql的数据文件复制到另一台安装mysql的机器的数据存放位置,然后 ...
- C# DateTime.Now详细用法
//2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...
- IOS中使用轻量级数据库
IOS中使用轻量级数据库 目录 概述 IOS中的轻量级数据库 sqlite的方法 数据库的实用操作 第三方类库 FMDatabase 概述 IOS中的轻量级数据库 sqlite的方法 sqlite3 ...
- java的list集合操作List<T>转化List<Long>
java的list集合操作List<T>转化List<Long> package com.google.common.collect; import com.google.co ...
- C++ 引用的辨别
对于习惯使用C进行开发的朋友们,在看到c++中出现的&符号,可能会犯迷糊,因为在C语言中这个符号表示了取地址符,但是在C++中它却有着不同的用途,掌握C++的&符号,是提高代码执行效率 ...