码迷,mamicode.com
首页 > 其他好文 > 详细

负载均衡中选择微服务工程的方式

时间:2020-05-27 20:57:11      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:pass   img   更换   服务提供者   netflix   默认   must   dom   方式   

默认是轮询方式,一共提供了8种,可以使用其他的方式

1、RoundRobinRule(轮询算法)

2、RandomRule(随机算法)

3、AvailabilityFilteringRule():会先过滤由于多次访问故障而处于断路器跳闸状态的服务,还有并发的连接数量超过阈值的服务,然后对剩余的服务列表按照轮询策略进行访问

4、WeightedResponseTimeRule():根据平均响应的时间计算所有服务的权重,响应时间越快服务权重越大被选中的概率越高,刚启动时如果统计信息不足,则使用5、RoundRobinRule策略,等统计信息足够会切换到WeightedResponseTimeRule

6、RetryRule():先按照RoundRobinRule的策略获取服务,如果获取失败则在制定时间内进行重试,获取可用的服务。

7、BestAviableRule():会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务

8、ZoneAvoidanceRule():默认规则,符合判断server所在区域的性能和server的可用性选择服务器

一、使用系统提供的方式查找

1、消费者工程(90工程),ConfigBeans类修改

技术图片

2、添加方法

技术图片

方法代码:

@Bean
public IRule myRule(){
return new RandomRule();//随机查找
}

重启消费者工程,刷新地址测试,每次刷新获取的库名是随机的

访问地址:http://127.0.0.1:90/consumer/dept/get/1

二、自定义算法

步骤:创建配置类、加载配置类

1、在消费者工程(90工程)下新建存放配置类的包

注意:该包与springcloud包同级,不能在启动类同级或之下

技术图片

2、在该包下创建Rule_Utils.java类和MySelfRule.java类

技术图片

Rule_Utils.java类的代码

package com.boxue.myrule;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;

public class Rule_Utils 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

}

}

MySelfRule.java类的代码

@Configuration
public class MySelfRule {
@Bean
public IRule myRule()
{
//如果返回的是默认提供的,就会使用默认提供的算法
//return new RandomRule();
// Ribbon默认是轮询,我自定义为随机
//return new RoundRobinRule();
// Ribbon默认是轮询,我自定义为随机
// 我自定义为每台机器5次
return new Rule_Utils();
}

}

3、启动类

技术图片

代码如下:

package com.boxue.springcloud;


import com.boxue.myrule.MySelfRule;
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;

@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "MICROSERVICECLOUD-DEPT",configuration = MySelfRule.class)
public class DeptConsumer90_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer90_App.class,args);
}
}

4、测试

依次启动注册中心,微服务提供者类和消费者类

访问地址:http://127.0.0.1:90/consumer/dept/get/1

每刷新5次后更换微服务工程

 

负载均衡中选择微服务工程的方式

标签:pass   img   更换   服务提供者   netflix   默认   must   dom   方式   

原文地址:https://www.cnblogs.com/lnboxue/p/12975912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!