第十六章 Spring cloud 重寫Feign+單個禁用Feign的Hystrix支援
重寫Feign+單個禁用Feign的Hystrix支援
如果Hystrix在類路徑和feign上hystrix。
啟用= true,Feign將用一個斷路器來包裝所有的方法。
返回一個com.netflix.hystrix.HystrixCommand也是可用的。
這允許您使用反應模式(呼叫. toobservable()或. observe()或非同步使用(呼叫. queue())。
要在每個客戶機基礎上禁用Hystrix支援,可以建立一個香草假。使用“原型”範圍的構建器,例如:
ConfigurationAuth類
package com.example.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; /** * 此類不要放在啟動類能掃描到的包下 * @author 重寫Feign的預設配置 * */ @Configuration public class ConfigurationAuth { //配置註冊到Eureka所需要的賬戶和密碼 @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("user", "123456"); } /** * 禁用單個feign的hystrix支援 * @return */ @Bean @Scope("prototype") public Feign.Builder feignBuilder() { return Feign.builder(); } }
ConfigurationContract類
package com.example.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import feign.Contract; import feign.Logger; /** * 此類不要放在啟動類能掃描到的包下 * @author 重寫Feign的預設配置 * */ @Configuration public class ConfigurationContract { //Contract預設是Spring MVC的契約,就可以用Spring MVC的註解 @Bean public Contract feignContract() { return new feign.Contract.Default(); } //配置日誌級別 @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
OrderController類
package com.example.demo.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.example.demo.entity.User; import com.example.demo.feign.FeignClientServiceName; import com.example.demo.feign.UserFeignClient; //宣告式服務 @RestController public class OrderController { @Autowired private UserFeignClient userFeignClient; @Autowired private FeignClientServiceName feignClientServiceName; @GetMapping("/order/{id}") public User findById(@PathVariable Long id) { return this.userFeignClient.findById(id); } @GetMapping("/{serviceName}") public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) { return this.feignClientServiceName.findServiceInfoFromEurekaByServiceName(serviceName); } }
User類
package com.example.demo.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
FeignClientServiceHystrixFeignFallBack類
package com.example.demo.feign;
import org.springframework.stereotype.Component;
@Component
public class FeignClientServiceHystrixFeignFallBack implements FeignClientServiceName {
@Override
public String findServiceInfoFromEurekaByServiceName(String serviceName) {
return "hello";
}
}
FeignClientServiceName介面
package com.example.demo.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.config.ConfigurationAuth;
@FeignClient(name = "aaaa", url = "http://localhost:8761/", configuration = ConfigurationAuth.class,fallback=FeignClientServiceHystrixFeignFallBack.class)
public interface FeignClientServiceName {
@RequestMapping(value = "/eureka/apps/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}
UserFeignClient介面
package com.example.demo.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import com.example.config.ConfigurationContract;
import com.example.demo.entity.User;
import feign.Param;
import feign.RequestLine;
//重寫Feign的預設配置
@FeignClient(name = "spring-cloud-user", configuration = ConfigurationContract.class,fallback=UserHystrixFeignFallBack.class) // 呼叫哪個服務的名稱
public interface UserFeignClient {
@RequestLine("GET /user/{id}") //注意:GET後面有一個空格
public User findById(@Param("id") Long id);
UserHystrixFeignFallBack類
package com.example.demo.feign;
import org.springframework.stereotype.Component;
import com.example.demo.entity.User;
@Component
public class UserHystrixFeignFallBack implements UserFeignClient {
@Override
public User findById(Long id) {
return new User();
}
}
SpringCloudOrderApplication類
package com.example.demo;
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//表示為Fegin客戶端
public class SpringCloudOrderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudOrderApplication.class, args);
}
}
application.yml配置
spring:
application:
name: spring-cloud-order_overriding_feign_defaults-hystrix #微服務的名稱
server:
port: 7907 #微服務埠號
eureka:
client:
healthcheck:
enabled: true # 開啟健康檢查
serviceUrl:
defaultZone: http://user:[email protected]:8761/eureka #服務eureka的URL地址
instance:
prefer-ip-address: true
logging:
level:
com.example.demo.feign.UserFeignClient: DEBUG
# 解決第一次請求報超時異常的方案:
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
# 或者:
# hystrix.command.default.execution.timeout.enabled: false
# 或者:
# feign.hystrix.enabled: false ## 索性禁用feign的hystrix支援
# 超時的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
pom.xml配置
<?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>
<groupId>com.example.demo</groupId>
<artifactId>spring-cloud-order_overriding_feign_defaults</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-cloud-order_overriding_feign_defaults</name>
<description>spring-cloud-order_overriding_feign_defaults</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<!-- 檔案拷貝時的編碼 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 編譯時的編碼 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<!-- jdk版本 -->
<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>
<!-- 這個庫讓我們可以訪問應用的很多資訊,包括:/env、/info、/metrics、/health等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 新增宣告式feign依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- 用於熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- 版本依賴管理,故之後新增依賴無需指定version -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- 用以為integration-test提供支援。 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
把使用者服務停掉之後,效果如下:
檢視健康指標:hystrix起作用了
相關推薦
第十六章 Spring cloud 重寫Feign+單個禁用Feign的Hystrix支援
重寫Feign+單個禁用Feign的Hystrix支援 如果Hystrix在類路徑和feign上hystrix。 啟用= true,Feign將用一個斷路器來包裝所有的方法。 返回一個com.netflix.hystrix.HystrixCommand也是可用的。 這允許您
【MySQL必知必會】第十六章 創建高級聯結
類型 where子句 contact items order by 其他 mysq custom 必知必會 1、使用表別名 好處: a、縮短SQL語句。 b、允許在單條SELECT語句中多次使用相同的表。 輸入: SELECT cust_name,cu
第十六章-進程和線程
lan 進程 命名 永遠 大小 無法使用 inux wait 沒有 對於操作系統來說, 一個任務就是一個進程(Process) 進程內的這些“子任務”稱為線程(Thread) 真正的並行執行多任務只能在多核CPU上實現 多任務的實現有3種方式:
算法導論筆記——第十六章 貪心算法
一個 出現 預處理 優化 節點 求解 多選 數據結構 集中 通常用於最優化問題,我們做出一組選擇來達到最優解。每步都追求局部最優。對很多問題都能求得最優解,而且速度比動態規劃方法快得多。 16.1 活動選擇問題 按結束時間排序,然後選擇兼容活動。 定理16.1 考慮任意
第十六章 沒有銀彈 ---軟件工程中的根本和次要問題
測試 未來 接收 增長 ada tail 進行 tro 困難 http://blog.csdn.net/zuochao_2013/article/details/73614151 在未來的十年內,無論是在技術還是管理方法上,都看不出有任何突破性的進步,能夠保證在十年內大幅度
進擊的Python【第十六章】:Web前端基礎之jQuery
name cat 隱藏 function wid get val 綁定 des 進擊的Python【第十六章】:Web前端基礎之jQuery 一、什麽是 jQuery ? jQuery是一個JavaScript函數庫。 jQuery是一個輕量級的"寫的少,做的多"的Java
從零開始的linux 第十六章
rm -rf /* glob通配符的用法詳解從零開始的linux 第十六章路人乙:“啊,小編你越來越過分了,這次居然推遲了一天才來!!”(嬉皮笑臉)嘻嘻~~抱歉啦同學們,小編昨天因為做LNMP以及負載均衡的DR工作模式的時候出了點小問題,原理部分與小編理解的有點誤差~~不過小編這麽厲害肯定是已經解決
算法導論第十六章
LG ted sum 不能 復雜度 selector else n-1 greedy 16.1 16.1-1 int c[n+1][n+1]; int b[n+1][n+1]; for (int i = 0; i <= n; i++) { for (int j =
java基礎 第十六章(連接數據庫)
ID 循環 exec manage () result creat AS 新的 連接數據庫(JDBC接口) 步驟如下: 先到數據庫jar包。 1.加載驅動 Class.forName("包路徑"); 例:Class.forName("com.mysql.jd
第十六章 在文件中搜索文本工具:grep命令 和egrep命令
oot his a-z 多個 查找 sea 內容 args lar 第十六章 在文件中搜索文本工具:grep命令 和egrep命令 名詞解釋 grep(global search regular expression(RE)and print out the line,全面
(十六) 整合spring cloud雲架構 -使用spring cloud Bus刷新配置
start ron 興趣 def cli localhost wid inf 存儲 我們使用spring cloud分布式微服務雲架構做了b2b2c的電子商務系統,除了架構本身自帶的系統服務外,我們將b2b2c的業務服務進行了細粒度拆分,做成了不同的業務微服務。 當我們的業
第十六章 創建高級聯結
sql where 們的 卡爾 類型 不同 custom 可能 工作 學習目的: 了解另外一些聯結類型,包括它們的含義和使用方法,如何對被聯結的表使用別名和聚集函數。 使用表別名: 別名除了用於列名和計算字段外,SQL還允許給表名起別名。這樣做主要有兩個理由: 縮短S
【練習題】第十六章--類和函式(Think Python)
class Time: hour=0 minute=0 second=0 def print_time(t): print("%.2d:%.2d:%.2d"%(t.hour,t.minute,t.second)) def is_after(t1,t2):
強化學習(RLAI)讀書筆記第十六章Applications and Case Studies(alphago)
強化學習(RLAI)讀書筆記第十六章Applications and Case Studies(alphago) 16.6 Mastering the Game of Go 16.6.1 AlphaGo 16.6.2 AlphaG
強化學習(RLAI)讀書筆記第十六章Applications and Case Studies(不含alphago)
強化學習(RLAI)讀書筆記第十六章Applications and Case Studies(不含alphago) 16.1 TD-Gammon 16.2 Samuel's Checkers Player 16.3 Watson‘s Dail
c++ primer 第十六章習題
c++ primer 第十六章習題 練習16.1 例項化是指傳遞給模板相應的模板引數,使其生成對應型別的函式體或是型別定義。 練習16.2 #include <iostream> #include <string> #include
《Linux Device Drivers》第十六章 塊裝置驅動程式——note
簡介 一個塊裝置驅動程式主要通過傳輸固定大小的隨機資料來訪問裝置 Linux核心視塊裝置為與字元裝置相異的基本裝置型別 Linux塊裝置驅動程式介面使得塊裝置可以發揮其最大的功效,但是其複雜程式又是程式設計者必須面對的一個問題 一個數據塊指的是固
《java程式設計思想》第十六章 陣列
16.1 陣列為什麼特殊 (1)出現泛型之前: 陣列與其他種類的容器之間的區別有三方面:效率、型別和儲存基本型別的能力。 (2)泛型之後: 泛型的出現使得容器也具備了型別檢查的能力,而自動裝箱機制使容器可以與陣列幾乎一模一樣的用於基本型別,陣列的碩果僅存的優點就是效率。
C++學習筆記——C++ Primer Plus中文第六版 第十六章STL程式設計練習解答
發現答案資源不全,因此貼出自己的解答,都為STL應用基礎題,如有謬誤,還請不吝賜教。 第一題 要求:迴文字串判斷(假定字串中沒有大小寫、空格、標點符號等問題) 解答: #include<iostream> #include<string>
Java架構-(十六) 整合spring cloud雲架構 -使用spring cloud Bus重新整理配置
我們使用spring cloud分散式微服務雲架構做了b2b2c的電子商務系統,除了架構本身自帶的系統服務外,我們將b2b2c的業務服務進行了細粒度拆分,做成了不同的業務微服務。 當我們的業務系統越來越龐大複雜的時候,各種配置也會隨之增多。配置檔案只要一修改,會對commonserv