Ribbon

Ribbon是什么

Ribbon是Netflix发布的云中间层服务开源项目,其主要功能是提供客户端实现负载均衡算法。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,Ribbon是一个客户端负载均衡器,我们可以在配置文件中Load Balancer后面的所有机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器,我们也很容易使用Ribbon实现自定义的负载均衡算法。

Eureka结合Ribbon的使用

Ribbon工作流程

Ribbon工作时分为两步:

第一步选择Eureka Server,它优先选择在同一个Zone且负载较少的Server;

第二步再根据用户指定的策略,再从Server取到的服务注册列表中选择一个地址。其中Ribbon提供了很多策略,例如轮询round robin、随机Random、根据响应时间加权等。

Ribbon的使用

如何集成Ribbon?

查看Spring cloud官方文档,搜索Ribbon。

① 首先引入Ribbon依赖

按照官方的意思是需要加入以下依赖

<dependency>

   <groupId>org.springframework.cloud</groupId>

   <artifactId>spring-cloud-starter-ribbon</artifactId>

</dependency>

但是其实是不需要的加入这个依赖的,在spring-cloud-starter-eureka依赖中就已经包含了Ribbon Starter (上节已知spring-cloud-starter-eureka-server 是为编写Eureka Server提供依赖,spring-cloud-starter-eureka是为编写Eureka Client提供依赖),因此只需要Eureka Client具有spring-cloud-starter-eureka依赖即可。即在POM中需要有 

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

 

② 如何使用Ribbon

在这里其实是服务消费者使用RestTemplate)之间的通信,为RestTemplate配置类添加@LoadBalanced注解即可,如下所示:

@Bean
@LoadBalanced
  public RestTemplate restTemplate() {
  return new RestTemplate();
}

③ 如何解决硬编码

使用添加@LoadBalanced注解后的RestTemplate调用服务提供者的接口时,可以使用虚拟IP替代真实IP地址。所谓的虚拟IP就是服务提供者在application.properties或yml文件中配置的spring.application.name属性的值。示例如下: 

@GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
  // VIP: Virtual IP http://microservice-provider-user/即虚拟IP 服务提供者的ServiceId (spring.application.name)
  return this.restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);
}

自定义RibbonClient

如何为服务消费者自定义Ribbon Client?

① 代码自定义RibbonClient

所谓的自定义Ribbon Client的主要作用就是使用自定义配置替代Ribbon默认的负载均衡策略,注意:自定义的Ribbon Client是有针对性的,一般一个自定义的Ribbon Client是对一个服务提供者(包括服务名相同的一系列副本)而言的。自定义了一个Ribbon Client 它所设定的负载均衡策略只对某一特定服务名的服务提供者有效,但不能影响服务消费者与别的服务提供者通信所使用的策略。根据官方文档的意思,推荐在 springboot主程序扫描的包范围之外进行自定义配置类。其实纯代码自定义RibbonClient的话有两种方式:

方式一:在springboot主程序扫描的包外定义配置类,然后为springboot主程序添加@RibbonClient注解引入配置类 

注意:@RibbonClient注解中的name属性是指服务提供者的服务名(即当前消费者使用自定义配置与其通信的服务提供者的spring.application.name的属性)

@RibbonClient(name = "microservice-provider-user",configuration = TestConfiguration.class)

方式二:在与springboot主程序的同一级目录新建RibbonClient的配置类,但是必须在springboot扫描的包范围内排除掉,方法是自定义注解标识配置类,然后在springboot的添加@ComponentScan根据自定义注解类型过滤掉配置类

自定义注解 

public @interface ExcludeFromComponentScan {
}

自定义配置类

@Configuration
@ExcludeFromComponentScan
public class TestConfiguration1 {
    @Autowired
    private IClientConfig config;
    @Bean
    public IRule ribbonRule(IClientConfig config) { // 自定义为随机规则
        return new RandomRule();
    }
}

在springboot主程序上添加注解

@RibbonClient(name = "microservice-provider-user",configuration = TestConfiguration1.class)
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = ExcludeFromComponentScan.class)})

Ribbon负载均衡策略有以下几种:

策略类名称说明
RandomRule随机策略随机选择 Server
RoundRobinRule轮训策略按顺序循环选择 Server
RetryRule重试策略在一个配置时问段内当选择 Server 不成功,则一直尝试选择一个可用的 Server
BestAvailableRule最低并发策略逐个考察 Server,如果 Server 断路器打开,则忽略,再选择其中并发连接最低的 Server
AvailabilityFilteringRule可用过滤策略过滤掉一直连接失败并被标记为 circuit tripped 的 Server,过滤掉那些高并发连接的 Server(active connections 超过配置的网值)
ResponseTimeWeightedRule响应时间加权策略根据 Server 的响应时间分配权重。响应时间越长,权重越低,被选择到的概率就越低;响应时间越短,权重越高,被选择到的概率就越高。这个策略很贴切,综合了各种因素,如:网络、磁盘、IO等,这些因素直接影响着响应时间
ZoneAvoidanceRule区域权衡策略综合判断 Server 所在区域的性能和 Server 的可用性轮询选择 Server,并且判定一个 AWS Zone 的运行性能是否可用,剔除不可用的 Zone 中的所有 Server

OpenFeign

Feign 是一个声明性的 web 服务客户端。 它使编写 web 服务客户端更加容易。 使用 Feign 创建一个接口并对其进行注释。 它具有可插拔的注释支持,包括 Feign 注释和 JAX-RS 注释。 Feign 还支持可插拔编码器和解码器。 Spring Cloud 增加了对 Spring MVC 注释的支持,以及对使用 Spring Web 默认使用的 HttpMessageConverters 的支持。 Spring Cloud 集成了 Ribbon 和 Eureka,以及 Spring Cloud LoadBalancer,以便在使用 Feign 时提供负载平衡的 http 客户端

OpenFeign的使用

创建项目

配置项目

主启动类添加以下注解

添加@EnableFeignClients和@EnableDiscoveryClient注解


@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class OpenFeignApplication {

	public static void main(String[] args) {
		SpringApplication.run(OpenFeignApplication.class, args);
	}

}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.milo</groupId>
		<artifactId>milgenius-springcloud</artifactId>
		<version>1.0.0</version>
	</parent>
	<groupId>com.milo</groupId>
	<artifactId>springcloud-openfeign</artifactId>
	<version>1.0.0</version>
	<name>springcloud-openfeign</name>
	<description>openfeign学习项目</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR5</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<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-openfeign</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>

 

application.yml

## 注册中心
eureka:
  client:
  serviceUrl:
    defaultZone: http://localhost:8761/eureka/


server:
  port: 8766

spring:
  application:
    name: service-openfeign

相关代码

ConsumerClient


@FeignClient("service-provider")
@Component
public interface ConsumerClient {

       @GetMapping("/hello")
       String hello();
}

ConsumerController


@RestController
public class ConsumerController {

    @Autowired
    private IConsumerService consumerService;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index(@RequestParam String name){
        return  consumerService.hello(name);
    }
}

IConsumerService


public interface IConsumerService {

    String hello(String name);
}

ConsumerServiceImpl


@Service
public class ConsumerServiceImpl implements IConsumerService {

    //注入client
    @Autowired
    private ConsumerClient client;

    @Override
    public String hello(String name) {
        return client.hello();
    }
}

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐