博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springcloud9----feign-client-without-hystrix
阅读量:5040 次
发布时间:2019-06-12

本文共 6126 字,大约阅读时间需要 20 分钟。

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@EnableFeignClientspublic 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;@RestControllerpublic 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;@Componentpublic 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;@Configurationpublic 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;@Configurationpublic 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-hystrixserver:  port: 7901eureka:  client:    healthcheck:      enabled: true    serviceUrl:      defaultZone: http://user:password123@localhost:8761/eureka  instance:    prefer-ip-address: truelogging:  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
4.0.0
microservice-consumer-movie-feign-customizing-without-hystrix
jar
com.itmuch.cloud
microservice-spring-cloud
0.0.1-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-feign

 

转载于:https://www.cnblogs.com/yaowen/p/9142228.html

你可能感兴趣的文章
一个Java程序员应该掌握的10项技能
查看>>
c#英文大小写快捷键
查看>>
tpframe免费开源框架又一重大更新
查看>>
一.go语言 struct json相互转换
查看>>
什么是架构设计
查看>>
程序员学习能力提升三要素
查看>>
PHP 微信错误状态返回码说明
查看>>
【4.1】Python中的序列分类
查看>>
ubuntu 移动文件
查看>>
Easy Mock
查看>>
看看 Delphi XE2 为 VCL 提供的 14 种样式
查看>>
Python内置函数(29)——help
查看>>
机器学习系列-tensorflow-01-急切执行API
查看>>
SqlServer 遍历修改字段长度
查看>>
Eclipse快捷键:同时显示两个一模一样的代码窗口
查看>>
《架构之美》阅读笔记05
查看>>
《大道至简》读后感——论沟通的重要性
查看>>
JDBC基础篇(MYSQL)——使用statement执行DQL语句(select)
查看>>
关于React中props与state的一知半解
查看>>
java中Hashtable和HashMap的区别(转)
查看>>