Rest客户端之Ribbon(三)

本文介绍了Spring Cloud Ribbon作为负载均衡框架的功能,包括其支持的可插拔负载均衡规则和多种协议。重点讲解了Ribbon的子模块,如ribbon-core、ribbon-eureka和ribbon-httpclient。此外,还探讨了负载均衡器组件的核心功能,如Rule、Ping和ServerList,并模拟了@LoadBalanced注解的工作原理,展示了如何通过拦截器实现负载均衡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介:

  1. 负载均衡框架,支持可插拔式的负载均衡规则
  2. 支持多种协议,如Http,UDP等
  3. 提供负载均衡的客户端(用在服务调用者client)

Ribbon子模块

ribbon-core

    包括负载均衡,以及负载均衡规则都在这个包里

ribbon-eureka

    为erurka客户端提供的负载均衡类 

ribbon-httpclient

    含有负载均衡功能的rest客户端

负载均衡器组件

1、一个负载均衡器,至少提供以下功能

    要维护各个服务器的ip等信息

   根据特定逻辑选取服务器

2、为了实现基本的负载均衡功能,Ribbon的负载均衡器有三大子模块

   Rule:规则

   Ping

   ServerList

//ribbon的RestTemplate调用方式
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
 return new RestTemplate();
}

@GetMappering("/router")
@ResponseBody
public String router(){
    Restemplate tpl = getRestTemplate();
    //调用方式: http://yml配置的服务id/方法名/参数
    String json = tpl.getForObject("http://服务id/call/1",String.class);
}

模拟@LoadBalanced原理

@LoadBalanced注解通过拦截器来实现负载均衡

1、新建一个注解

package spring.cloud.spring_member.controller.locdbalaned;

import org.springframework.beans.factory.annotation.Qualifier;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 自定义负载均衡的注解
 *
 * @param
 * @return
 * @date 2019-06-16 13:57
 */
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MyLoadBalanced {


}

2、注册RestTemplate实例到容器中,并加上自定义的注解

package spring.cloud.spring_member.controller.locdbalaned;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.awt.*;

/**
 * 类的描述 TODO
 *
 * @author wangwei
 * @Date 2019-06-16 14:04
 */
@RestController
@Configuration
public class MyController {

    @Bean
    @MyLoadBalanced
    RestTemplate tplA() {
        return new RestTemplate();
    }

    @Bean
    @MyLoadBalanced
    RestTemplate tplB() {
        return new RestTemplate();
    }

    @RequestMapping(value = "/call", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String call() {

        RestTemplate tpl = tplA();
        //被拦截器拦截,拦截器内部实现负载均衡
        String json = tpl.getForObject("http://app-member-server/call", String.class);
        return json;
    }

}

3、自定义拦截器,可以可以写负载均衡逻辑,todo

package spring.cloud.spring_member.controller.locdbalaned;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * 自定义拦截器
 *
 * @author wangwei
 * @Date 2019-06-16 14:08
 */
public class MyInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        System.out.println("=========自定义拦截器");
        System.out.println("=========" + httpRequest.getURI());
        //可以在这里写负载均衡逻辑代码
        return null;
    }
}

4、定义配置类,给tpls自动装配restTemplate,并且给restTemplate的拦截器增加自定义的拦截器

package spring.cloud.spring_member.controller.locdbalaned;

import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定义配置类,给restTemplate的拦截器增加自定义的拦截器
 * @Date 2019-06-16 14:01
 */
@Configuration
public class MyConfig {

    @Autowired(required = false)
    @MyLoadBalanced
    private List<RestTemplate> tpls = Collections.emptyList();

    @Bean
    public SmartInitializingSingleton Ibinitalizing() {
        return new SmartInitializingSingleton() {
            @Override
            public void afterSingletonsInstantiated() {
                System.out.println(tpls.size());
                for(RestTemplate restTemplate : tpls){
                    List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
                    interceptors.add(new MyInterceptor());
                    restTemplate.setInterceptors(interceptors);
                }
            }
        };
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三丶竹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值