private int incrementAndGetModulo(int modulo) {
int current;
int next;
do {
current = this.nextServerCyclicCounter.get();
next = (current + 1) % modulo;
} while(!this.nextServerCyclicCounter.compareAndSet(current, next));
return next;
}
//使用自旋锁完成下标的求解
private int incrementAndGetModulo(int modulo) {
for(;;){
int current=nextServerCyclicCounter.get();
int next = (current+1)%module;
if(nextServerCyclicCounter.compareAndSet(current,next))
return next;
}
}
2.1 使用ribbon步骤
1.自定义一个IRule
@Configuration
public class MySelfRule {//不要放在主启动类所在的包下
@Bean
public IRule myRule(){
return new RandomRule();//替换默认的轮询规则 自带七种
}
}
2.指明是使用自己的
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
3.配置给RestTemplate使用
@Configuration
public class ApplicationContextconfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
线程属性
  1、Number of Threads(users):线程数,相当于模拟的用户数量;
  2、Ramp-up Period(in seconds):达到指定线程需要的时间,例如线程
  数为100,时间设定为10s
  ,那么就是10s加载100个线程,每秒启动的线程数=100/10=10;
  3、Loop Count:如果填具体的数值,就是循环对应的次数;如果选择“Fo
  rever”,则一直执行下去,直到手动停止;
  4、Delay Thread creation until
  needed:延迟线程创建,直到需要才创建。
5.两个配置register-with-eureka fetch-registry
1.使用feign时为什么让register-with-eureka为false有待研究
eureka:
client:
register-with-eureka: false
fetch-registry: false
用Feign搭建服务消费者的时候,考虑消费者不需要再提供服务给其他服务,
所以不需要注册到注册中心(eureka)中。结果把registerWithEureka和fetc
hRegistry都关掉了,服务调用时报错:com.netflix.client.ClientExceptio
n: Load balancer does not have available server for client: XXXXXX。
看报错信息,负载均衡器没有找到可用的服务,Feign默认使用ribbon做负载
均衡。不想注册,将registerWithEureka关掉就行了。启动类有eureka注解的
情况下(即是一个eureka客户端),fetchRegistry打开才能从eureka拉取服
务列表,ribbon才能做负载均衡。
2.依赖说明
因为feign底层是使用了ribbon作为负载均衡的客户端,而ribbon的负载均衡
也是依赖于eureka 获得各个服务的地址,所以要引入eureka-client。