springcloud——eureka中ribbon采用随机负载均衡的方法(默认为轮巡)

eureka依赖中集成了ribbon依赖所以可以不额外添加ribbon依赖:

在这里插入图片描述

在ComentScan扫描不到的包中创建新的负载均衡配置方式:

在这里插入图片描述

package com.dc;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.env.RandomValuePropertySource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author dc
 * @date 2020/7/27 - 15:55
 */
@Configuration      //注明该类为配置类
public class MySelfRule {

    @Bean   //将返回值注入spring容器
    public IRule getMyRule() {
        return new RandomRule();    //定义为随机
    }


}

在主启动类中添加注解:

在这里插入图片描述

package com.springcloud;

import com.dc.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     //表示开启eureka客户端
@RibbonClient(name = "payment-provider", configuration = MySelfRule.class)  //指定负载均衡方式
public class PaymentConsumerMain81 {

    public static void main(String[] args) {
        SpringApplication.run(PaymentConsumerMain81.class, args);
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_43925059/article/details/107614460