现在我们通过插件的方式添加新的一种策略。
package com.zhuyang.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.IPing; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.PingUrl; import com.netflix.loadbalancer.RandomRule; /** * * Here, we override the IPing and IRule used by the default load balancer. The * default IPing is a NoOpPing (which doesn’t actually ping server instances, * instead always reporting that they’re stable), and the default IRule is a * ZoneAvoidanceRule (which avoids the Amazon EC2 zone that has the most * malfunctioning servers, and might thus be a bit difficult to try out in our * local environment). * */
@Configuration public class RibbonConfiguration { @Bean public IClientConfig ribbonPing(){ IClientConfig config = new DefaultClientConfigImpl(); return config; } @Bean public IRule ribbonRule(IClientConfig config) { return new MyRule(); } }
MyRule.java是自己定义的个算法,大概算法是随机选中能被2整除的server
这样我们自己写的策略算法就可以正常工作了。 所有的请求都只会到8003或者8000折两台server去。