摘要:不和在同級(jí)目錄,因?yàn)槲臋n有說明,該配置文件不需要被掃描到。添加訪問層自定義控制器。添加電影微服務(wù)啟動(dòng)類電影微服務(wù)使用定制化在客戶端進(jìn)行負(fù)載均衡調(diào)度并為配置帳號(hào)密碼登錄認(rèn)證。注解表示該電影微服務(wù)已經(jīng)接入模塊。
SpringCloud(第 013 篇)電影微服務(wù)使用定制化 Feign 在客戶端進(jìn)行負(fù)載均衡調(diào)度并為 Feign 配置帳號(hào)密碼登錄認(rèn)證 Eureka
-
一、大致介紹1、定制 Feign 實(shí)現(xiàn)訪問遠(yuǎn)端微服務(wù); 2、為 Feign 配置帳號(hào)密碼來登錄認(rèn)證 Eureka 服務(wù)發(fā)現(xiàn)模塊; 3、修改 Feign 的日志打印級(jí)別; 4、定制 Feign 也毫不掩飾的支持負(fù)載均衡調(diào)度功能;二、實(shí)現(xiàn)步驟 2.1 添加 maven 引用包
2.2 添加應(yīng)用配置文件(springms-consumer-movie-feign-customsrcmainresourcesapplication.yml)4.0.0 springms-consumer-movie-feign-custom 1.0-SNAPSHOT jar com.springms.cloud springms-spring-cloud 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-feign
spring: application: name: springms-consumer-movie-feign-custom server: port: 8050 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 # 解決第一次請(qǐng)求報(bào)超時(shí)異常的方案,因?yàn)?hystrix 的默認(rèn)超時(shí)時(shí)間是 1 秒,因此請(qǐng)求超過該時(shí)間后,就會(huì)出現(xiàn)頁面超時(shí)顯示 : # # 這里就介紹大概三種方式來解決超時(shí)的問題,解決方案如下: # # 第一種方式:將 hystrix 的超時(shí)時(shí)間設(shè)置成 5000 毫秒 # hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000 # # 或者: # 第二種方式:將 hystrix 的超時(shí)時(shí)間直接禁用掉,這樣就沒有超時(shí)的一說了,因?yàn)橛肋h(yuǎn)也不會(huì)超時(shí)了 # hystrix.command.default.execution.timeout.enabled: false # # 或者: # 第三種方式:索性禁用feign的hystrix支持 feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持 # 超時(shí)的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768 # 超時(shí)的解決方案: 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.timeoutInMilliseconds2.3 添加實(shí)體用戶類User(springms-consumer-movie-feign-customsrcmainjavacomspringmscloudentityUser.java)
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; } }2.4 添加訪問遠(yuǎn)端服務(wù) Feign 客戶端(springms-consumer-movie-feign-customsrcmainjavacomspringmscloudfeignUserFeignCustomClient.java)
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請(qǐng)求的客戶端,F(xiàn)eignClient 注解地方采用了自定義的配置。 * * 注解FeignClient的傳參:表示的是注冊(cè)到 Eureka 服務(wù)上的模塊名稱。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/19 * */ @FeignClient(name = "springms-provider-user", configuration = TestFeignCustomConfiguration.class) public interface UserFeignCustomClient { /** * 這里的注解 RequestLine、Param 是 Feign 的配置新的注解,詳細(xì)請(qǐng)參考鏈接: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") List2.5 添加登錄認(rèn)證Eureka服務(wù) Feign 客戶端(springms-consumer-movie-feign-customsrcmainjavacomspringmscloudfeignUserFeignCustomSecondClient.java)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 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請(qǐng)求的客戶端,F(xiàn)eignClient 注解地方采用了自定義的配置。 * * 注解FeignClient的傳參:表示的是注冊(cè)到 Eureka 服務(wù)上的模塊名稱。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/19 * */ @FeignClient(name = "xxx", url = "http://localhost:8761/", configuration = TestEurekaAuthConfiguration.class) public interface UserFeignCustomSecondClient { @RequestMapping(value = "/eureka/apps/{serviceName}") public String findEurekaInfo(@PathVariable("serviceName") String serviceName); }2.6 添加訪問遠(yuǎn)端服務(wù)配置類(springms-consumer-movie-feign-customsrcmainjavacomspringmsconfigTestFeignCustomConfiguration.java)
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 在同級(jí)目錄,因?yàn)槲臋n有說明,該配置文件不需要被掃描到。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/19 * */ @Configuration public class TestFeignCustomConfiguration { @Bean public Contract feignContract(){ return new feign.Contract.Default(); } /** * 日志級(jí)別配置 * * @return */ @Bean Logger.Level feignLoggerLevel(){ return Logger.Level.FULL; } }2.7 添加登錄認(rèn)證Eureka服務(wù)配置(springms-consumer-movie-feign-customsrcmainjavacomspringmsconfigTestEurekaAuthConfiguration.java)
package com.springms.config; import feign.auth.BasicAuthRequestInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 認(rèn)證配置,由于 UserFeignCustomSecondClient 訪問 http://localhost:8761/ 需要密碼登錄,所以才有了此配置的出現(xiàn)。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/19 * */ @Configuration public class TestEurekaAuthConfiguration { /** * 此方法主要配置登錄 Eureka 服務(wù)器的帳號(hào)與密碼。 * * @return */ @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor(){ return new BasicAuthRequestInterceptor("admin", "admin"); } }2.8 添加Web訪問層Controller(springms-consumer-movie-feign-customsrcmainjavacomspringmscloudcontrollerMovieFeignCustomController.java)
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; /** * 自定義 Feign 控制器。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/19 * */ @RestController public class MovieFeignCustomController { @Autowired private UserFeignCustomClient userFeignCustomClient; @Autowired private UserFeignCustomSecondClient userFeignCustomSecondClient; @GetMapping("/movie/{id}") public User findById(@PathVariable Long id) { return userFeignCustomClient.findById(id); } @GetMapping("/{serviceName}") public String findEurekaInfo(@PathVariable String serviceName){ return userFeignCustomSecondClient.findEurekaInfo(serviceName); } }2.9 添加電影微服務(wù)啟動(dòng)類(springms-consumer-movie-feign-customsrcmainjavacomspringmscloudMsConsumerMovieFeignCustomApplication.java)
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; /** * * 電影微服務(wù)使用定制化 Feign 在客戶端進(jìn)行負(fù)載均衡調(diào)度并為 Feign 配置帳號(hào)密碼登錄認(rèn)證 Eureka。 * * Feign: Java HTTP 客戶端開發(fā)的工具。 * * 注解 EnableFeignClients 表示該電影微服務(wù)已經(jīng)接入 Feign 模塊。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/19 * */ @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class MsConsumerMovieFeignCustomApplication { public static void main(String[] args) { SpringApplication.run(MsConsumerMovieFeignCustomApplication.class, args); System.out.println("【【【【【【 電影自定義Feign微服務(wù) 】】】】】】已啟動(dòng)."); } }三、測(cè)試
/**************************************************************************************** 一、電影微服務(wù)使用定制化Feign在客戶端進(jìn)行負(fù)載均衡調(diào)度并為Feign配置帳號(hào)密碼登錄認(rèn)證Eureka(測(cè)試接入 Feign 模塊): 1、注解:EnableFeignClients 2、啟動(dòng) springms-discovery-eureka 模塊服務(wù),啟動(dòng)1個(gè)端口; 3、啟動(dòng) springms-provider-user 模塊服務(wù),啟動(dòng)1個(gè)端口; 4、啟動(dòng) springms-consumer-movie-feign-custom 模塊服務(wù); 5、在瀏覽器輸入地址http://localhost:8050/movie/1 可以看到信息成功的被打印出來; 總結(jié):說明接入 Feign 已經(jīng)成功通過測(cè)試; ****************************************************************************************/ /**************************************************************************************** 二、電影微服務(wù)使用定制化Feign在客戶端進(jìn)行負(fù)載均衡調(diào)度并為Feign配置帳號(hào)密碼登錄認(rèn)證Eureka(測(cè)試登錄 Eureka 服務(wù)器需要認(rèn)證配置): 1、注解:EnableFeignClients 2、啟動(dòng) springms-discovery-eureka 模塊服務(wù),啟動(dòng)1個(gè)端口; 3、啟動(dòng) springms-provider-user 模塊服務(wù),啟動(dòng)1個(gè)端口; 4、啟動(dòng) springms-consumer-movie-feign-custom 模塊服務(wù); 5、在瀏覽器輸入地址 http://localhost:8050/springms-provider-user 可以看到信息成功的被打印出來; 總結(jié):說明 TestEurekaAuthConfiguration 類中配置的帳號(hào)密碼已經(jīng)生效,可以正常訪問 Eureka 服務(wù); ****************************************************************************************/ /**************************************************************************************** 三、電影微服務(wù)使用定制化Feign在客戶端進(jìn)行負(fù)載均衡調(diào)度并為Feign配置帳號(hào)密碼登錄認(rèn)證Eureka(測(cè)試接入 Feign 模塊進(jìn)行負(fù)載均衡): 1、注解:EnableFeignClients 2、啟動(dòng) springms-discovery-eureka 模塊服務(wù),啟動(dòng)1個(gè)端口; 3、啟動(dòng) springms-provider-user 模塊服務(wù),啟動(dòng)3個(gè)端口(7900、7899、7898); 4、啟動(dòng) springms-consumer-movie-feign-custom 模塊服務(wù); 5、在瀏覽器輸入地址http://localhost:8050/movie/1 連續(xù)刷新9次,正常情況可以看到 springms-provider-user 的3個(gè)端口輪詢打印用戶日志信息; 總結(jié):1、說明接入 Feign 已經(jīng)成功的在客戶端進(jìn)行了負(fù)載均衡處理; 2、之所以會(huì)在客戶端進(jìn)行輪詢打印日志信息,是因?yàn)闆]有配置調(diào)度算法,而默認(rèn)的調(diào)度算法就是輪詢,所以會(huì)出現(xiàn)輪詢打印日志信息; ****************************************************************************************/ /**************************************************************************************** 四、電影微服務(wù)使用定制化Feign在客戶端進(jìn)行負(fù)載均衡調(diào)度并為Feign配置帳號(hào)密碼登錄認(rèn)證Eureka(配置日志級(jí)別): 1、application.yml 修改:logging.level.com.springms.cloud.feign.UserFeignCustomClient: DEBUG 2、編寫 TestFeignCustomConfiguration 新增日志級(jí)別的方法 3、啟動(dòng) springms-discovery-eureka 模塊服務(wù),啟動(dòng)1個(gè)端口; 4、啟動(dòng) springms-provider-user 模塊服務(wù),啟動(dòng)1個(gè)端口; 5、啟動(dòng) springms-consumer-movie-feign-custom 模塊服務(wù); 6、在瀏覽器輸入地址http://localhost:8050/springms-provider-user 可以看到控制臺(tái)中 DEBUG 日志級(jí)別的信息成功的被打印出來; ****************************************************************************************/四、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關(guān)注,您的肯定是對(duì)我最大的支持!!!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70497.html
摘要:在該配置中,加入這個(gè)方法的話,表明使用了該配置的地方,就會(huì)禁用該模塊使用容災(zāi)降級(jí)的功能添加訪問層添加電影微服務(wù)啟動(dòng)類電影微服務(wù),定制,一個(gè)功能禁用,另一個(gè)功能啟用。 SpringCloud(第 016 篇)電影微服務(wù),定制Feign,一個(gè)Feign功能禁用Hystrix,另一個(gè)Feign功能啟用Hystrix - 一、大致介紹 1、在一些場(chǎng)景中,部分功能需要使用斷路器功能,部分功能不需...
摘要:添加電影微服務(wù)啟動(dòng)類電影微服務(wù)接入進(jìn)行客戶端負(fù)載均衡,通過調(diào)用遠(yuǎn)程微服務(wù)。注解表示該電影微服務(wù)已經(jīng)接入模塊。 SpringCloud(第 012 篇)電影微服務(wù)接入 Feign 進(jìn)行客戶端負(fù)載均衡,通過 FeignClient 調(diào)用遠(yuǎn)程 Http 微服務(wù) - 一、大致介紹 1、本章節(jié)主要介紹在 SpringCloud 生態(tài)圈中,使用一個(gè)類似于 Java HTTP 客戶端的工具 Feig...
摘要:服務(wù)器充當(dāng)微服務(wù)架構(gòu)體系中的服務(wù)注冊(cè)中心。其他的微服務(wù),使用客戶端連接到,并維持心跳連接,系統(tǒng)維護(hù)人員可以通過來監(jiān)控系統(tǒng)中各個(gè)微服務(wù)是否正常運(yùn)行。 當(dāng)前微服務(wù)架構(gòu)逐漸成為系統(tǒng)開發(fā)的主流方向,搭建微服務(wù)方式可以有多種,例如springcloud、dubbo、k8s等,本系列文章將基于本人參與的實(shí)際項(xiàng)目,來介紹基于springcloud搭建微服務(wù)的方式,Spring Cloud是一個(gè)基于S...
摘要:系統(tǒng)中的各個(gè)微服務(wù)可被獨(dú)立部署,各個(gè)微服務(wù)之間是松耦合的。每個(gè)微服務(wù)僅關(guān)注于完成一件任務(wù)并很好地完成該任務(wù)。傳統(tǒng)架構(gòu)升級(jí)困難。新的輕量級(jí)協(xié)議容器化的出現(xiàn)。熔斷處理在微服務(wù)出現(xiàn)問題時(shí)防止出現(xiàn)雪崩效應(yīng)。 聊完Spring Boot,我們來看看Spring Boot最重要的一方面的應(yīng)用——Spring Cloud。 Spring Cloud 再聊SpringCloud之前我們先聊聊微服務(wù)。 ...
摘要:服務(wù)發(fā)現(xiàn)服務(wù)治理注冊(cè)中心將所有微服務(wù)注冊(cè)到一個(gè)上,然后通過心跳進(jìn)行服務(wù)健康監(jiān)測(cè)。 服務(wù)發(fā)現(xiàn) | 服務(wù)治理 | 注冊(cè)中心 將所有微服務(wù)注冊(cè)到一個(gè)Server上,然后通過心跳進(jìn)行服務(wù)健康監(jiān)測(cè)。這樣服務(wù)A調(diào)用服務(wù)B可以通過注冊(cè)中心獲取服務(wù)B的地址、端口調(diào)用 Eureka - Eureka 提供云端服務(wù)發(fā)現(xiàn),一個(gè)基于 REST 的服務(wù),用于定位服務(wù),以實(shí)現(xiàn)云端中間層服務(wù)發(fā)現(xiàn)和故障轉(zhuǎn)移 S...
閱讀 2868·2021-11-22 11:56
閱讀 3568·2021-11-15 11:39
閱讀 913·2021-09-24 09:48
閱讀 774·2021-08-17 10:14
閱讀 1340·2019-08-30 15:55
閱讀 2766·2019-08-30 15:55
閱讀 1325·2019-08-30 15:44
閱讀 2794·2019-08-30 10:59