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);
}
}