1.概述
2.zookeeper与eureka在CAP理论中的区别
(电商时,应当保证高可用,即最好选用AP)
eureka遵守AP
zookeeper遵守CP
eureka集群,不区分主从节点
zookeeper集群,区分主从节点,主挂掉,其他进行选举
3.负载均衡Ribbon(指的是consumer端)
3.1
3.2常用负载均衡算法有轮询,IP哈希,随机
3.3自定义ribbon类
import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* 自定义负载均衡算法1:
* 不能与主启动类放在同一包下,即不能一起扫描
* 此时主启动类需要添加注解:
* @RibbonClient(name="microservice-cloud-dept",configuration= CustomerIRule01.class)
*/
@Configuration
public class CustomerIRule01 { @Bean
public IRule myRule(){
//return new RandomRule();// Ribbon默认是轮询,我自定义为随机
//return new RoundRobinRule();// Ribbon默认是轮询,我自定义为随机
return new IRuleAlgo(); // 使用自定义的算法
}
}
package com.zy.IRuleSelf; 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 IRuleAlgo 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 void initWithNiwsConfig(IClientConfig iClientConfig) { } @Override
public Server choose(Object o) {
return null;
}
}
3.4负载均衡的另一种方式(feign)
4.服务熔断(指的是provider端)
5.服务降级(降级是在consumer端,当然是先关闭provider端)
6.网关,路由