(二)Ribbon(負載均衡的客戶端)+Rest

前面講了服務的註冊與發現,微服務項目會把項目的各個業務需求劃分紅幾個模塊來單獨提供服務,各服務間的調用都是採用Http Restful來實現,可是在SpringClound中服務間的調用有兩種方式:一種是ribbon+ restTemplate;另外一種是feign;html

 Ribbon:在SpringClound中是做爲一個負載均衡的客戶端,控制訪問入口,定製訪問策略等功能; Feign組件同時也是集成了ribbon的java

 

打開前面準備的服務client

 

在Idea裏,新建項目,選擇Spring initializer.web

勾選組件spring

 

下面的pom瀏覽器

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
			<version>1.4.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

  

配置properties文件參數;服務器

#服務端口
server.port=8885
#註冊服務中心地址
eureka.client.service-url.defaultZone=http://localhost:8882/eureka/
#註冊服務端name
spring.application.name=service-ribbon
#調用鏈接時間
eureka.client.eureka-server-read-timeout-seconds=6000
#調用鏈接時間
eureka.client.eureka-server-connect-timeout-seconds=6000
hystrix.metrics.polling-interval-ms=6000

  

在啓動類上添加註解@EnableDiscoveryClient併發

@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloundRibbonExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloundRibbonExampleApplication.class, args);
    }

    /**
     * ioc注入一個bean: restTemplate;並經過@LoadBalanced註解代表這個restRemplate開啓負載均衡的功能
     *
     * @return
     */
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

  

建立conroller、serviceapp

@RestController
public class HelloController {

    @Autowired
    IHelloService helloService;

    @RequestMapping(value = "/index")
    public String index() {
        return helloService.index();
    }

}

  

@Service
public class HelloServiceImpl implements IHelloService {

    @Autowired
    RestTemplate restTemplate;

    @Override
    public String index() {
        return restTemplate.getForObject("http://SERVICE-HELLO/index",String.class);
    }
}

  

啓動項目,而後再看服務中心,已經註冊成功負載均衡

 

 

回到瀏覽器,輸入http://localhost:8885/indexdom

刷新:

 

Ribbon自己提供了下面幾種負載均衡策略:

核心組件IRule

  • RoundRobinRule: 輪詢策略,Ribbon以輪詢的方式選擇服務器,這個是默認值。因此示例中所啓動的兩個服務會被循環訪問;
  • RandomRule: 隨機選擇,也就是說Ribbon會隨機從服務器列表中選擇一個進行訪問;
  • BestAvailableRule: 最大可用策略,即先過濾出故障服務器後,選擇一個當前併發請求數最小的;
  • WeightedResponseTimeRule: 帶有加權的輪詢策略,對各個服務器響應時間進行加權處理,而後在採用輪詢的方式來獲取相應的服務器;
  • AvailabilityFilteringRule: 可用過濾策略,先過濾出故障的或併發請求大於閾值一部分服務實例,而後再以線性輪詢的方式從過濾後的實例清單中選出一個;
  • ZoneAvoidanceRule: 區域感知策略,先使用主過濾條件(區域負載器,選擇最優區域)對全部實例過濾並返回過濾後的實例清單,依次使用次過濾條件列表中的過濾條件對主過濾條件的結果進行過濾,判斷最小過濾數(默認1)和最小過濾百分比(默認0),最後對知足條件的服務器則使用RoundRobinRule(輪詢方式)選擇一個服務器實例。
@Configuration
public class RibbonConfiguration {

@Bean
public IRule ribbonRule() {
return new RandomRule();
}
}
咱們能夠經過繼承ClientConfigEnabledRoundRobinRule,來實現本身負載均衡策略。
相關文章
相關標籤/搜索