javascript
Dubbo与SpringBoot整合流程(从实例入手,附代码下载)
場景
Dubbo環(huán)境搭建-管理控制臺dubbo-admin實現(xiàn)服務(wù)監(jiān)控:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103624846
Dubbo搭建HelloWorld-搭建服務(wù)提供者與服務(wù)消費者并完成遠程調(diào)用(附代碼下載):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103675259
在上面搭建好Dubbo的HelloWorld后,將其與Springboot項目進行整合。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費下載。
實現(xiàn)
還是采用上面用戶服務(wù)提供者和訂單服務(wù)消費者的模式。
將公共的接口和實體類抽離出來,放在gmall-interface中。
?
新建服務(wù)提供者
打開Eclipse-新建一個Spring Starter Project
?
點擊Next,輸入相關(guān)包名與應(yīng)用名
?
點擊next,這里新建的是服務(wù)提供者,只需要簡單的SpringBoot項目,不用選擇Web依賴,直接點擊Next,
注意這里的SpringBoot的版本為2.2.2,在后面選擇Dubbo-starter依賴時有版本對應(yīng)關(guān)系。
?
建完之后的目錄為
?
然后打開pom.xml,添加上面公共接口的依賴以及dubbo-starter的依賴。
引入公共接口依賴,具體實現(xiàn)參照上面博客
??<dependency><groupId>com.badao.gmall</groupId><artifactId>gmall-interface</artifactId><version>0.0.1-SNAPSHOT</version></dependency>引入dubbo-starter以及相關(guān)依賴
按照其官方指示:https://github.com/apache/dubbo-spring-boot-project
添加相應(yīng)的dubbo的依賴和dubbo-starter的依賴
?
完整pom.xml代碼
<?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.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.badao</groupId><artifactId>boot-user-service-provider</artifactId><version>0.0.1-SNAPSHOT</version><name>boot-user-service-provider</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>com.badao.gmall</groupId><artifactId>gmall-interface</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!-- 引入dubbo --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.6.2</version></dependency><!-- 注冊中心使用的是zookeeper,引入操作zookeeper的客戶端端 --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>2.12.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><!-- Dubbo Spring Boot Starter --><dependency><groupId>com.alibaba.boot</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>0.2.1.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>參考上面博客搭建服務(wù)提供者時配置信息是在provider.xml中進行配置,在這里要將其配置在application.properties中。
dubbo.application.name=user-service-provider dubbo.registry.address=127.0.0.1:2181 dubbo.registry.protocol=zookeeper # dubbo.protocol.name=dubbo dubbo.protocol.port=20881 # dubbo.monitor.protocol=registry dubbo.scan.base-packages=com.badao.gmall具體配置信息作用見上面博客?;蛘邊⒄掌涔俜街甘?/p>
?
然后將上面博客搭建好的serviceImpl復(fù)制到springBoot項目中
package com.badao.gmall.service.impl;import java.util.Arrays; import java.util.List;import org.springframework.stereotype.Component;import com.alibaba.dubbo.config.annotation.Service; import com.badao.gmall.bean.UserAddress; import com.badao.gmall.service.UserService;@Service? //暴露服務(wù) @Component public class UserServiceImpl implements UserService {public List<UserAddress> getUserAddressList(String userId) {// TODO Auto-generated method stubUserAddress address1 = new UserAddress(1, "霸道流氓氣質(zhì)", "1", "李老師", "123456789", "Y");UserAddress address2 = new UserAddress(2, "公眾號:霸道的程序猿)", "1", "王老師", "987654321", "N");return Arrays.asList(address1,address2);}}注意的是之前在上面搭建的spring項目中使用的@Service注解是spring的注解,而這里使用的dubbo的注解
import com.alibaba.dubbo.config.annotation.Service;為了區(qū)分spring的@Service注解,所以使用spring的@Componment注解。
dubbo的@Service注解的作用是指定要暴露的服務(wù),讓別人能引用。其作用就是上面在provider.xml中使用dubbo-service標(biāo)簽
暴露服務(wù)一樣
?
然后在SpringBoot的主程序中添加注解@EnableDubbo時支持dubbo
package com.badao.gmall;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;@EnableDubbo?? //開啟基于注解的dubbo功能 @SpringBootApplication public class BootUserServiceProviderApplication {public static void main(String[] args) {SpringApplication.run(BootUserServiceProviderApplication.class, args);}}啟動提供者主程序
?
使用上面博客中搭建好的管理平臺可見服務(wù)提供者搭建成功
?
新建服務(wù)消費者
參照上面搭建服務(wù)提供者的流程,新建服務(wù)消費者
?
此時添加web依賴
?
此時的pom.xml同樣引入公共接口依賴與dubbo和dubbo-starter相關(guān)依賴
<?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.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.badao</groupId><artifactId>boot-order-service-consumer</artifactId><version>0.0.1-SNAPSHOT</version><name>boot-order-service-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>com.badao.gmall</groupId><artifactId>gmall-interface</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><!-- 引入dubbo --><!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.6.2</version></dependency><!-- 注冊中心使用的是zookeeper,引入操作zookeeper的客戶端 --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>2.12.0</version></dependency><!-- Dubbo Spring Boot Starter --><dependency><groupId>com.alibaba.boot</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>0.2.1.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>?
然后打開其配置文件進行相關(guān)配置
server.port=8081dubbo.application.name=boot-order-service-consumer dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.monitor.protocol=registry?
注意:這里修改端口號是因為8080已經(jīng)被dubbo-monitor所占用,
具體配置的作用參照上面博客。
然后將服務(wù)消費者的接口實現(xiàn)復(fù)制過來
package com.badao.gmall.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.alibaba.dubbo.config.annotation.Reference; import com.badao.gmall.bean.UserAddress; import com.badao.gmall.service.OrderService; import com.badao.gmall.service.UserService;/*** 1、將服務(wù)提供者注冊到注冊中心(暴露服務(wù))* ??1)、導(dǎo)入dubbo依賴(2.6.2)\操作zookeeper的客戶端(curator)* ??2)、配置服務(wù)提供者** 2、讓服務(wù)消費者去注冊中心訂閱服務(wù)提供者的服務(wù)地址* @author badao**/ @Service public class OrderServiceImpl implements OrderService {//@Autowired@ReferenceUserService userService;public List<UserAddress> initOrder(String userId) {// TODO Auto-generated method stubSystem.out.println("用戶id:"+userId);//1、查詢用戶的收貨地址List<UserAddress> addressList = userService.getUserAddressList(userId);for (UserAddress userAddress : addressList) {System.out.println(userAddress.getUserAddress());}return addressList;}}注意:這里的自動注入的@Autowired注解要修改為dubbo的@Reference,其作用是能遠程引用userService的服務(wù),自己能從服務(wù)注冊中心發(fā)現(xiàn)。
其作用相當(dāng)于之前的consumer.xml中使用dubbo:reference標(biāo)簽聲明需要調(diào)用的遠程服務(wù)接口,即生成遠程服務(wù)代理。
?
在包下新建controller包以及OrderController類
package com.badao.gmall.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.badao.gmall.bean.UserAddress; import com.badao.gmall.service.OrderService;@Controller public class OrderController {@AutowiredOrderService orderService;@ResponseBody@RequestMapping("/initOrder")public List<UserAddress> initOrder(@RequestParam("uid")String userId) {return orderService.initOrder(userId);}}然后修改主程序開啟dubbo支持
package com.badao.gmall;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;@EnableDubbo @SpringBootApplication public class BootOrderServiceConsumerApplication {public static void main(String[] args) {SpringApplication.run(BootOrderServiceConsumerApplication.class, args);}}啟動應(yīng)用主程序
此時在管理平臺就會監(jiān)控到服務(wù)消費者
?
然后打開瀏覽器,輸入:localhost:8081/initOrder?uid=1
?
示例代碼下載
https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12052055
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Dubbo与SpringBoot整合流程(从实例入手,附代码下载)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZedGraph怎样在双击图形后添加箭头
- 下一篇: AndroidStudio报错:Grad