学习springcluod的时候,有一个困惑,为什么RestTemplate上面@LoadBalanced注解,就能实现负载均衡,今天我们一起学习下源码,探索下springCloud底层的秘密:
第一步:在看源码之前我们先自己搭建一个消费者微服务(因为我们这里主要讲解的是springCloud的Ribbon负载均衡,所以注册中心和提供者这里就不再讲解了)
1、引入必要的maven依赖:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon