前一篇讲了服务注册和发现 ,没有看过的朋友先看上一篇,这篇是在上一篇基础上展开。
Ribbon简介 Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
Ribbon是负载均衡客户端,可以很好的控制HTTP和TCP客户端的行为。 Feign已经集成了Ribbon。
Spring Cloud Netflix默认为ribbon(BeanType beanName:ClassName)提供以下bean:
IClientConfig ribbonClientConfig: DefaultClientConfigImpl
IRule ribbonRule: ZoneAvoidanceRule
IPing ribbonPing: NoOpPing
ServerList<Server> ribbonServerList: ConfigurationBasedServerList
ServerListFilter<Server> ribbonServerListFilter: ZonePreferenceServerListFilter
ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
准备工作 这篇文章基于上一篇文章项目,启动eureka-server项目;启动eureka-client项目,端口为8040; 将eureka-client的配置文件端口改为8041,并启动,同一个项目修改端口号,启动多个实例,只需要在IDEA中勾选Allow parallel run:
此时你会发现注册服务中注册了两个eureka-client实例,相当于起了2个节点的集群。 访问http://localhost:9090 :
新建一个服务消费者 使用Spring Initializr新建一个项目,取名为ribbon-service, 在Spring Cloud Discovery中勾选Eureka Discovery Client,在Spring Cloud Routing中勾选Ribbon,在Web中勾选Spring Web:
    创建成功后,项目pom.xml如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.1.9.RELEASE</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>     <groupId>com.noodles.mars</groupId>     <artifactId>ribbon-service</artifactId>     <version>0.0.1-SNAPSHOT</version>     <name>ribbon-service</name>     <description>Ribbon Service</description>     <properties>         <java.version>1.8</java.version>         <spring-cloud.version>Greenwich.SR3</spring-cloud.version>     </properties>     <dependencies>         <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.cloud</groupId>             <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>     </dependencies>     <dependencyManagement>         <dependencies>             <dependency>                 <groupId>org.springframework.cloud</groupId>                 <artifactId>spring-cloud-dependencies</artifactId>                 <version>${spring-cloud.version}</version>                 <type>pom</type>                 <scope>import</scope>             </dependency>         </dependencies>     </dependencyManagement>     <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> </project> 
ribbon-service配置服务中心地址、应用名、端口,配置文件内容:
1 2 3 4 5 6 7 8 9 10 11 server:   port: 8050 spring:   application:     name: ribbon-service eureka:   client:     service-url:        defaultZone: http://localhost:9090/eureka/ 
在项目启动类上注解@EnableDiscoveryClient, 开启向服务中心注册;定义一个 RestTemplate Bean, 并注解@LoadBalanced开启负载均衡功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @EnableEurekaClient @SpringBootApplication public class RibbonServiceApplication {     public static void main(String[] args) {         SpringApplication.run(RibbonServiceApplication.class, args);     }     @Bean     @LoadBalanced     public RestTemplate restTemplate() {         return new RestTemplate();     } } 
写一个controller, 通过之前定义的RestTemplate来消费eureka-client服务的/hello接口,url中使用应用名,ribbon会根据应用名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @RestController public class HelloController {     private final RestTemplate restTemplate;     @Autowired     public HelloController(RestTemplate restTemplate) {         this.restTemplate = restTemplate;     }     @GetMapping("/hello")     public String hello(@RequestParam("name") String name) {         return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class);     } } 
在浏览器上多次访问 http://localhost:8050/hello?name=Mars  :
1 2 Hello, My name is Mars, I'm from port: 8040 Hello, My name is Mars, I'm from port: 8041 
这说明负载均衡器已经工作了。
点击进入源码仓库