欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?boot整合dubbo+zookeeper的詳細過程

 更新時間:2025年07月11日 11:25:42   作者:ciku  
本文講解Spring?Boot整合Dubbo與Zookeeper實現(xiàn)API、Provider、Consumer模式,包含依賴配置、啟動類注解、服務注冊與調用步驟,及QoS、超時、負載均衡等參數(shù)設置,確保分布式服務通信正常運行,感興趣的朋友一起看看吧

Spring boot整合dubbo+zookeeper

下文將簡述springboot整合dubbo+zookeeper實現(xiàn)api+provider+consumer模式,Api用于定于interface,provider和consumer依賴Api,provider實現(xiàn)api接口,consumer調用provider。
spring boot版本:3.5.3
jdk版本:java17

1.創(chuàng)建父工程

此處只勾選了spring web和lombok,需要Orm框架自行增加

2.父工程引入依賴

父工程pom.xml,引入dubbo和zookeeper相關依賴,修改packaging為pom不打包,并刪除src目錄

           <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>3.2.10</version>
        </dependency>
        <!-- Zookeeper客戶端 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-x-discovery</artifactId>
            <version>5.2.0</version> <!-- 與Zookeeper版本匹配 -->
        </dependency>
           <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>3.5.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

3.創(chuàng)建api子模塊

(1).新建dubbo-api module

(2).修改pom.xml文件
修改dubbo-api的pom.xml,parent修改為剛才新建的groupId、artifactId和version,再去掉重復的配置

<parent>
		<groupId>com.example</groupId>
		<artifactId>DubboDemo</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

父工程pom.xml增加子模塊

<modules>
		<module>dubbo-api</module>
	</modules>

( 3).修改配置
修改application.properties為application.yml,并增加dubbo配置內容

spring:
  application:
      name: dubbo-api
  config:
    activate:
      on-profile: default
server:
  port: 8080
dubbo:
  application:
     name: dubbo-api
  protocol:
    name: dubbo
    port: 20880
  registry:
    address: zookeeper://192.168.48.154:2181

dubbo.protocol 此處協(xié)議不是自定義的,自由dubbo、rpc等類型
registry:配置zookeeper地址
(4)啟動類,開啟dubbo,
@EnableDubbo 注解用于開啟dubbo客戶端,scanBasePackages 可以配置包路徑

@SpringBootApplication
@EnableDubbo
public class DubboApiApplication{
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }
}

(5)新增Service接口

public interface TestService {
    String sayHello(String name);
}

4.創(chuàng)建provider子模塊

(1)創(chuàng)建dubbo-provider模塊,和上面一樣,就不贅述了
(2)修改pom.xml文件
依賴中加入api模塊的依賴

  <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

(3)修改配置文件
application.yml

spring:
  application:
   name: provider
  config:
    activate:
      on-profile: default
server:
  port: 8081
dubbo:
  application:
    name: dubbo-provider
    qos-enable: true
    qos-port: 22223
  registry:
    address: zookeeper://192.168.48.154:2181
  protocol:
    name: dubbo
    port: 20881  #同一臺機器不能相同

對比api模塊增加了qos-enable和qos-port兩個屬性,QoS是Dubbo提供的運維和管理功能,表示開啟;qos-port表示運維管理功能的端口
(4)啟動類,開啟dubbo,
@EnableDubbo 注解用于開啟dubbo客戶端,scanBasePackages 可以配置包路徑

@SpringBootApplication
@EnableDubbo
public class DubboProviderApplication {
    public static void main(String[] args) {
        System.setProperty("zookeeper.sasl.client", "false");
        SpringApplication.run(DubboProviderApplication.class, args);
    }
}

(5)實現(xiàn)TestService接口

package com.examplexu.dubboprovider.service;
import com.examplexu.duubo.service.TestService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(version = "1.0.0")
public class TestServiceImpl implements TestService {
    @Override
    public String sayHello(String name) {
        return "welcome"+name+" to dubbo!";
    }
}

@DubboService 的作用為標記TestService為服務提供者,暴露為dubbo服務,并注冊到zookeeper并進行管理??梢耘渲玫膮?shù)如下:
version: 服務版本號(必填)
timeout: 調用超時時間(毫秒)
loadbalance: 負載均衡策略
retries: 失敗重試次數(shù)
check: 啟動時檢查依賴服務是否可用

5.創(chuàng)建consumer子模塊

(1)創(chuàng)建dubbo-consumer模塊,和上面一樣,就不贅述了
(2)修改pom.xml文件
依賴中加入api模塊的依賴

  <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

(3)修改配置文件

spring:
  application:
    name: consumer
  config:
    activate:
      on-profile: default
server:
  port: 8084
dubbo:
  config:
  application:
    name: dubbo-consumer
    qos-enable: true
    qos-port: 22224
  registry:
    address: zookeeper://192.168.48.154:2181
    timeout: 20000
  protocol:
    name: dubbo
    port: 20882
  config-center:
    timeout: 20000

此處增加了超時時間,注冊中心如果超時,會拋出異常,若配置了重試會進行重試操作;配置中心超時,會記錄警告日志,可能會影響動態(tài)配置更新
(5)啟動類,開啟dubbo

@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
    public static void main(String[] args) {
        System.setProperty("zookeeper.sasl.client", "false");
        SpringApplication.run(DubboConsumerApplication.class, args);
    }
}

(6)增加Controller調用provider

@RestController
@RequestMapping("/consumer")
public class TestController {
    @DubboReference(version = "1.0.0",timeout = 5000,loadbalance = "roundrobin",retries = 3,check = false)
    private TestService testService;
    @GetMapping("/sayHello")
    public String test(String name) {
        return testService.sayHello(name);
    }
}

@DubboReference 用于標記引用Dubbo服務,自動從注冊中心發(fā)現(xiàn)并創(chuàng)建服務代理,實現(xiàn)服務的遠程調用。主要配置參數(shù)如下:
version 版本號,與服務提供者提供的version一直
timeout 單次調用超時時間
loadbalance 負載均衡策略有random/roundrobin/leastactive策略,即隨機、輪詢、近期最少使用
retries 失敗自動重試次數(shù)
check 啟動時是否檢查依賴服務的可用性

6.啟動服務

依次啟動dubbo-api、dubbo-provider、dubbo-consumer;啟動時可以查看日志會出現(xiàn)很多zookeeper和dubbo的日志

啟動過程中如果報錯:

2025-07-10T15:28:14.979+08:00 ERROR 37840 --- [consumer] [           main] o.a.d.c.deploy.DefaultModuleDeployer     :  [DUBBO] Model reference failed: Dubbo Module[1.1.1] , catch error : Can not create registry service-discovery-registry://192.168.48.154:2181/org.apache.dubbo.registry.RegistryService?application=dubbo-consumer&dubbo=2.0.2&executor-management-mode=isolation&file-cache=true&interface=org.apache.dubbo.registry.RegistryService&pid=37840&qos.enable=true&qos.port=22224&registry=zookeeper&release=3.2.10&timeout=20000, dubbo version: 3.2.10, current host: 192.168.1.93, error code: 5-15. This may be caused by , go to https://dubbo.apache.org/faq/5/15 to find instructions. 
java.lang.RuntimeException: Can not create registry service-discovery-registry://192.168.48.154:2181/org.apache.dubbo.registry.RegistryService?application=dubbo-consumer&dubbo=2.0.2&executor-management-mode=isolation&file-cache=true&interface=org.apache.dubbo.registry.RegistryService&pid=37840&qos.enable=true&qos.port=22224&registry=zookeeper&release=3.2.10&timeout=20000
	at org.apache.dubbo.registry.support.AbstractRegistryFactory.getRegistry(AbstractRegistryFactory.java:105) ~[dubbo-3.2.10.jar:3.2.10]
	at org.apache.dubbo.registry.RegistryFactoryWrapper.getRegistry(RegistryFactoryWrapper.java:33) ~[dubbo-3.2.10.jar:3.2.10]

啟動類中新增:

 System.setProperty("zookeeper.sasl.client", "false");

表示不進行SASL認證,生產環(huán)境視要求具體設置

測試結果

當dubbo-api、dubbo-provider和dubbo-consumer都啟動完成后,使用消費者接口訪問。

可以發(fā)現(xiàn)此處消費者調用的是提供者的實現(xiàn)內容。

到此這篇關于Spring boot整合dubbo+zookeeper的文章就介紹到這了,更多相關Spring boot整合dubbo+zookeeper內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java通過SSLEngine與NIO實現(xiàn)HTTPS訪問的操作方法

    Java通過SSLEngine與NIO實現(xiàn)HTTPS訪問的操作方法

    這篇文章主要介紹了Java通過SSLEngine與NIO實現(xiàn)HTTPS訪問,需要在Connect操作、Connected操作、Read和Write操作中加入SSL相關的處理即可,需要的朋友可以參考下
    2021-08-08
  • Java如何固定大小的線程池

    Java如何固定大小的線程池

    這篇文章主要介紹了Java固定大小的線程池操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java泛型extends關鍵字設置邊界的實現(xiàn)

    Java泛型extends關鍵字設置邊界的實現(xiàn)

    這篇文章主要介紹了Java泛型extends關鍵字設置邊界的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • elasticsearch bucket 之rare terms聚合使用詳解

    elasticsearch bucket 之rare terms聚合使用詳解

    這篇文章主要為大家介紹了elasticsearch bucket 之rare terms聚合使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • java ArrayList和Vector的區(qū)別詳解

    java ArrayList和Vector的區(qū)別詳解

    這篇文章主要介紹了java ArrayList和Vector的區(qū)別詳解的相關資料,并附簡單實例代碼,需要的朋友可以參考下
    2016-11-11
  • 詳解Java內部類——匿名內部類

    詳解Java內部類——匿名內部類

    這篇文章主要介紹了詳解Java 匿名內部類的相關資料,幫助大家更好的理解和學習java 內部類的相關知識,感興趣的朋友可以了解下
    2020-08-08
  • SpringCloud中Gateway實現(xiàn)鑒權的方法

    SpringCloud中Gateway實現(xiàn)鑒權的方法

    本文主要介紹了SpringCloud中Gateway實現(xiàn)鑒權的方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 快速了解JAVA垃圾回收機制

    快速了解JAVA垃圾回收機制

    這篇文章主要介紹了有關Java垃圾回收機制的知識,文中實例簡單易懂,方便大家更好的學習,有興趣的朋友可以了解下
    2020-06-06
  • Spring security 如何開放 Swagger 訪問權限

    Spring security 如何開放 Swagger 訪問權限

    這篇文章主要介紹了Spring security 如何開放 Swagger 訪問權限操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java 中 阻塞隊列BlockingQueue詳解及實例

    java 中 阻塞隊列BlockingQueue詳解及實例

    這篇文章主要介紹了java 中 阻塞隊列BlockingQueue詳解及實例的相關資料,需要的朋友可以參考下
    2017-03-03

最新評論