1、Ribbon是什么?
Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随即连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。
2、Ribbon负载均衡策略
简单轮询负载均衡(RoundRobin)
以轮询的方式依次将请求调度不同的服务器,即每次调度执行i = (i + 1) mod n,并选出第i台服务器。
加权响应时间负载均衡 (WeightedResponseTime)
随机负载均衡 (Random)
随机选择状态为UP的Server
区域感知轮询负载均衡(ZoneAware)
区域感知负载均衡内置电路跳闸逻辑,可被配置基于区域同源关系(Zone Affinity,也就是更倾向于选择发出调用的服务所在的托管区域内,这样可以降低延迟,节省成本)选择目标服务实例。它监控每个区域中运行实例的行为,而且能够实时的快速丢弃一整个区域。这样在面对整个区域故障时,帮我们提升了弹性。
3、建一个服务消费者(springcloud-ribbon)
创建完pom文件修改如下
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.vesus</groupId> <artifactId>springcloud-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-ribbon</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.vesus</groupId> <artifactId>springcloud-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- eureka:微服务注册 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!--ribbon:负载均衡--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4、创建application.yml配置文件,增加注册中心地址
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ # 注册中心地址 spring: application: name: spring-cloud-ribbon server: port: 8763
5、入口方法加入@EnableDiscoveryClient,增加负载均衡支持。
package com.vesus.springcloudribbon; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class SpringcloudRibbonApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudRibbonApplication.class, args); } @Bean @LoadBalanced // 添加负载均衡支持,很简单,只需要在RestTemplate上添加@LoadBalanced注解, // 那么RestTemplate即具有负载均衡的功能,如果不加@LoadBalanced注解的话, // 会报java.net.UnknownHostException异常,此时无法通过注册到Eureka Server上的服务名来调用服务, // 因为RestTemplate是无法从服务名映射到ip:port的,映射的功能是由LoadBalancerClient来实现的。 RestTemplate restTemplate(){ return new RestTemplate() ; } }
5、添加测试类HelloService
package com.vesus.springcloudribbon.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class HelloService { @Autowired RestTemplate restTemplate ; public String sayhello() { //SPRING-CLOUD-SERVICE为注册到Eureka Server上的应用名 return restTemplate.getForObject("http://SPRING-CLOUD-SERVICE/hello",String.class); } }
6、加入控制器HelloWordController
package com.vesus.springcloudribbon.controller; import com.vesus.springcloudribbon.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloControler { @Autowired HelloService helloService ; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String sayHello(){ return helloService.sayhello() ; } }
访问 http://localhost:8763/hello ,显示hello world 。
源码:https://gitee.com/vesus198/springcloud-demo/tree/master/springcloud-ribbon