SpringCloud 進階之Ribbon和Feign
阿新 • • 發佈:2018-06-16
bin 選擇服務器 clas cati tar 一個 string etime lis
1. Ribbon 負載均衡
- Spring Cloud Ribbon是基於Netflix Ribbon實現的一套客戶端,負載均衡的工具;
1.1 Ribbon 配置初步
1.1.1 修改 microservicecloud-consumer-dept-80
// pom.xml <!-- Ribbon相關 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> // 修改application.yml,追加eureka的服務註冊地址 eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ // ConfigBean 添加新註解 @LoadBalanced, 用於加入 Ribbon 配置 @Configuration public class ConfigBean { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } } // 主啟動類 DeptConsumer80_App添加 @EnableEurekaClient @SpringBootApplication @EnableEurekaClient public class DeptConsumer80_App { public static void main(String[] args) { SpringApplication.run(DeptConsumer80_App.class, args); } } // 修改 DeptController_Consumer 客戶端訪問類 private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT"; // 測試訪問: // http://localhost:8082/consumer/dept/get/1 // http://localhost:8082/consumer/dept/list // http://localhsot:8082/consumer/dept/add?dname=廣告部
1.2 Ribbon 負載均衡
// 新建microservicecloud-provider-dept-8002
// 新建microservicecloud-provider-dept-8003
// 新建8002/8003數據庫
// 修改8002/8003各自YML
1.3 Ribbon 核心組件IRule
- 根據特定算法,從服務列表中選取一個要訪問的服務;
- RoundRobinRule:輪詢
- RandomRule:隨機
- AvailabilityFilteringRule: 會先過濾掉由於多次訪問故障而處於斷路器跳閘狀態的服務,以及並發的連接數量
超過閾值的服務,然後對剩余的服務列表按照輪詢策略進行訪問; - WeightedResponseTimeRule: 根據平均響應時間計算所有服務的權重,響應時間越快,服務權重越大,被選中的機率越高;
- RetryRule: 先按照RoundRobinRule的策略獲取服務,如果獲取服務失敗,則在指定時間內會進行重試,獲取可用的服務;
- BestAvailableRule: 會先過濾掉由於多次訪問故障而處於斷路器跳閘狀態的服務,然後選擇一個並發量最小的服務;
- ZoneAvoidanceRule: 默認規則,復合判斷server所在區域的性能和server的可用性選擇服務器;
// 修改 microservicecloud-consumer-dept-80 // ConfigBean @Configuration public class ConfigBean { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } @Bean public IRule myRule() { return new RoundRobinRule(); // 顯式的指定使用輪詢算法 } }
1.4 自定義Ribbon的負載均衡策略
// 修改主啟動類
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name="MICROSERVICECLOUD-DEPT", configuration=MySelfRule.class) // 自定義Ribbon配置類
public class DeptConsumer80_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_App.class, args);
}
}
// com.noodles.myrule
// 自定義Robbin規則類
@Configuration
public class MySelfRule{
@Bean
public IRule myRule(){
return new RandomRule(); //自定義均衡策略
}
}
2. Feign 負載均衡
- Feign 是一個聲明式WebService客戶端:
- 使用方法:定義一個接口,然後在上面添加註解;
2.1 創建microservicecloud-consumer-dept-feign
// 參考 microservicecloud-consumer-dept-80
// pom.xml
<!-- Feign相關 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
// 修改 microservicecloud-api 工程
// pom.xml
<!-- Feign相關 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
// 新建DeptClientService接口,並新增註解@FeignClient
@FeignClient(value="MICROSERVICECLOUD-DEPT")
public interface DeptClientService {
@RequestMapping(value="/dept/get/{id}", method= RequestMethod.GET)
public Dept get(@PathVariable("id") long id);
@RequestMapping(value="/dept/list", method= RequestMethod.GET)
public List<Dept> list();
@RequestMapping(value="/dept/add", method= RequestMethod.POST)
public boolean add(Dept dept);
}
// mvn clean
// mvn install
// microservice-consumer-dept-feign 工程修改Controller
@RestController
public class DeptController_Consumer {
@Autowired
private DeptClientService service;
@RequestMapping(value="/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id) {
return this.service.get(id);
}
@RequestMapping(value="/consumer/dept/list")
public List<Dept> list(){
return this.service.list();
}
@RequestMapping(value="/consumer/dept/add")
public Object add(Dept dept) {
return this.service.add(dept);
}
}
// 修改主啟動類
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.noodles.springcloud"})
@ComponentScan("com.noodles.springcloud")
public class DeptConsumer80_Feign_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_Feign_App.class, args);
}
}
參考資料:
- SpringCloud 教程
SpringCloud 進階之Ribbon和Feign