SpingCloud 2020微服务教程【22】Ribbion负载规则替换

视频链接: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值