ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Dubbo+Zookeeper笔记

2022-01-07 22:03:33  阅读:164  来源: 互联网

标签:Dubbo dubbo int Zookeeper invokers 笔记 invocation get cloud


Dubbo简介

Dubbo是阿里巴巴开源的一款Java RPC框架

  • 一款分布式服务框架

  • 高性能和透明化的RPC远程服务调用方案

  • SOA服务治理方案

核心功能

智能容错和负载均衡

服务注册和发现

面向接口的远程方法调用

角色

Provider(生产者):暴露服务的服务提供者

Container:服务运行的容器

Consumer(消费者):调用远程服务的消费者

Registry:服务注册和发现的注册中心

Minitor:统计服务调用次数和时间的监控中心

架构

1、单体应用

2、垂直应用

3、SOA 架构

4、微服务架构

dubbo+zookeeper

zookeeper是dubbo的注册中心,可以用于dubbo作负载均衡。

1、安装zookeeper

https://zookeeper.apache.org/releases.html

2、安装dubbo

https://github.com/apache/dubbo

配置 依赖:

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.bx.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

多个服务端口使用一个通讯协议

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world"  />
    <dubbo:registry id="registry" address="" username="admin" password="hello1234" />
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- Use dubbo protocol to expose the service -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" />
    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" />
</beans>

LoadBalance算法

@SPI(RandomLoadBalance.NAME)
public interface LoadBalance {
    @Adaptive("loadbalance")
    <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException;
 
}
 

随机均衡算法

public class RandomLoadBalance extends AbstractLoadBalance {
 
    public static final String NAME = "random";
    private final Random random = new Random();
    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        //provider 的数量
        int length = invokers.size(); 
        int totalWeight = 0; 
        boolean sameWeight = true; 
        for (int i = 0; i < length; i++) {
            int weight = getWeight(invokers.get(i), invocation);
            totalWeight += weight; 
            if (sameWeight && i > 0
                    && weight != getWeight(invokers.get(i - 1), invocation)) {
                sameWeight = false;
            }
        }
        if (totalWeight > 0 && !sameWeight) {
            int offset = random.nextInt(totalWeight);

            for (int i = 0; i < length; i++) {
                offset -= getWeight(invokers.get(i), invocation);
                if (offset < 0) {
                    return invokers.get(i);
                }
            }
        }
        return invokers.get(random.nextInt(length));
    }
}

标签:Dubbo,dubbo,int,Zookeeper,invokers,笔记,invocation,get,cloud
来源: https://www.cnblogs.com/yezhuang/p/15776958.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有