在SpringBoot中無(wú)縫整合Dubbo的實(shí)現(xiàn)過(guò)程
前言
微服務(wù)架構(gòu)已經(jīng)成為現(xiàn)代應(yīng)用開(kāi)發(fā)的熱門(mén)趨勢(shì),而Dubbo作為一款強(qiáng)大的分布式服務(wù)框架,與Spring Boot的結(jié)合是構(gòu)建高性能微服務(wù)應(yīng)用的理想選擇。就像拼裝一把鋒利的刀刃,讓我們一起揭開(kāi)Spring Boot整合Dubbo的神秘面紗,探索如何在分布式世界中獲得競(jìng)爭(zhēng)優(yōu)勢(shì)。
項(xiàng)目結(jié)構(gòu)

主項(xiàng)目(作為主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>fun.bo</groupId>
<artifactId>dubbo-studys</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>dubbo-provider</module>
<module>dubbo-interface</module>
<module>dubbo-consumer</module>
</modules>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dubbo-version>2.7.8</dubbo-version>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Apache Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo-version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-version}</version>
</dependency>
<!-- Dubbo核心組件 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!--Spring Boot 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Zookeeper客戶端框架 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<!-- Zookeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo-version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
接口
package fun.bo.api;
/**
* @author todoitbo
* @date 2024/1/12
*/
public interface TestDubboService {
String sayHello(String name);
}
服務(wù)提供者
properties文件
# 服務(wù)端口
server.port=18081
# 應(yīng)用程序名稱
spring.application.name=dubbo-provider
# Dubbo服務(wù)掃描路徑
dubbo.scan.base-packages=fun.bo
# Dubbo 通訊協(xié)議
dubbo.protocol.name=dubbo
# Dubbo服務(wù)提供的端口, 配置為-1,代表為隨機(jī)端口 默認(rèn)20880
dubbo.protocol.port=-1
## Dubbo 注冊(cè)器配置信息
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.registry.file = ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
dubbo.spring.provider.version = 1.0.0
實(shí)現(xiàn)類
package fun.bo.iml;
import fun.bo.api.TestDubboService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
/**
* @author todoitbo
* @date 2024/1/12
*/
@DubboService(version = "${dubbo.spring.provider.version}")
public class TestDubboServiceImpl implements TestDubboService {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}
服務(wù)消費(fèi)者
properties
# 服務(wù)端口
server.port=18084
#服務(wù)名稱
spring.application.name=dubbo-spring-consumer
#服務(wù)版本號(hào)
dubbo.spring.provider.version = 1.0.0
#消費(fèi)端注冊(cè)器配置信息
dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.consumer.check=false
dubbo.registry.file = ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
接口層
package fun.bo.controller;
import fun.bo.api.TestDubboService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @author todoitbo
* @date 2024/1/12
*/
@RestController
public class TestDubboController {
@DubboReference(version = "${dubbo.spring.provider.version}")
private TestDubboService testDubboService;
@GetMapping("/test-dubbo/{name}")
public String testDubbo(@PathVariable String name) {
return testDubboService.sayHello(name);
}
}
實(shí)現(xiàn)效果圖

以上就是在SpringBoot中無(wú)縫整合Dubbo的實(shí)現(xiàn)過(guò)程的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot無(wú)縫整合Dubbo的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java編程實(shí)現(xiàn)獲取服務(wù)器IP地址及MAC地址的方法
這篇文章主要介紹了java編程實(shí)現(xiàn)獲取機(jī)器IP地址及MAC地址的方法,實(shí)例分析了Java分別針對(duì)單網(wǎng)卡及多網(wǎng)卡的情況下獲取服務(wù)器IP地址與MAC地址的相關(guān)技巧,需要的朋友可以參考下2015-11-11
SpringCloud中的斷路器(Hystrix)和斷路器監(jiān)控(Dashboard)
本篇主要介紹的是SpringCloud中的斷路器(Hystrix)和斷路器指標(biāo)看板(Dashboard)的相關(guān)使用知識(shí),需要的朋友可以參考下2019-06-06
Mybatis實(shí)現(xiàn)SQL映射的兩種方法(xml文件形式和注解形式)
這篇文章主要介紹了Mybatis實(shí)現(xiàn)SQL映射的兩種方法(xml文件形式和注解形式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java文件讀寫(xiě)IO/NIO及性能比較詳細(xì)代碼及總結(jié)
這篇文章主要介紹了Java文件讀寫(xiě)IO/NIO及性能比較詳細(xì)代碼及總結(jié),具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
JVM調(diào)整java虛擬機(jī)可使用的最大內(nèi)存的方法
本文主要介紹了調(diào)整JVM的內(nèi)存參數(shù)來(lái)優(yōu)化Java應(yīng)用程序的性能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
SpringBoot實(shí)現(xiàn)JPA多數(shù)據(jù)源配置小結(jié)
本文主要介紹了SpringBoot實(shí)現(xiàn)JPA多數(shù)據(jù)源配置小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
java遞歸設(shè)置層級(jí)菜單的實(shí)現(xiàn)
本文主要介紹了java遞歸設(shè)置層級(jí)菜單的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
項(xiàng)目連接nacos配置中心報(bào)錯(cuò):Client not connected, current
這篇文章主要介紹了項(xiàng)目連接nacos配置中心報(bào)錯(cuò):Client not connected, current status:STARTING的解決方案,采用了mysql作為持久化的數(shù)據(jù)庫(kù),docker作為運(yùn)行的環(huán)境,感興趣的朋友跟隨小編一起看看吧2024-03-03

