您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)SpringBoot如何整合dubbo的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
用dubbo肯定是多模塊化了 所以我們先創(chuàng)建一個聚合項目
這是項目結(jié)構(gòu)
Dubbo_demo 的pom 這個主要用來聚合業(yè)務模塊用不做任何業(yè)務處理
<?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.hzb</groupId> <artifactId>Dubbo_demo</artifactId> <version>0.0.1-SNAPSHOT</version> <!--我們這是多模塊開發(fā) 所以這邊要 變成 pom--> <packaging>pom</packaging> <!--系統(tǒng)模塊--> <modules> <module>hzbdubbo-consumer</module> <module>hzbdubbo-provider</module> </modules> <!--配置文件屬性--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> </project>
然后按照上面的方法創(chuàng)建兩個項目一個是 服務的提供者 和服務的消費者
提供者和消費者不懂的話點擊這個連接了解一下http://dubbo.io/
服務提供者代碼片段:
生成基本項目按第一個圖片的去生成
代碼結(jié)構(gòu): 由于是demo不按規(guī)范結(jié)構(gòu)來看得懂就行
服務提供者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.hzb.dubbo.provider</groupId> <artifactId>hzbdubbo-provider</artifactId> <packaging>jar</packaging> <name>hzbdubbo-provider</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.hzb</groupId> <artifactId>Dubbo_demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.10</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.2.RELEASE</version> </plugin> </plugins> </build> </project>
注意pom中 parent 對應的是什么和聚合pom進行關(guān)聯(lián)
dubbo.xml中配置:
<?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://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 配置可參考 http://dubbo.io/User+Guide-zh.htm --> <!-- 服務提供方應用名,用于計算依賴關(guān)系 --> <dubbo:application name="dubbo-provider" owner="dubbo-provider" /> <!-- 定義 zookeeper 注冊中心地址及協(xié)議 --> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" client="zkclient" /> <!-- 定義 Dubbo 協(xié)議名稱及使用的端口,dubbo 協(xié)議缺省端口為 20880,如果配置為 -1 或者沒有配置 port,則會分配一個沒有被占用的端口 --> <dubbo:protocol name="dubbo" port="-1" /> <!-- 聲明需要暴露的服務接口 --> <dubbo:service interface="com.hzb.dubbo.provider.DemoService" ref="demoService" timeout="10000" /> <!-- 和本地 bean 一樣實現(xiàn)服務 --> <bean id="demoService" class="com.hzb.dubbo.provider.DemoServiceImpl" /> </beans>
這個想要了解配置的意思點擊這個連接:http://dubbo.io/books/dubbo-user-book-en/英文版https://help.github.com/categories/github-pages-basics/
服務提供者啟動類代碼:
package com.hzb.dubbo.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication // 使用 dubbo.xml 配置 @ImportResource(value = {"classpath:dubbo.xml"}) public class DubboProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboProviderApplication.class, args); System.out.println("服務提供者啟動成功"); } }
服務 service接口代碼:
業(yè)務實現(xiàn)代碼:“
接下來就是服務消費者了:
項目結(jié)構(gòu):
消費者pom 也是要注意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.hzb.dubbo.consumer</groupId> <artifactId>hzbdubbo-consumer</artifactId> <packaging>jar</packaging> <name>hzbdubbo-consumer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.hzb</groupId> <artifactId>Dubbo_demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>1.5.2.RELEASE</version> <scope>test</scope> </dependency> <!-- 阿里巴巴提供的格式化依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.10</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.hzb.dubbo.provider</groupId> <artifactId>hzbdubbo-provider</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.2.RELEASE</version> </plugin> </plugins> </build> </project>
dubbo.xml配置:
啟動類:
測試調(diào)用服務Controller
接下來就是先把服務提供者啟動起來 然后在啟動消費者
運行結(jié)果
感謝各位的閱讀!關(guān)于“SpringBoot如何整合dubbo”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。