简介
LB负载均衡
集中LB(负载均衡)
进程LB(负载均衡)
RestTemplate (远程)调用
Ribbon 的核心组件 IRule
Ribbon 负载均衡的算法如何替换
注意:不要将这个自定义的配置类放在主启动类的包下以及主启动类的包的子包下
- 新建 package 包名为 com.atguigu.myrule
- 在 com.atguigu.myrule 包下新建 MySelfRule 类
package com.atguigu.myrule;
import com.netflix.client.IResponse;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MySelfRule {
@Bean
public IRule myRule() {
return new RandomRule(); // 定义为随机
}
}
- 主启动类添加 @RibbonClient 注解
package com.atguigu.springcloud;
import com.atguigu.myrule.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
// 需要访问的服务的名称 CLOUD-PAYMENT-SERVICE
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE", configuration = MySelfRule.class)
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}