本文采用Spring cloud本文爲2.1.8RELEASE,version=Greenwich.SR3 java
本文基於前兩篇文章eureka-server和eureka-client的實現。 參考git
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
複製代碼
application.ymlgithub
spring:
application:
name: eureka-ribbon
server:
port: 8901
eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
複製代碼
package spring.cloud.demo.eurekaribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
}
}
複製代碼
@EnableDiscoveryClient啓動eureka服務發現相關配置web
package spring.cloud.demo.eurekaribbon.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/** * @auther: maomao * @DateT: 2019-09-17 */
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
複製代碼
@LoadBalanced:實現負載均衡,默認輪詢。spring
Ribbon自帶的負載規則瀏覽器
- RoundRobinRule:系統默認的規則,經過簡單的輪詢服務列表來選擇服務器,其餘的規則在不少狀況下,仍然使用RoundRobinRule。
- AvailablilityFilteringRule:該各類會忽略如下服務器:
沒法鏈接的服務器:在默認狀況下,若是3次鏈接失敗,該服務器將會被置爲「短路」的狀態,該狀態將持續30秒,若是再次鏈接失敗,「短路」狀態的持續時間將會以幾何級增長。能夠經過修改niws.loadbalance..connerctionFailureCountThreshold屬性來配置鏈接失敗的次數。
併發數太高的服務器:若是鏈接到該服務器的併發數太高,也會被這個規則忽略,能夠經過修改.ribbon.ActiveConnectionLimit屬性來設定最高併發數。服務器
- WeightedResponseTimeRule:爲每一個服務器賦予一個權重值,服務器的響應時間越長,該權重值就越少,這個規則會隨機選擇服務器,這個權重值有能夠能會決定服務器的選擇。
- ZoneAvoidanceRule:該規則以區域、可用服務器爲基礎,進行服務器選擇。使用Zone對服務器進行分類,能夠理解爲機架或者機房。
- BestAvailiableRule:忽略「短路」的服務器,並選擇併發數較低的服務器。
- RandomRule:隨機選擇可用服務器。
- RetryRule:含有重試的選擇邏輯,若是使用RoundRobinRule。
application.yml增長配置:網絡
#RoundRobinRule:系統默認的規則,經過簡單的輪詢服務列表來選擇服務器,其餘的規則在不少狀況下,仍然使用RoundRobinRule
eureka-client: #對應的服務client的name
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
複製代碼
package spring.cloud.demo.eurekaribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
/** * @auther: maomao * @DateT: 2019-09-17 */
@Service
public class EurekaRibbonService {
@Autowired
RestTemplate restTemplate;
public String sayHello() {
String message;
try {
message = restTemplate.getForObject("http://eureka-client/info", String.class);
} catch (RestClientException e) {
message = e.getMessage();
}
return message;
}
}
複製代碼
http://eureka-client/info, 其中eureka-client爲服務提供者對應的spring.application.name併發
package spring.cloud.demo.eurekaribbon.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.cloud.demo.eurekaribbon.service.EurekaRibbonService;
/** * @auther: maomao * @DateT: 2019-09-17 */
@RestController
public class EurekaRibbonConntroller {
@Autowired
private EurekaRibbonService eurekaRibbonService;
@RequestMapping("/syaHello")
public String syaHello() {
String message = eurekaRibbonService.sayHello();
return "ribbon result: " + message;
}
}
複製代碼
前題保證eureka-server和eureka-client已經正常啓動。而後啓動eureka-ribbon服務。 在瀏覽器輸入http://localhost:8901/syaHello,以下圖所示:app
屢次刷新後能夠看到瀏覽器顯示的是結果中端口是變化的。
至此,一個簡單的單點Ribbon服務消費者就搭建完成。
場景:假如在生產環境中,訪問量很大的狀況下,那麼就會產生不少請求阻塞的狀況,而後服務器的內存消耗就會陡增,嚴重狀況下會致使系統的崩潰,也就是常見的雪崩。爲了不這種狀況,熔斷保護機制就迎刃而生。在訪問不通的狀況下,要及時做出響應,而不是等待超時。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
複製代碼
package spring.cloud.demo.eurekaribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
}
}
複製代碼
package spring.cloud.demo.eurekaribbon.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
/** * @auther: maomao * @DateT: 2019-09-17 */
@Service
public class EurekaRibbonService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(
commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000"),
@HystrixProperty(name = "execution.isolation.strategy",value = "THREAD")},
fallbackMethod = "syaHelloFailure")
public String sayHello() {
String message;
try {
message = restTemplate.getForObject("http://eureka-client/info", String.class);
} catch (RestClientException e) {
message = e.getMessage();
}
return message;
}
public String syaHelloFailure() {
System.out.println("error come in ");
String message = "網絡繁忙, 請稍後再試";
return message;
}
}
複製代碼
停掉其中一臺服務,屢次訪問http://localhost:8901/syaHello會出現以下圖狀況,
本文簡單實現了ribbon作爲消費者的搭建過程,並假如了Hystrix熔斷機制。
轉載請註明出處,