-java
一、在一些場景中,部分功能須要使用斷路器功能,部分功能不須要斷路器功能,因此纔有了本章節的介紹; 二、定製 Feign 的時候,能夠爲 Feign 設置 Configuration 配置,在該配置中能夠設置是否須要斷路器功能; 三、該定製的 Configuration 在官方文件介紹中特地提到不須要被啓動類所在的包下,由於該配置類不不要被掃描到; 四、固然,此章節定製後的 Feign 一樣支持客戶端負載均衡功能,只要開啓多個用戶微服務便可; 五、這裏提到爲何開啓多個微服務就能作到客戶端負載均衡呢? - 簡答的說就是 Feign 能夠看做僅僅只是一個請求網絡已被封裝好的客戶端HTTP請求工具類,將發出去的請求封裝好; - 至於要請求到哪臺遠端微服務的話,剩下的就交給 Ribbon 來處理了,並且默認集成使用了負載均衡的Ribbon客戶端; - 天然而然添加多個用戶微服務,Feign天然就支持負載均衡的功能。
<?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>springms-consumer-movie-feign-custom-without-hystrix</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.springms.cloud</groupId> <artifactId>springms-spring-cloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <!-- web模塊 --> <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> <!-- Java HTTP 客戶端開發的工具的模塊 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <!-- Hystrix 斷路器模塊 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> </dependencies> </project>
spring: application: name: springms-consumer-movie-feign-custom-without-hystrix server: port: 8110 eureka: client: # healthcheck: # enabled: true serviceUrl: defaultZone: http://admin:admin@localhost:8761/eureka instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} logging: level: com.springms.cloud.feign.UserFeignCustomClient: DEBUG # 解決第一次請求報超時異常的方案,由於 hystrix 的默認超時時間是 1 秒,所以請求超過該時間後,就會出現頁面超時顯示 : # # 這裏就介紹大概三種方式來解決超時的問題,解決方案以下: # # 第一種方式:將 hystrix 的超時時間設置成 5000 毫秒 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000 # # 或者: # 第二種方式:將 hystrix 的超時時間直接禁用掉,這樣就沒有超時的一說了,由於永遠也不會超時了 # hystrix.command.default.execution.timeout.enabled: false # # 或者: # 第三種方式:索性禁用feign的hystrix支持 # 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
package com.springms.cloud.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; } }
package com.springms.cloud.feign; import com.springms.cloud.entity.User; import com.springms.config.TestFeignCustomConfiguration; import feign.Param; import feign.RequestLine; import org.springframework.cloud.netflix.feign.FeignClient; /** * 用戶Http請求的客戶端,FeignClient 註解地方採用了自定義的配置。 * * 註解FeignClient的傳參:表示的是註冊到 Eureka 服務上的模塊名稱。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/24 * */ @FeignClient(name = "springms-provider-user", configuration = TestFeignCustomConfiguration.class, fallback = UserFeignCustomClientFallback.class) public interface UserFeignCustomClient { /** * 這裏的註解 RequestLine、Param 是 Feign 的配置新的註解,詳細請參考連接:https://github.com/OpenFeign/feign * * @param id * @return */ @RequestLine("GET /simple/{id}") public User findById(@Param("id") Long id); } /**************************************************************************************** 參考代碼以下: interface GitHub { @RequestLine("GET /repos/{owner}/{repo}/contributors") List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo); } static class Contributor { String login; int contributions; } public static void main(String... args) { GitHub github = Feign.builder().decoder(new GsonDecoder()).target(GitHub.class, "https://api.github.com"); // Fetch and print a list of the contributors to this library. List<Contributor> contributors = github.contributors("OpenFeign", "feign"); for (Contributor contributor : contributors) { System.out.println(contributor.login + " (" + contributor.contributions + ")"); } } ****************************************************************************************/
package com.springms.cloud.feign; import com.springms.config.TestEurekaAuthConfiguration; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; /** * 用戶Http請求的客戶端,FeignClient 註解地方採用了自定義的配置。 * * 註解FeignClient的傳參:表示的是註冊到 Eureka 服務上的模塊名稱。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/24 * */ @FeignClient(name = "xxx", url = "http://localhost:8761/", configuration = TestEurekaAuthConfiguration.class, fallback = UserFeignCustomSecondClientFallback.class) public interface UserFeignCustomSecondClient { @RequestMapping(value = "/eureka/apps/{serviceName}") public String findEurekaInfo(@PathVariable("serviceName") String serviceName); }
package com.springms.cloud.feign; import com.springms.cloud.entity.User; import org.springframework.stereotype.Component; /** * Hystrix 客戶端回退機制類。 * * 這裏加上註解 Component 的目的:就是由於沒有這個註解,運行時候會報錯,報錯會說沒有該類的這個實例,因此咱們就想到要實例化這個類,所以加了這個註解。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/24 * */ @Component public class UserFeignCustomClientFallback implements UserFeignCustomClient{ /** * Fallback 回退降級的方法,返回一個默認的用戶信息。 * * @param id * @return */ @Override public User findById(Long id) { System.out.println("========== findById Fallback " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName()); User tmpUser = new User(); tmpUser.setId(0L); return tmpUser; } }
package com.springms.cloud.feign; import org.springframework.stereotype.Component; /** * Hystrix 客戶端回退機制類。 * * 這裏加上註解 Component 的目的:就是由於沒有這個註解,運行時候會報錯,報錯會說沒有該類的這個實例,因此咱們就想到要實例化這個類,所以加了這個註解。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/24 * */ @Component public class UserFeignCustomSecondClientFallback implements UserFeignCustomSecondClient{ @Override public String findEurekaInfo(String serviceName) { System.out.println("========== findEurekaInfo Fallback " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName()); return "springms-provider-user"; } }
package com.springms.config; import feign.Contract; import feign.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 自定義配置。 * * 不和 com.springms.cloud 在同級目錄,由於文檔有說明,不要被掃描到便可。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/24 * */ @Configuration public class TestFeignCustomConfiguration { @Bean public Contract feignContract(){ return new feign.Contract.Default(); } /** * 日誌級別配置 * * @return */ @Bean Logger.Level feignLoggerLevel(){ return Logger.Level.FULL; } }
package com.springms.config; import feign.Feign; import feign.auth.BasicAuthRequestInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; /** * 認證配置,因爲 UserFeignCustomSecondClient 訪問 http://localhost:8761/ 須要密碼登陸,因此纔有了此配置的出現。 * * 不和 com.springms.cloud 在同級目錄,由於文檔有說明,不要被掃描到便可。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/24 * */ @Configuration public class TestEurekaAuthConfiguration { /** * 此方法主要配置登陸 Eureka 服務器的賬號與密碼。 * * @return */ @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor(){ return new BasicAuthRequestInterceptor("admin", "admin"); } /** * 在該配置中,加入這個方法的話,代表使用了該配置的地方,就會禁用該模塊使用 Hystrix 容災降級的功能; * * @return */ @Bean @Scope("prototype") public Feign.Builder feignBuilder(){ return Feign.builder(); } }
package com.springms.cloud.controller; import com.springms.cloud.entity.User; import com.springms.cloud.feign.UserFeignCustomClient; import com.springms.cloud.feign.UserFeignCustomSecondClient; 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; @RestController public class MovieFeignCustomWithoutHystrixController { @Autowired private UserFeignCustomClient userFeignCustomClient; @Autowired private UserFeignCustomSecondClient userFeignCustomSecondClient; @GetMapping("/movie/{id}") public User findById(@PathVariable Long id) { System.out.println("======== findById Controller " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName()); return userFeignCustomClient.findById(id); } @GetMapping("/{serviceName}") public String findEurekaInfo(@PathVariable String serviceName){ System.out.println("======== findEurekaInfo Controller " + Thread.currentThread().getThreadGroup() + " - " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName()); return userFeignCustomSecondClient.findEurekaInfo(serviceName); } }
package com.springms.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; /** * * 電影微服務,定製Feign,一個Feign功能禁用Hystrix,另外一個Feign功能啓用Hystrix。 * * Feign: Java HTTP 客戶端開發的工具。 * * 註解 EnableFeignClients 表示該電影微服務已經接入 Feign 模塊。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/24 * */ @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class MsConsumerMovieFeignCustomWithoutHystrixApplication { public static void main(String[] args) { SpringApplication.run(MsConsumerMovieFeignCustomWithoutHystrixApplication.class, args); System.out.println("【【【【【【 電影自定義FeignWithoutHystrix微服務 】】】】】】已啓動."); } }
/**************************************************************************************** 1、電影微服務,定製Feign,一個Feign功能禁用Hystrix,另外一個Feign功能啓用Hystrix(正常功能測試): 一、編寫:UserFeignCustomClientFallback、UserFeignCustomSecondClientFallback 斷路器回退客戶端類; 二、編寫 TestEurekaAuthConfiguration 配置,加入禁用 Hystrix 模塊功能的代碼,表示禁用該配置的客戶端模塊; 三、啓動 springms-discovery-eureka 模塊服務,啓動1個端口; 四、啓動 springms-provider-user 模塊服務,啓動1個端口; 五、啓動 springms-consumer-movie-feign-custom-without-hystrix 模塊服務; 六、在瀏覽器輸入地址 http://localhost:8110/movie/1 能夠看到信息成功的被打印出來,代表正常狀況下一切正常; 七、在瀏覽器輸入地址 http://localhost:8110/springms-provider-user 能夠看到信息成功的被打印出來,代表正常狀況下一切正常; 注意:若是第6步若是一開始就輸入ID = 0 的用戶信息,不要灰心,耐心等等,說不定服務之間還沒通訊連接成功呢。。。 若是第6步的地址輸入錯誤,請回頭看看 springms-provider-user 該微服務 appname 的名字,再次輸入http://localhost:8110/ + appname 便可試試; ****************************************************************************************/ /**************************************************************************************** 2、電影微服務,定製Feign,一個Feign功能禁用Hystrix,另外一個Feign功能啓用Hystrix(禁用其中一個 Feign 的斷路器功能): 一、編寫:UserFeignCustomClientFallback、UserFeignCustomSecondClientFallback 斷路器回退客戶端類; 二、編寫 TestEurekaAuthConfiguration 配置,加入禁用 Hystrix 模塊功能的代碼,表示禁用該配置的客戶端模塊; 三、啓動 springms-discovery-eureka 模塊服務,啓動1個端口; 四、啓動 springms-provider-user 模塊服務,啓動1個端口; 五、啓動 springms-consumer-movie-feign-custom-without-hystrix 模塊服務; 六、在瀏覽器輸入地址 http://localhost:8110/movie/1 能夠看到信息成功的被打印出來,代表正常狀況下一切正常; 七、在瀏覽器輸入地址 http://localhost:8110/springms-provider-user 能夠看到信息成功的被打印出來,代表正常狀況下一切正常; 八、關閉 springms-provider-user 模塊服務; 九、在瀏覽器輸入地址 http://localhost:8110/movie/1 能夠看到用戶ID = 0 的用戶信息被打印出來,代表該模塊的Hystrix斷路器模塊起做用了; 十、再關閉 springms-discovery-eureka 模塊服務; 十一、再在瀏覽器輸入地址 http://localhost:8110/appname-springms-provider-user 能夠看到網頁已經打印出連接不上服務的錯誤頁面了,代表該模塊禁用Hystrix斷路器模塊也起做用了; ****************************************************************************************/
https://gitee.com/ylimhhmily/SpringCloudTutorial.gitgit
SpringCloudTutorial交流QQ羣: 235322432github
SpringCloudTutorial交流微信羣: 微信溝通羣二維碼圖片連接web
歡迎關注,您的確定是對我最大的支持!!!spring