文件目录结构很重要,特别注意的是rule文件要放在主启动类上一级位置,才能够扫描。
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-netflix-eureka-client
因为eureka的依赖已经整合了ribbon的依赖,所以不用额外引入新的东西。
server: port: 80 spring: application: name: cloud-book-consumer eureka: client: register-with-eureka: false fetch-registry: true service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
@SpringBootApplication @EnableEurekaClient @RibbonClient(name = "CLOUD-BOOK-SERVICE", configuration = LoadBalanceRule.class) //更换轮询算法 public class RestTemplateMain80 { public static void main(String[] args) { SpringApplication.run(RestTemplateMain80.class,args); } }
在图示文件目录下新建LoadBalanceRule.class,用于更换负载均衡算法。
@Configuration public class LoadBalanceRule { @Bean public IRule iRule() { // 定义为随机 return new RandomRule(); } }
开启restTemplate负载均衡
在config文件夹下创建LoadBalanceConfig.class
@Configuration public class LoadBalanceConfig { @Bean @LoadBalanced //开启负载均衡 public RestTemplate getReatTemplate(){ return new RestTemplate(); } }
新建BookController.class
写业务代码
@RestController @Slf4j public class BookController { @Resource private RestTemplate restTemplate; public static final String PAYMENT_URL = "http://CLOUD-BOOK-SERVICE"; @GetMapping(value = "restTemplate/book/getAllBooks")//只要json数据时 public CommonResult getAllBooks(){ return restTemplate.getForObject(PAYMENT_URL+"/book/getAllBooks",CommonResult.class); } @GetMapping("restTemplate/book/getAllBooks2") //需要知道更多数据时,使用getForEntity方法 public CommonResult getAllBooks2(){ ResponseEntityresultResponseEntit = restTemplate.getForEntity(PAYMENT_URL+"/book/getAllBooks",CommonResult.class); if (resultResponseEntit.getStatusCode().is2xxSuccessful()){ log.info(resultResponseEntit.getStatusCode()+"\t"+resultResponseEntit.getHeaders()); return resultResponseEntit.getBody(); }else { return new CommonResult<>(444,"操作失败"); } } @GetMapping(value = "restTemplate/book/index") public String index() { return restTemplate.getForObject(PAYMENT_URL+"/book/index",String.class); } }
使用restTemplate+Ribboin实现服务调用和负载均衡完成。
public interface LoadBalancer { ServiceInstance instances(ListserviceInstances); }
@Component public class MyLB implements LoadBalancer { private AtomicInteger atomicInteger = new AtomicInteger(0); //求第几次访问 自旋锁思想 public final int getAndIncrement(){ int current; int next; do { current = this.atomicInteger.get(); next = current >=2147483647 ? 0 : current+1; }while(!this.atomicInteger.compareAndSet(current,next)); System.out.println("***第几次访问next->"+next); return next; } //负载均衡算法,实现roundRobin算法 @Override public ServiceInstance instances(ListserviceInstances) { int index = getAndIncrement() % serviceInstances.size(); return serviceInstances.get(index); } }
@RestController @Slf4j public class BookController { @Resource private RestTemplate restTemplate; @Resource private LoadBalancer loadBalancer; @Resource private DiscoveryClient discoveryClient; public static final String PAYMENT_URL = "http://CLOUD-BOOK-SERVICE"; @GetMapping(value = "restTemplate/book/getAllBooks")//只要json数据时 public CommonResult getAllBooks(){ Listinstances = discoveryClient.getInstances("CLOUD-BOOK-SERVICE"); if (instances == null || instances.size() <= 0){ return null; } ServiceInstance serviceInstance = loadBalancer.instances(instances); URI uri = serviceInstance.getUri(); return restTemplate.getForObject(uri+"/book/getAllBooks",CommonResult.class); } @GetMapping(value = "restTemplate/book/index") public String index() { List instances = discoveryClient.getInstances("CLOUD-BOOK-SERVICE"); if (instances == null || instances.size() <= 0){ return null; } ServiceInstance serviceInstance = loadBalancer.instances(instances); URI uri = serviceInstance.getUri(); return restTemplate.getForObject(uri+"/book/index",String.class); } }
@RibbonClient(name = “CLOUD-BOOK-SERVICE”, configuration = LoadBalanceRule.class)
@LoadBalanced
手写Ribbon算法并使用完成
以上就是Ribbon和RestTemplate实现服务调用与负载均衡的详细内容,更多关于Ribbon和RestTemplate服务与负载的资料请关注其它相关文章!