欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

程序员文章站 2022-04-15 19:59:20
1.Ribbon入门介绍...

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

1.Ribbon入门介绍

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

2.Ribbon的负载均衡和Rest调用

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

3.Ribbon默认自带的负载规则

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

4.Ribbon负载规则替换

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

5.Ribbon默认负载轮训算法原理

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

public class RoundRobinRule extends AbstractLoadBalancerRule {

    private AtomicInteger nextServerCyclicCounter;
    private static final boolean AVAILABLE_ONLY_SERVERS = true;
    private static final boolean ALL_SERVERS = false;

    private static Logger log = LoggerFactory.getLogger(RoundRobinRule.class);

    public RoundRobinRule() {
        nextServerCyclicCounter = new AtomicInteger(0);
    }

    public RoundRobinRule(ILoadBalancer lb) {
        this();
        setLoadBalancer(lb);
    }

    public Server choose(ILoadBalancer lb, Object key) {
        if (lb == null) {
            log.warn("no load balancer");
            return null;
        }

        Server server = null;
        int count = 0;
        while (server == null && count++ < 10) {
            List<Server> reachableServers = lb.getReachableServers();
            List<Server> allServers = lb.getAllServers();
            ////获取状态是活着的,健康的机器,计数
            int upCount = reachableServers.size();
            //获取所有机器,计数
            int serverCount = allServers.size();

            if ((upCount == 0) || (serverCount == 0)) {
                log.warn("No up servers available from load balancer: " + lb);
                return null;
            }

            int nextServerIndex = incrementAndGetModulo(serverCount);
            //下一次是哪个机器要启动
            server = allServers.get(nextServerIndex);

            if (server == null) {
                /* Transient. */
                Thread.yield();
                continue;
            }

            if (server.isAlive() && (server.isReadyToServe())) {
                return (server);
            }

            // Next.
            server = null;
        }

        if (count >= 10) {
            log.warn("No available alive servers after 10 tries from load balancer: "
                    + lb);
        }
        return server;
    }
    private int incrementAndGetModulo(int modulo) {
    	//自旋锁
        for (;;) {
            int current = nextServerCyclicCounter.get();
            int next = (current + 1) % modulo;
            //比较并交换
            if (nextServerCyclicCounter.compareAndSet(current, next))
                return next;
        }
    }
}

6.手写轮询算法

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
修改8001和8002的controller
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

7.OpenFeign

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

8.OpenFeign服务调用

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

<?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">
    <parent>
        <artifactId>springcloud2020</artifactId>
        <groupId>com.atguigu.springcolud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-order80</artifactId>
    <description>订单消费者之feign</description>

    <dependencies>
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

9.OpenFeign超时控制

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

10.OpenFeign日志增强

SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记
SpringCloud_4Ribbon负载均衡服务调用和OpenFeign服务接口调用学习笔记

本文地址:https://blog.csdn.net/lizongxiao/article/details/107447713

相关标签: ribbon 分布式