javascript
SpringCloud微服务:Ribbon和Feign组件,实现服务调用的负载均衡
一、Ribbon簡介
1、基本概念
Ribbon是一個客戶端的負載均衡(Load Balancer,簡稱LB)器,它提供對大量的HTTP和TCP客戶端的訪問控制。
2、負載均衡簡介
目前主流的負載均衡方案可分成兩類:
1)集中式
即在服務的消費方和提供方之間使用獨立的LB設施,可以是硬件,如F5,也可以是軟件,如nginx,由該設施負責把訪問請求通過某種策略轉發至服務的提供方;
2)進程內
將LB邏輯集成到消費方,消費方從服務注冊中心獲取可用服務列表,然后根據指定負載均衡策略選擇合適的服務器。Ribbon就屬于該方式。
3、Ribbon負載策略
1) RoundRobinRule 輪詢 輪詢服務列表List<Server>的index,選擇index對應位置的服務。 2) RandomRule 隨機 隨機服務列表List<Server>的index,選擇index對應位置的服務。 3) RetryRule 重試 指定時間內,重試(請求)某個服務不成功達到指定次數,則不再請求該服務。二、Feign簡介
1、基本概念
Feign 是一個聲明式的 Web Service 客戶端。它的出現使開發 Web Service 客戶端變得很簡單。使用 Feign 只需要創建一個接口加上對應的注解,比如:@FeignClient 接口類注解。
2、執行流程
三、綜合使用案例
1、項目結構圖
1)、模塊描述
Eureka注冊中心 node02-eureka-7001 兩個服務提供方 node02-provider-6001 node02-provider-6002 Ribbon服務調用 node02-consume-8001 Feign服務調用 node02-consume-80022)、依賴Eureka知識
上篇文章Eureka使用:
2、Ribbon服務調用
代碼所屬模塊:node02-consume-8001
1)、核心依賴
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId> </dependency>2)、配置文件
@Configuration public class LoadConfig {@Bean@LoadBalancedpublic RestTemplate getRestTemplate (){return new RestTemplate() ;}@Beanpublic IRule getIRule (){// 默認輪詢算法// return new RoundRobinRule() ;// 重試算法:默認情況,訪問某個服務連續三次失敗,就不會再訪問// return new RetryRule() ;// 隨機算法return new RandomRule() ;} }3)、調用方式
@RestController public class ConsumeController {@Autowiredprivate RestTemplate restTemplate ;String server_name = "http://NODE02-PROVIDER" ;// http://localhost:8001/showInfo@RequestMapping("/showInfo")public String showInfo (){return restTemplate.getForObject(server_name+"/getInfo",String.class) ;}}這里的NODE02-PROVIDER就是服務提供方的配置文件。兩個服務提供方的這塊配置相同,Ribbon正基于此,實現多個服務調用的負載均衡。
spring:application:name: node02-provider4)、提供方接口
@RequestMapping("/getInfo") public String getInfo (){LOG.info("provider-6002");return "success" ; }3、Feign服務調用
代碼所屬模塊:node02-consume-8002
1)、核心依賴
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId> </dependency>2)、配置文件
@FeignClient(value = "NODE02-PROVIDER") public interface GetAuthorService {@RequestMapping(value = "/getAuthorInfo/{authorId}",method = RequestMethod.GET)String getAuthorInfo (@PathVariable("authorId") String authorId) ; }3)、調用方式
@RestController public class ConsumeController {@Resourceprivate GetAuthorService getAuthorService ;@RequestMapping(value = "/getAuthorInfo")public String getAuthorInfo () {return getAuthorService.getAuthorInfo("1") ;} }4)、啟動類注解
// 因為包名路徑不同,需要加basePackages屬性 @EnableFeignClients(basePackages={"cloud.block.code.service"})5)、提供方接口
@RequestMapping(value = "/getAuthorInfo/{authorId}",method = RequestMethod.GET) public String getAuthorInfo (@PathVariable("authorId") String authorId) {LOG.info("provider-6002");return "知了一笑"+authorId ; }四、源代碼說明
GitHub地址:知了一笑 https://github.com/cicadasmile/spring-cloud-base 碼云地址:知了一笑 https://gitee.com/cicadasmile/spring-cloud-base
總結
以上是生活随笔為你收集整理的SpringCloud微服务:Ribbon和Feign组件,实现服务调用的负载均衡的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TCP/IP入门(1) --链路层
- 下一篇: shell日志切割