①修改microservicecloud-consumer-dept-80,主启动类添加@RibbonClient注释。
在启动该微服务的时候就能去加载我们的自定义Ribbon配置类(不用ribbon出厂默认的负载均衡方式,用自己定义的ribbon负载均衡方式),从而使配置生效,形如:
@RibbonClient(name="MICROSERVICECLOUD-DEPT",configuration=MySelfRule.class)
name="MICROSERVICECLOUD-DEPT",表示针对eureka的微服务MICROSERVICECLOUD-DEPT使用。
configuration=MySelfRule.class,自定义一个MySelfRule类,里面写我们自定义的轮询规则和算法。
DeptConsumer80_App.java的全部内容是:
package com.lss.springcloud;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import com.lss.myrule.MySelfRule;
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name="MICROSERVICECLOUD-DEPT",configuration=MySelfRule.class)
public class DeptConsumer80_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_App.class, args);
}
}
②创建MySelfRule类。
官方文档明确给出了警告:
这个自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,也就是说我们达不到特殊化定制的目的了。
@SpringBootApplication注解里面包含@ComponentScan注解。
又@SpringBootApplication注解是包com.lss.springcloud下的主启动类DeptConsumer80_App.java下的注解,说明该包com.lss.springcloud和该包下的子包:com.lss.springcloud.cfgbeans、com.lss.springcloud.controller下不能定义MySelfRule类。
所以需要新建包,新建package:com.lss.myrule。
③新建自定义Robbin规则类MySelfRule。并修改内容。
MySelfRule.java的内容是:
package com.lss.myrule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
@Configuration
public class MySelfRule {
@Bean
public IRule myRule()
{
return new RandomRule();//Ribbon默认是轮询,我自定义为随机
}
}
④问题:依旧轮询策略,但是加上新需求,每个服务器要求被调用5次。也即以前是每台机器一次,现在是每台机器5次。借鉴源码。
找到GitHub上对应的源码。
地址栏输入:
解析源码:
https://github.com/Netflix/ribbon/blob/master/ribbon-loadbalancer/src/main/java/com/netflix/loadbalancer/RandomRule.java
GitHub上的源码内容是:
/*
*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.loadbalancer;
import com.netflix.client.config.IClientConfig;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
/**
* A loadbalacing strategy that randomly distributes traffic amongst existing
* servers.
*
* @author stonse
*
*/
public class RandomRule extends AbstractLoadBalancerRule {
/**
* Randomly choose from all living servers
*/
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
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 = chooseRandomInt(serverCount);
server = upList.get(index);
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;
}
protected int chooseRandomInt(int serverCount) {
return ThreadLocalRandom.current().nextInt(serverCount);
}
@Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
}
}
我们要用到上面的源码,需要新建一个类。
RandomRule_ZY.java需要增加的内容是:
//总共被调用的次数,目前要求每台被调用5次,当total为5,指针currentIndex才能往下走,即+1。
private int total = 0;
//当currentIndex达到上限,即如果有n台机器,currentIndex=n,total=5,则需要
//将currentIndex清零。
private int currentIndex = 0;//当前提供服务的机器号
if(total < 5)
{
server = upList.get(currentIndex);
total++;
}else {
total = 0;
currentIndex++;
if(currentIndex >= upList.size())
{
currentIndex = 0;
}
}
RandomRule_ZY.java全部内容是:
package com.lss.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 {
//总共被调用的次数,目前要求每台被调用5次,当total为5,指针currentIndex才能往下走,即+1。
private int total = 0;
//当currentIndex达到上限,即如果有n台机器,currentIndex=n,total=5,则需要
//将currentIndex清零。
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;
}
if(total < 5)
{
server = upList.get(currentIndex);
total++;
}else {
total = 0;
currentIndex++;
if(currentIndex >= upList.size())
{
currentIndex = 0;
}
}
// int index = chooseRandomInt(serverCount);
// server = upList.get(index);
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;
}
// protected int chooseRandomInt(int serverCount) {
// return ThreadLocalRandom.current().nextInt(serverCount);
// }
@Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
}
@Override
public void initWithNiwsConfig(IClientConfig arg0) {
}
}
⑤MySelfRule.java的全部内容是:
package com.lss.myrule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
@Configuration
public class MySelfRule {
@Bean
public IRule myRule()
{
//return new RandomRule();//Ribbon默认是轮询,我自定义为随机
//return new RoundRobinRule();
return new RandomRule_ZY();//我自定义为每个机器被访问5次
}
}
测试,利用虚拟机。
http://192.168.10.115:83/consumer/dept/list
依旧轮询策略,但是加上新需求,每个服务器要求被调用5次。也即以前是每台机器一次,现在是每台机器5次。