SpringCloud(第 008 篇)電影微服務(wù)顽染,使用 application.yml 配置文件配置 Ribbon 在客戶端進(jìn)行負(fù)載均衡調(diào)度算法
一、大致介紹
1轰绵、通過(guò) application.yml 配置來(lái)設(shè)置 Ribbon 客戶端進(jìn)行負(fù)載均衡的調(diào)度算法粉寞;
2、通過(guò) restTemplate.getForObject左腔、loadBalancerClient.choose 兩種代碼調(diào)用方式來(lái)測(cè)試客戶端負(fù)載均衡算法唧垦;
二、實(shí)現(xiàn)步驟
2.1 添加 maven 引用包
<?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">
<modelVersion>4.0.0</modelVersion>
<artifactId>springms-consumer-movie-ribbon-properties</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.springms.cloud</groupId>
<artifactId>springms-spring-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- web模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 客戶端發(fā)現(xiàn)模塊 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 監(jiān)控和管理生產(chǎn)環(huán)境的模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
2.2 添加應(yīng)用配置文件(springms-consumer-movie-ribbon-properties\src\main\resources\application.yml)
spring:
application:
name: springms-consumer-movie-ribbon-properties
server:
port: 8030
#做負(fù)載均衡的時(shí)候液样,不需要這個(gè)動(dòng)態(tài)配置的地址
#user:
# userServicePath: http://localhost:7900/simple/
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://admin:admin@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
# 配置 Ribbon 負(fù)載均衡調(diào)度規(guī)則
springms-provider-user:
ribbon:
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #輪調(diào)
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #隨機(jī)分配
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
2.3 添加實(shí)體用戶類User(springms-consumer-movie-ribbon-properties\src\main\java\com\springms\cloud\entity\User.java)
package com.springms.cloud.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
2.4 添加Web訪問(wèn)層Controller(springms-consumer-movie-ribbon-properties\src\main\java\com\springms\cloud\controller\MovieRibbonPropertiesController.java)
package com.springms.cloud.controller;
import com.springms.cloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class MovieRibbonPropertiesController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
// http://localhost:7900/simple/
// VIP virtual IP
// HAProxy Heartbeat
ServiceInstance serviceInstance = this.loadBalancerClient.choose("springms-provider-user");
System.out.println(">>>>>" + " " + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());
return this.restTemplate.getForObject("http://springms-provider-user/simple/" + id, User.class);
}
@GetMapping("/choose")
public String test() {
ServiceInstance serviceInstance = this.loadBalancerClient.choose("springms-provider-user");
System.out.println("00000" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());
ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("springms-provider-user2");
System.out.println("222222222222222222" + ":" + serviceInstance2.getServiceId() + ":" + serviceInstance2.getHost() + ":" + serviceInstance2.getPort());
return "choose successful";
}
}
2.5 添加電影微服務(wù)啟動(dòng)類(springms-consumer-movie-ribbon-properties\src\main\java\com\springms\cloud\MsConsumerMovieRibbonPropertiesApplication.java)
package com.springms.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* 電影微服務(wù)业崖,使用 application.yml 配置文件配置 Ribbon 在客戶端進(jìn)行負(fù)載均衡調(diào)度算法。
*
* LoadBalanced:該負(fù)載均衡注解蓄愁,已經(jīng)整合了 Ribbon双炕;
*
* Ribbon 的默認(rèn)負(fù)載均衡的算法為:輪詢;
*
* 配置文件優(yōu)先級(jí)最高撮抓,Java代碼設(shè)置的配置其次妇斤,默認(rèn)的配置優(yōu)先級(jí)最低;
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017/9/17
*
*/
@SpringBootApplication
@EnableEurekaClient
public class MsConsumerMovieRibbonPropertiesApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MsConsumerMovieRibbonPropertiesApplication.class, args);
System.out.println("【【【【【【 電影微服務(wù)-Properties定制Ribbon 】】】】】】已啟動(dòng).");
}
}
三丹拯、測(cè)試
/****************************************************************************************
一站超、電影微服務(wù),使用 application.yml 配置文件配置 Ribbon 在客戶端進(jìn)行負(fù)載均衡調(diào)度算法(測(cè)試輪詢分配服務(wù)器地址):
1乖酬、application.yml配置輪詢算法:springms-provider-user.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule死相;
2、啟動(dòng) springms-provider-user 模塊服務(wù)咬像,啟動(dòng)3個(gè)端口(7900算撮、7899、7898)县昂;
3肮柜、啟動(dòng) springms-provider-user2 模塊服務(wù),啟動(dòng)2個(gè)端口(7997倒彰、7996)(直接將用戶微服務(wù) spring.application.name 改了個(gè)名字為 springms-provider-user2 再啟動(dòng)而已)审洞;
4、啟動(dòng) springms-consumer-movie-ribbon-properties 模塊服務(wù)待讳,啟動(dòng)1個(gè)端口芒澜;
5仰剿、在瀏覽器輸入地址http://localhost:8030/choose,然后看看 springms-provider-user痴晦、springms-provider-user2 的各個(gè)對(duì)應(yīng)的端口的服務(wù)打印的信息是否均勻南吮,正常情況下應(yīng)該是輪詢分配打印的;
總結(jié):springms-provider-user(之所以輪詢是因?yàn)榕渲梦募捎?RoundRobinRule 輪詢調(diào)度算法)阅酪、springms-provider-user2(之所以輪詢是因?yàn)闆](méi)有任何配置旨袒,默認(rèn)調(diào)度算法就是輪詢算法)
****************************************************************************************/
/****************************************************************************************
二汁针、電影微服務(wù)术辐,使用 application.yml 配置文件配置 Ribbon 在客戶端進(jìn)行負(fù)載均衡調(diào)度算法(測(cè)試隨機(jī)分配服務(wù)器地址):
1、application.yml配置隨機(jī)算法:springms-provider-user.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule施无;
2辉词、啟動(dòng) springms-provider-user 模塊服務(wù),啟動(dòng)3個(gè)端口(7900猾骡、7899瑞躺、7898);
3兴想、啟動(dòng) springms-provider-user2 模塊服務(wù)幢哨,啟動(dòng)2個(gè)端口(7997、7996)(直接將用戶微服務(wù) spring.application.name 改了個(gè)名字為 springms-provider-user2 再啟動(dòng)而已)嫂便;
4捞镰、啟動(dòng) springms-consumer-movie-ribbon-properties 模塊服務(wù),啟動(dòng)1個(gè)端口毙替;
5岸售、在瀏覽器輸入地址http://localhost:8030/choose,然后看看 springms-provider-user厂画、springms-provider-user2 的各個(gè)對(duì)應(yīng)的端口的服務(wù)打印的信息是否均勻凸丸,正常情況下應(yīng)該是輪詢分配打印的;
總結(jié):springms-provider-user(之所以隨機(jī)是因?yàn)榕渲梦募捎?RandomRule 隨機(jī)調(diào)度算法)袱院、springms-provider-user2(之所以輪詢是因?yàn)闆](méi)有任何配置屎慢,默認(rèn)調(diào)度算法就是輪詢算法)
****************************************************************************************/
四、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
歡迎關(guān)注忽洛,您的肯定是對(duì)我最大的支持!!!
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者