場景 Dubbo環境搭建-管理控制台dubbo-admin實現服務監控: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103624846 Dubbo搭建HelloWorld-搭建服務提供者與服務消費者並完成遠程調用(附代碼下載) ...
場景
Dubbo環境搭建-管理控制台dubbo-admin實現服務監控:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103624846
Dubbo搭建HelloWorld-搭建服務提供者與服務消費者並完成遠程調用(附代碼下載):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103675259
在上面搭建好Dubbo的HelloWorld後,將其與Springboot項目進行整合。
註:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關註公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載。
實現
還是採用上面用戶服務提供者和訂單服務消費者的模式。
將公共的介面和實體類抽離出來,放在gmall-interface中。
新建服務提供者
打開Eclipse-新建一個Spring Starter Project
點擊Next,輸入相關包名與應用名
點擊next,這裡新建的是服務提供者,只需要簡單的SpringBoot項目,不用選擇Web依賴,直接點擊Next,
註意這裡的SpringBoot的版本為2.2.2,在後面選擇Dubbo-starter依賴時有版本對應關係。
建完之後的目錄為
然後打開pom.xml,添加上面公共介面的依賴以及dubbo-starter的依賴。
引入公共介面依賴,具體實現參照上面博客
<dependency> <groupId>com.badao.gmall</groupId> <artifactId>gmall-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
引入dubbo-starter以及相關依賴
按照其官方指示:https://github.com/apache/dubbo-spring-boot-project
添加相應的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>
參考上面博客搭建服務提供者時配置信息是在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
具體配置信息作用見上面博客。或者參照其官方指示
然後將上面博客搭建好的serviceImpl複製到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 //暴露服務 @Component public class UserServiceImpl implements UserService { public List<UserAddress> getUserAddressList(String userId) { // TODO Auto-generated method stub UserAddress address1 = new UserAddress(1, "霸道流氓氣質", "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;
為了區分spring的@Service註解,所以使用spring的@Componment註解。
dubbo的@Service註解的作用是指定要暴露的服務,讓別人能引用。其作用就是上面在provider.xml中使用dubbo-service標簽
暴露服務一樣
然後在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); } }
啟動提供者主程式
使用上面博客中搭建好的管理平臺可見服務提供者搭建成功
新建服務消費者
參照上面搭建服務提供者的流程,新建服務消費者
此時添加web依賴
此時的pom.xml同樣引入公共介面依賴與dubbo和dubbo-starter相關依賴
<?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>
然後打開其配置文件進行相關配置
server.port=8081 dubbo.application.name=boot-order-service-consumer dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.monitor.protocol=registry
註意:這裡修改埠號是因為8080已經被dubbo-monitor所占用,
具體配置的作用參照上面博客。
然後將服務消費者的介面實現複製過來
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、將服務提供者註冊到註冊中心(暴露服務) * 1)、導入dubbo依賴(2.6.2)\操作zookeeper的客戶端(curator) * 2)、配置服務提供者 * * 2、讓服務消費者去註冊中心訂閱服務提供者的服務地址 * @author badao * */ @Service public class OrderServiceImpl implements OrderService { //@Autowired @Reference UserService userService; public List<UserAddress> initOrder(String userId) { // TODO Auto-generated method stub System.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的服務,自己能從服務註冊中心發現。
其作用相當於之前的consumer.xml中使用dubbo:reference標簽聲明需要調用的遠程服務介面,即生成遠程服務代理。
在包下新建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 { @Autowired OrderService 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); } }
啟動應用主程式
此時在管理平臺就會監控到服務消費者
然後打開瀏覽器,輸入:localhost:8081/initOrder?uid=1
示例代碼下載
https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12052055