從零開始教你dubbo自定義負載均衡策略

前言:官網文檔有一重要章節SPI,建立好好閱讀,爲本文做鋪墊

1.搭建工程,結構如下:

dubbo-learn                ---> 父pom

dubbo-api                   ---> 服務提供者和消費者共同依賴對接口api

dubbo-provide1,2,3    ---> 分別是3個服務提供者

dubbo-consumer        ---> 服務消費者

1.1) dubbo-learn對應pom

<?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">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.example</groupId>
    <artifactId>dubbo-learn</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>dubbo-provide1</module>
        <module>dubbo-provide2</module>
        <module>dubbo-provide3</module>
        <module>dubbo-consumer</module>
        <!--        <module>dubbo-api</module>-->
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.6</version>
            <scope>compile</scope>
        </dependency>
        <!-- redis做註冊中心必須依賴jedis客戶端否則報錯 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

1.2) dubbo-api層pom

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>dubbo-api</artifactId>
    <version>1.0.0</version>
</project>

1.3) dubbo-consumer層pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>dubbo-learn</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>dubbo-consumer</artifactId>
    <name>dubbo-consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.4) dubbo-privode1,2,3對應pom一樣,如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>dubbo-learn</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>dubbo-provide1</artifactId>
    <version>1.0.0</version>
    <name>dubbo-provide1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.5)dubbo-consumer 配置文件application.yml

dubbo:
  scan:
    base-packages: com.example.dubbo
  registry:
    protocol: redis
    parameters:
      # 指定redis數據庫,別問我爲什麼是這個參數,看源碼發現的
      db.index: 15
    # 上面配置了redis故直接取值即可,也可以直接寫成 address: root:[email protected]:6379
    address: root:${spring.redis.password}@${spring.redis.host}:${spring.redis.port}
    cluster: failover
  protocol:
    # 名字任意
    id: dubbo-executor
    port: 20880
    name: cust
  application:
    # 名字任意
    name: consumer
  consumer:
    # 自定義調用提供者負載均衡模式,再未進行自定義的情況下先註釋
    # loadbalance: cust
spring:
  redis:
    host: 127.0.0.1
    password: 123456
    port: 6379
    database: 3
server:
  port: 12000

1.6)dubbo-provide1,2,3 對應pom文件,注意修改端口號

dubbo:
  scan:
    base-packages: com.example.dubbo
  registry:
    protocol: redis
    parameters:
      # 指定redis數據庫,別問我爲什麼是這個參數,看源碼發現的
      db.index: 15
    # 上面配置了redis故直接取值即可,也可以直接寫成 address: root:[email protected]:6379
    address: root:${spring.redis.password}@${spring.redis.host}:${spring.redis.port}
    cluster: failover
  protocol:
    # 名字任意
    id: dubbo-executor
    port: 20881
    name: dubbo
  application:
    # 名字任意
    name: provide1

spring:
  redis:
    host: 127.0.0.1
    password: 123456
    port: 6379
    database: 3
server:
  port: 12001

筆者是本地測試,因此需要修改這2處

注意:provide1,2,3配置文件有2處需要修改端口號,1)dubbo.protocol.port: xxx ; 2) server.port: xxx ;

1.7)分別在各個模塊入口類在添加註解 @EnableDubbo

示例:

2.在dubbo-api層新建接口

3.在dubbo-provide1,2,3模塊中分別實現該接口

4.dubbo-consumer寫一個消費接口

以上就已經完成了一個消費者對應多個提供者骨架,

編譯順序

1)dubbo-api      ---> clear install

2)dubbo-learn  ---> clear install

啓動順序是:

1)啓動redis

2)啓動dubbo-provide1,2,3

3)啓動dubbo-consumer

4)測試

5.自定義負載均衡策略

說明:第4步再建一層dubbo文件夾時,其實也可以建一個名爲services文件夾,效果是一樣的

在新建的純文本文件裏輸入:

cust=com.example.dubbo.balance.CustomLoadBalance

格式:別名=自定義類全類名

6.修改dubbo-consumer配置,

完成,進行測試。

遇到的問題:

1)dubbo版本對應的jedis不應太高,否則會報錯找不到redis/clients/util/Pool

解決辦法:降低jedis版本到2.8.1(2.8.0也行自行選擇,不要高於2.9.0)

2)自定義負載均衡不生效

可能原因:

a)可能是文件層級不對,應該分開建META-INF,在建dubbo文件夾

b)配置錯誤,在消費端配置調用到提供端負載,應該配置dubbo.consumer.loadbalance,而不是配置dubbo.provider.loadbalance

寫的很亂有問題可以留言,看到後會回覆

學習源碼在gitee上:https://gitee.com/vick-chow/dubbo-learn.git

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章