Ribbon在專案中的使用++單個服務環境的測試+多個服務環境下的測試+使用官方的負載均衡策略+自定義負載均衡策略
測試中使用到的程式碼到在這裡https://download.csdn.net/download/zhou920786312/10853300
客戶端
新增依賴包
<!-- 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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置獲取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/
配置負載均衡
啟動eureka客戶端
controller層遠端服務地址的修改
單個服務環境的測試(8001)
啟動eureka的3個伺服器7001,7002,7003,啟動服務單8001,再啟動客戶端80
訪問獲取到資料
步驟:先去服務中心獲取列表,負載均衡選擇一個服務,訪問服務。
多個服務環境下的測試
原理圖
步驟1
拷貝8001,埠改為8002,8003,資料庫各自指向都修改
測試
啟動3個eureka,啟動3個微服務,啟動測試80
註冊中心有3個服務註冊進來,他們的對方服務名稱都是一樣(microservicecloud-dept)
第一次訪問的資料庫02,重新整理後訪問的資料庫03(負載均衡預設是簡單輪詢)
使用官方的負載均衡策略
官方的策略
程式碼修改
測試
先重新整理下面這個請求,發現是輪詢,在關閉8002服務單元,可以猜到當輪詢到8002會報錯,多次以後,服務輪詢策略會刪除8002的單元服務。
輪詢到8002的情況
過段時間後,輪詢列表中就刪除掉8002單元了。
自定義負載均衡策略
自定義輪詢策略,但是每個服務單元要使用5次後才輪詢給下一個單元。
程式碼
MySelfRule.class
@SpringBootApplication
@EnableEurekaClient
//在啟動該微服務的時候就能去載入我們的自定義Ribbon配置類,針對的服務單元是MICROSERVICECLOUD-DEPT
//注意MySelfRule.class類不能放在啟動主類(DeptConsumer80_App)的包或者子包下。
//DeptConsumer80_App的包是在com.atguigu.springcloud,所以我把MySelfRule.class放在com.atguigu.myrule下
@RibbonClient(name="MICROSERVICECLOUD-DEPT",configuration=MySelfRule.class)
public class DeptConsumer80_App
{
public static void main(String[] args)
{
SpringApplication.run(DeptConsumer80_App.class, args);
}
}
package com.atguigu.myrule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
@Configuration
public class MySelfRule
{
@Bean
public IRule myRule()
{
//return new RandomRule();// Ribbon預設是輪詢,我自定義為 隨機
//return new RoundRobinRule();// Ribbon預設是輪詢,我自定義為隨機
return new RandomRule_ZY();// 我自定義為每臺機器5次
}
}
package com.atguigu.myrule;
import java.util.List;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
public class RandomRule_ZY extends AbstractLoadBalancerRule
{
// total = 0 // 當total==5以後,我們指標才能往下走,
// index = 0 // 當前對外提供服務的伺服器地址,
// total需要重新置為零,但是已經達到過一個5次,我們的index = 1
// 分析:我們5次,但是微服務只有8001 8002 8003 三臺,OK?
//
private int total = 0; // 總共被呼叫的次數,目前要求每臺被呼叫5次
private int currentIndex = 0; // 當前提供服務的機器號
public Server choose(ILoadBalancer lb, Object key)
{
if (lb == null) {
return null;
}
//輪詢的服務
Server server = null;
while (server == null) {
if (Thread.interrupted()) {
return null;
}
//獲取當前存活的服務單元
List<Server> upList = lb.getReachableServers();
//獲取總的服務單元
List<Server> allList = lb.getAllServers();
int serverCount = allList.size();
if (serverCount == 0) {
/*
* No servers. End regardless of pass, because subsequent passes only get more
* restrictive.
*/
return null;
}
// int index = rand.nextInt(serverCount);// java.util.Random().nextInt(3);
// server = upList.get(index);
// private int total = 0; // 總共被呼叫的次數,目前要求每臺被呼叫5次
// private int currentIndex = 0; // 當前提供服務的機器號
//每五次輪詢下一個服務單元
if(total < 5)
{
server = upList.get(currentIndex);
total++;
}else {
total = 0;
currentIndex++;
if(currentIndex >= upList.size())
{
currentIndex = 0;
}
}
if (server == null) {
/*
* The only time this should happen is if the server list were somehow trimmed.
* This is a transient condition. Retry after yielding.
*/
//讓出時間片,等其他執行緒執行完畢在執行
Thread.yield();
continue;
}
if (server.isAlive()) {
return (server);
}
// Shouldn't actually happen.. but must be transient or a bug.
server = null;
Thread.yield();
}
return server;
}
@Override
public Server choose(Object key)
{
return choose(getLoadBalancer(), key);
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig)
{
// TODO Auto-generated method stub
}
}
測試
重新整理五次後在輪詢下一個服務單元