LoadBalancer

一、手写随机负载均衡

1、引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>


<!--引入nacos discovery-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

2、controller定义

@Resource
private RestTemplate restTemplate;

@Resource
private DiscoveryClient discoveryClient;

@RequestMapping("/restLoadbalance")
public String manualLoadBlance(){
    //1、获取order服务urls
    List<ServiceInstance> instances = this.discoveryClient.getInstances("order");
    List<String> urls = instances.stream().map(instance->instance.getUri().toString()+"/order/query").collect(Collectors.toList());

    //2、随机负载请求
    int index = ThreadLocalRandom.current().nextInt(urls.size());
    return this.restTemplate.getForObject(urls.get(index),String.class);
}

二、LoadBalancer

2.1、API

(1)、引入依赖

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

(2)、代码

//2、loadbalancer api
@Resource
private LoadBalancerClient loadBalancerClient;


@RequestMapping("/loadbanlancerapi")
public String loadBanlancerApi(){
    ServiceInstance serviceInstance = this.loadBalancerClient.choose("order");
    return this.restTemplate.getForObject(serviceInstance.getUri().toString()+"/order/query",String.class);
}

2.2、注解

(1)、引入依赖

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

(2)、自定义@LoadBanlancer注解得RestTemplate

@Bean
@LoadBalanced   // 相当于为restTemplate整合了lb的能力
public RestTemplate restTemplateLoadBalancer(){
    return new RestTemplate();
}

(3)、controller代码

@Resource
private RestTemplate restTemplateLoadBalancer;
@RequestMapping("/loadbanlanceraop")
public String loadBanlancerAop(){
    return this.restTemplateLoadBalancer.getForObject("http://order/order/query",String.class);
}

三、默认负载算法

默认轮询负载:RoundRobinLoadBalancer

更改默认负载算法:Cloud Native Applications

(1)、自定义负载算法

public class CustomLoadBalancerConfiguration {
    @Bean
    ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,
                                                            LoadBalancerClientFactory loadBalancerClientFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new RandomLoadBalancer(loadBalancerClientFactory
                .getLazyProvider(name, ServiceInstanceListSupplier.class),
                name);
    }
}

(2)、启动类增加注解

@SpringBootApplication
@LoadBalancerClients(defaultConfiguration = CustomLoadBalancerConfiguration.class)
public class UserApplicaiton implements ApplicationRunner {
}

四、源码分析

(1)、RestTemplate流程

(2)、LoadBalancerClient#choose(String serviceId)

相关推荐
荆州克莱16 小时前
mysql中局部变量_MySQL中变量的总结
spring boot·spring·spring cloud·css3·技术
解梦者1 天前
Spring(七)Spring Cloud----Feign、Zuul和Apollo
spring·spring cloud·feign·apollo·zuul
新手小袁_J1 天前
JDK11下载安装和配置超详细过程
java·spring cloud·jdk·maven·mybatis·jdk11
维李设论2 天前
Node.js的Web服务在Nacos中的实践
前端·spring cloud·微服务·eureka·nacos·node.js·express
Doker 多克2 天前
IntelliJ IDEA Docker集成
spring cloud·docker·intellij-idea
Hello Dam2 天前
面向微服务的Spring Cloud Gateway的集成解决方案:用户登录认证与访问控制
spring cloud·微服务·云原生·架构·gateway·登录验证·单点登录
小马爱打代码3 天前
SpringCloud(注册中心+OpenFeign+网关+配置中心+服务保护+分布式事务)
分布式·spring·spring cloud
小笨猪-3 天前
统⼀服务⼊⼝-Gateway
java·spring cloud·微服务·gateway
岁月变迁呀3 天前
Spring Cloud Gateway 源码
java·spring·spring cloud·gateway