6.1 引介
6.2 Ribbon概述
6.2.1 什麼是Ribbon
6.2.2 Ribbon主要作用
1服務調用
eureka內部繼承了ribbon
》在創建RestTemplate的時候,聲明@LoadBalanced
package xx.study.sc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class OrderApplication {
/**
* 使用spring 提供的RestTemplate發送http請求到商品服務
* 1。創建RestTemplate對象交給容器管理
* 2。在使用的時候,調用其方法完成操作
*
*/
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class);
}
}
》使用RestTemplate調用遠程微服務:不需要拼接微服務的URL,以請求的微服務名替換IP地址
package xx.study.sc.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
//注入
@Autowired
private RestTemplate restTemplate;
/**
* ribbon 使用】
* 調用遠程服務
*/
@RequestMapping(value = "/buyTea",method = RequestMethod.GET)
public String buyTea(@RequestParam String name){
name=restTemplate.getForObject("http://service-product/product/buy?name= "+name,String.class);
String returnVal="從註冊中心收到"+name+"!!!";
return returnVal;
}
}
2負載均衡
Ribbon是一個典型的客戶端負載均衡器,Ribbon會獲取服務的所有地址,根據內部的負載均衡算法,獲取本次請求的有效地址。
上述調用就實現了負載均衡,輪詢策略。
6.3負載均衡策略
6.4 請求重試
1增加依賴 spring-retry
<?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">
<parent>
<artifactId>spring_cloud_demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order_service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
</dependencies>
</project>
2 application.properties 增加配置
server.port=9003
#服務名稱
spring.application.name=service-order
#註冊中心訪問地址
eureka.client.service-url.defaultZone=http://localhost:9000/eureka/,http://localhost:8000/eureka/
#使用ip地址註冊
eureka.instance.prefer-ip-address=true
#心跳時間默認30s
eureka.instance.lease-renewal-interval-in-seconds=4
#續約到期時間 默認90s
eureka.instance.lease-expiration-duration-in-seconds=9
#修改ribbon負載均衡策略
service-product.ribbon.NFLoadBalanceRuleClassName:com.netflix.loadbalancer.RandomRule
#ribbon連接的超時時間
service-product.ribbon.ConnectTimeout=250
#ribbon數據讀取的超時時間
service-product.ribbon.ReadTimeout=1000
#是否對所有操作都進行重試
service-product.ribbon.OkToRetryOnAllOperations=true
#切換實例的重試次數
service-product.ribbon.MaxAutoRetriesNextServer=1
#對當前實例的重試次數
service-product.ribbon.MaxAutoRetries=1
6.5 Ribbon源碼