测试中使用到的代码到在这里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
}
}
测试
刷新五次后在轮询下一个服务单元