视频链接:2020最新版SpringCloud框架开发教程-周阳
文章源码:https://github.com/geyiwei-suzhou/cloud2020/
警告
:自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下。否则我们自定义的这个配置类会被所有的Ribbon客户端所共享,达不到特殊化定制的目的了。
cloud-consumer-order80模块
新建类:com.antherd.rule.MyRule
package com.antherd.rule;
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 MyRule {
@Bean
public IRule myRibbonRule() {
return new RandomRule(); // 定义为随机
}
}
修改:com.antherd.springcloud.OrderMain80
package com.antherd.springcloud;
import com.antherd.rule.MyRule;
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
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE", configuration = MyRule.class)
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}
测试
启动:cloud-eureka-server7001,cloud-eureka-server7002,cloud-provider-payment8001,cloud-provider-payment8002,cloud-consumer-order80三个模块
访问:http://localhost/consumer/payment/1