温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring cloud集成ribbon负载均衡怎么实现

发布时间:2021-12-02 14:59:11 来源:亿速云 阅读:92 作者:iii 栏目:开发技术

这篇文章主要介绍“spring cloud集成ribbon负载均衡怎么实现”,在日常操作中,相信很多人在spring cloud集成ribbon负载均衡怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”spring cloud集成ribbon负载均衡怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

本文比较简单集成ribbon,如需要更详细,请查看我的更多博客内容。

首先创建两个服务提供者

spring cloud集成ribbon负载均衡怎么实现

服务一,集成的nacos注册中心,这块随便写一个同名接口

spring cloud集成ribbon负载均衡怎么实现

端口配置8301

spring cloud集成ribbon负载均衡怎么实现

服务二,同名接口内容修改,其他跟上一个服务一大体内容一致

spring cloud集成ribbon负载均衡怎么实现

端口配置成8302

spring cloud集成ribbon负载均衡怎么实现

创建服务消费者

spring cloud集成ribbon负载均衡怎么实现

RibbonConfig.java

package com.example.nacosribbonconsumers.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
// 如果多个服务可以选择不同的策略
/*@RibbonClients({
        @RibbonClient(name = "other",configuration = OtherConfig.class),
        @RibbonClient(name = "provider",configuration = ProviderConfig.class)
})*/
@RibbonClient(name = "nacos-ribbon-provider")
public class RibbonConfig {

    //定义负载均衡规则
    @Bean
    public IRule ribbonRule(){
        return new RoundRobinRule();

        /**
         * RoundRobinRule:
         *  轮询规则
         *
         * RandomRule:
         *  随机规则
         *
         * WeightedResponseTimeRule:
         *  使用响应时间的平均或者百分比为每个服务分配权重的规则,如果没法收集响应时间信息,会默认使用轮询规则
         *
         * BestAvailableRule:
         *  会先根据断路器过滤掉处于故障的服务,然后选择并发量最小的服务
         *
         * ZoneAvoidanceRule:
         *  根据server所在Zone和其性能,选择服务器,默认规则
         *
         * AvailabilityFilteringRule:
         *  先根据断路器规则过滤掉有问题的服务,然后对剩余的服务按照轮询的策略进行访问
         *
         * RetryRule:
         *  先按照RoundRobinRule规则进行服务获取,如果调用服务失败会在指定时间内进行重试,直到获取到可用的服务。
         */
    }

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

}

RibbonTest.java

package com.example.nacosribbonconsumers.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RibbonTest {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/ribbon-consumers/ribbon-test")
    public String printProviderLog(){
        String result = restTemplate.getForObject("http://nacos-ribbon-provider/ribbon-test", String.class);
        return result;
    }

}

pom包

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

配置文件

spring cloud集成ribbon负载均衡怎么实现

先启动两个服务提供者,然后在启动服务消费者,浏览访问

spring cloud集成ribbon负载均衡怎么实现
spring cloud集成ribbon负载均衡怎么实现

到此,关于“spring cloud集成ribbon负载均衡怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI