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

Spring?boot整合dubbo+zookeeper的詳細(xì)過程

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

Spring boot整合dubbo+zookeeper

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

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

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

2.父工程引入依賴

父工程pom.xml,引入dubbo和zookeeper相關(guān)依賴,修改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,再去掉重復(fù)的配置

<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配置內(nèi)容

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)啟動(dòng)類,開啟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  #同一臺(tái)機(jī)器不能相同

對(duì)比api模塊增加了qos-enable和qos-port兩個(gè)屬性,QoS是Dubbo提供的運(yùn)維和管理功能,表示開啟;qos-port表示運(yùn)維管理功能的端口
(4)啟動(dòng)類,開啟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)實(shí)現(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 的作用為標(biāo)記TestService為服務(wù)提供者,暴露為dubbo服務(wù),并注冊(cè)到zookeeper并進(jìn)行管理??梢耘渲玫膮?shù)如下:
version: 服務(wù)版本號(hào)(必填)
timeout: 調(diào)用超時(shí)時(shí)間(毫秒)
loadbalance: 負(fù)載均衡策略
retries: 失敗重試次數(shù)
check: 啟動(dòng)時(shí)檢查依賴服務(wù)是否可用

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

此處增加了超時(shí)時(shí)間,注冊(cè)中心如果超時(shí),會(huì)拋出異常,若配置了重試會(huì)進(jìn)行重試操作;配置中心超時(shí),會(huì)記錄警告日志,可能會(huì)影響動(dòng)態(tài)配置更新
(5)啟動(dòng)類,開啟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調(diào)用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 用于標(biāo)記引用Dubbo服務(wù),自動(dòng)從注冊(cè)中心發(fā)現(xiàn)并創(chuàng)建服務(wù)代理,實(shí)現(xiàn)服務(wù)的遠(yuǎn)程調(diào)用。主要配置參數(shù)如下:
version 版本號(hào),與服務(wù)提供者提供的version一直
timeout 單次調(diào)用超時(shí)時(shí)間
loadbalance 負(fù)載均衡策略有random/roundrobin/leastactive策略,即隨機(jī)、輪詢、近期最少使用
retries 失敗自動(dòng)重試次數(shù)
check 啟動(dòng)時(shí)是否檢查依賴服務(wù)的可用性

6.啟動(dòng)服務(wù)

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

啟動(dòng)過程中如果報(bào)錯(cuò):

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]

啟動(dòng)類中新增:

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

表示不進(jìn)行SASL認(rèn)證,生產(chǎn)環(huán)境視要求具體設(shè)置

測(cè)試結(jié)果

當(dāng)dubbo-api、dubbo-provider和dubbo-consumer都啟動(dòng)完成后,使用消費(fèi)者接口訪問。

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

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

相關(guān)文章

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

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

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

    Java如何固定大小的線程池

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

    Java泛型extends關(guān)鍵字設(shè)置邊界的實(shí)現(xiàn)

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

    elasticsearch bucket 之rare terms聚合使用詳解

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

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

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

    詳解Java內(nèi)部類——匿名內(nèi)部類

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

    SpringCloud中Gateway實(shí)現(xiàn)鑒權(quán)的方法

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

    快速了解JAVA垃圾回收機(jī)制

    這篇文章主要介紹了有關(guān)Java垃圾回收機(jī)制的知識(shí),文中實(shí)例簡(jiǎn)單易懂,方便大家更好的學(xué)習(xí),有興趣的朋友可以了解下
    2020-06-06
  • Spring security 如何開放 Swagger 訪問權(quán)限

    Spring security 如何開放 Swagger 訪問權(quán)限

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

    java 中 阻塞隊(duì)列BlockingQueue詳解及實(shí)例

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

最新評(píng)論