在上一篇帖子的基礎上,開始使用dubbo來實現RPC調用:
根據dubbo的架構圖可知,需要做以下幾件事情:
1.將服務提供者注冊到注冊中心(暴露服務)
(1)引入dubbo依賴, 這里依賴2.6.2版本(版本如果使用zookeeper作為注冊中心,那么對應的客戶端是curator,不是原來的zkClient)
(2)注冊中心使用的是zookeeper,需要引入操作zookeeper的客戶端 2.6.以上版本的dubbo,如果使用zookeeper作為注冊中心,那么注冊中心客戶端使用的是curator,2.6版本之前的dubbo,是使用zkClient操作zookeeper注冊中心
(3)配置服務提供者:在src/main/resources目錄下創建一個provider.xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> 6 7 <!-- 1.提供方應用信息,用於計算依賴關系;name屬性用來指定當前服務/應用的名字,使用工程名即可 --> 8 <dubbo:application name="user-service-provider" /> 9 10 <!-- 2.指定注冊中心的地址:使用zookeeper作為注冊中心暴露服務地址 下面兩種方式都可以--> 11 <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" /> --> 12 <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> 13 14 <!--3.指定通信規則: 用dubbo協議在20880端口暴露服務 即服務的調用者和服務的提供者之間使用端口20880進行通信 --> 15 <dubbo:protocol name="dubbo" port="20880" /> 16 17 <!--4.暴露服務dubbo:service 聲明需要暴露的服務接口 ref:指向服務的真正實現對象--> 18 <dubbo:service interface="com.lch.test.service.UserService" ref="userService" /> 19 20 <!-- 和本地bean一樣實現服務 --> 21 <bean id="userService" class="com.lch.test.service.impl.UserServiceImpl" /> 22 </beans>
(4)測試服務提供者:
還可以在dubbo管理控制台查看服務提供者的信息:(先要啟動dubbo監控中心)
2.讓服務的消費者去注冊中心訂閱服務提供者的服務地址
(1)order-service-consumer工程引入dubbo依賴:在order-service-consumer工程的pom文件中添加依賴:
(2)配置服務的消費者:在order-service-consumer工程的resources目錄下創建一個consumer.xml文件,添加如下內容:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> 6 7 <!-- 1.消費方應用名--> 8 <dubbo:application name="order-service-consumer" /> 9 10 <!-- 2.指定注冊中心地址 --> 11 <dubbo:registry address="zookeeper://127.0.0.1:2181"/> 12 13 <!-- 生成遠程服務代理dubbo:reference:聲明需要調用的遠程服務的接口 --> 14 <!-- user-service-provider工程里面暴露了一個名為userService的服務,這里要引用這個服務--> 15 <dubbo:reference id="userService" interface="com.lch.test.service.UserService" /> 16 </beans>
3. 服務消費者的實現:
(1)在上一步,order-service-consumer通過dubbo引用了服務提供者暴露的接口userService,那么在orderService的實現類中,就可以使用Spring的注解來注入userService這個bean:
1 package com.lch.test.service.impl; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import com.lch.test.bean.UserAddress; 9 import com.lch.test.service.OrderService; 10 import com.lch.test.service.UserService; 11 12 /** 13 * 1.將服務提供者注冊到注冊中心 2.讓服務消費者去注冊中心訂閱服務提供者的服務地址 14 * 15 * @author 16 */ 17 @Service // 這里暫時使用spring的注解 18 public class OrderServiceImpl implements OrderService { 19 20 /* 21 * 這里dubbo工程里面只是引入了該接口,而該接口的實現在其他工程里面, 這里就需要遠程過程調用才能獲取到該接口的實現 22 */ 23 @Autowired 24 UserService userService; 25 26 public void initOrder(String userId) { 27 System.out.println("用戶id=" + userId); 28 // 調用userService 獲取用戶收貨地址 29 List<UserAddress> addressList = userService.getUserAddressList(userId); 30 addressList.forEach(address -> { 31 System.out.println(address); 32 }); 33 } 34 35 }
這里使用了注解,consumer.xml配置文件中需要掃描包里的注解:
(2)服務的消費者測試:
1 package com.lch.test; 2 3 import java.io.IOException; 4 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 import com.lch.test.service.OrderService; 8 9 public class MainApplication { 10 11 public static void main(String[] args) throws IOException { 12 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:consumer.xml"); 13 OrderService orderService = context.getBean(OrderService.class); 14 orderService.initOrder("1"); 15 System.out.println("調用完成"); 16 System.in.read(); 17 } 18 }
調用結果:
在dubbo管理控制台查看消費者: