SpringCloud-整合ribbonjava
1 在customer(服务消费方添加)算法
<!-- Ribbon相关 (ribbon须要和eureka配合使用) --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
2 在yml文件中添加配置文件spring
eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka9001.com:9001/eureka/,http://eureka9002.com:9002/eureka/,http://eureka9003.com:9003/eureka/
3 在RestTemplate 实例化中添加开启开启客户端负载均衡的注解服务器
@Bean @LoadBalanced //开启负载均衡的工具 public RestTemplate getRestTemlate() { return new RestTemplate(); }
4 在主启动类中添加开启eureka客户端注解app
@SpringBootApplication @EnableEurekaClient public class Customer7001 { public static void main(String[] args) { SpringApplication.run(Customer7001.class, args); } }
5 在配置文件中访问改成eureka微服务名访问负载均衡
private static final String REST_URL_PREFIX = "http://SPRINGCLOUD04-PRODECT-8001";
/** * 自指定负载均衡算法 * @author SHF * @version 建立时间:2018年11月29日 下午6:04:28 * @return */ @Bean public IRule myRule() { return new RoundRobinRule();//使用随机数进行负载均衡 }
自定义ribbon的使用()dom
1 自定义负载均衡规则ide
package com.shi.rule; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractLoadBalancerRule; import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.Server; /** * 自定义咱们的LB算法规则 * 注意:不能把该类放在@ComponentScan注解包含的全部包及子包下面 * MySelfRule必须不能是spring所扫描的包, * @author SHF * @version 建立时间:2018年11月30日 上午11:32:23 */ public class MyselfRule extends AbstractLoadBalancerRule { private static Logger log = LoggerFactory.getLogger(MyselfRule.class); /* * 需求:让每一个服务被调用5次以后再切换到下一台服务器 */ private int total = 0; //总共被调用的次数,目前要求每台被调用5次 private int currentIndex = 0; //当前提供服务的机器号 public Server choose(ILoadBalancer lb, Object key) { if (lb == null) { return null; } Server server = null; while (server == null) { if (Thread.interrupted()) { return null; } List<Server> upList = lb.getReachableServers(); List<Server> allList = lb.getAllServers();//获取全部的服务器 int serverCount = allList.size(); if (serverCount == 0) { return null; } //int index = chooseRandomInt(serverCount); if(total < 5) { server = upList.get(currentIndex); total++; }else { total = 0; currentIndex++; if(currentIndex >= upList.size()) { currentIndex = 0; } } if (server == null) { /* * The only time this should happen is if the server list were * somehow trimmed. This is a transient condition. Retry after * yielding. */ Thread.yield(); continue; } if (server.isAlive()) { return (server); } // Shouldn't actually happen.. but must be transient or a bug. server = null; Thread.yield(); } return server; } @Override public Server choose(Object key) { return choose(getLoadBalancer(), key); } @Override public void initWithNiwsConfig(IClientConfig clientConfig) { } }
2 配置rule的对象微服务
/** * 自指定负载均衡算法 * @author SHF * @version 建立时间:2018年11月29日 下午6:04:28 * @return */ @Bean public IRule myselfRule() { //return new RoundRobinRule();//使用随机数进行负载均衡 return new com.shi.rule.MyselfRule();//使用自定义的负载均衡算法 }
3 在启动类上面加上注解工具
@RibbonClient(name="SPRINGCLOUD04-PRODECT-8001",configuration=com.shi.rule.MyselfRule.class)