版权声明:本文为原创文章,转载请注明出处。https://blog.csdn.net/weixin_40790006/article/details/82348821
继Spring Cloud (三) 实现
在Ribbon 服务下操作:
一。代码实现
1.新建Configuration类
使用随机策略
2.在启动类使用注解: @RibbonClient并引入configuration
3.修改controller文件:
3.1添加Log:
Logger LOGGER = LoggerFactory.getLogger(SayController.class);
3.2添加注解:
@Autowired
LoadBalancerClient loadBalancerClient;
3.3添加方法,打印端口号
4.启动服务:
结果为:随机
二。配置文件实现
在YML文件里添加:
spring-cloud-provider: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
参考:Spring Cloud 官方文档等。https://springcloud.cc/spring-cloud-dalston.html#_customizing_the_ribbon_client