Dubbo搭建HelloWorld-搭建服務提供者與服務消費者並完成遠程調用(附代碼下載)

場景

Dubbo簡介與基本概念:html

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103555224java

Dubbo環境搭建-ZooKeeper註冊中心:spring

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103555470apache

Dubbo環境搭建-管理控制檯dubbo-admin實現服務監控:編程

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103624846app

在上面搭建好ZooKeeper註冊中心和管理控制檯dubbo-admin搭建後,實現HelloWorld程序完成基本入門。maven

注:ide

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。微服務

實現

爲了解決依賴問題與實現微服務的概念,下面須要新建三個Maven Project測試

gmall-interface 用來做爲公共依賴,存放公共接口和公共Bean等。

uesr-service-provider 搭建服務提供者

order-service-consumer 搭建服務消費者

新建公共接口程序

打開Eclipse-新建maven Project

 

 

輸入相關座標信息,後面兩個項目過程也是如此。

 

 

 

而後在項目下新建包com.badao.gmall

而後再新建bean包和service包

bean中新建UserAddress實體類

package com.badao.gmall.bean;

import java.io.Serializable;

/**
 * 用戶地址
 * @author badao
 *
 */
public class UserAddress implements Serializable {
 
 private Integer id;
    private String userAddress; //用戶地址
    private String userId; //用戶id
    private String consignee; //收貨人
    private String phoneNum; //電話號碼
    private String isDefault; //是否爲默認地址    Y-是     N-否
    
    public UserAddress() {
  super();
  // TODO Auto-generated constructor stub
 }
    
 public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum,
   String isDefault) {
  super();
  this.id = id;
  this.userAddress = userAddress;
  this.userId = userId;
  this.consignee = consignee;
  this.phoneNum = phoneNum;
  this.isDefault = isDefault;
 }
 
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getUserAddress() {
  return userAddress;
 }
 public void setUserAddress(String userAddress) {
  this.userAddress = userAddress;
 }
 public String getUserId() {
  return userId;
 }
 public void setUserId(String userId) {
  this.userId = userId;
 }
 public String getConsignee() {
  return consignee;
 }
 public void setConsignee(String consignee) {
  this.consignee = consignee;
 }
 public String getPhoneNum() {
  return phoneNum;
 }
 public void setPhoneNum(String phoneNum) {
  this.phoneNum = phoneNum;
 }
 public String getIsDefault() {
  return isDefault;
 }
 public void setIsDefault(String isDefault) {
  this.isDefault = isDefault;
 }
    
   


}

 

而後在service包下新建兩個公共接口類

 

 

OrderService.java

package com.badao.gmall.service;

import java.util.List;

import com.badao.gmall.bean.UserAddress;

public interface OrderService {
 
 /**
  * 初始化訂單
  * @param userId
  */
 public List<UserAddress> initOrder(String userId);

}

該接口聲明一個初始化訂單的方法。

UserService.java

package com.badao.gmall.service;

import java.util.List;

import com.badao.gmall.bean.UserAddress;

/**
 * 用戶服務
 * @author badao
 *
 */
public interface UserService {
 
 /**
  * 按照用戶id返回全部的收貨地址
  * @param userId
  * @return
  */
 public List<UserAddress> getUserAddressList(String userId);

}

 

該接口提供獲取全部收穫地址的方法的聲明

新建服務提供者程序

參照上面流程,新建user-service-provider

而後在pom.xml中引入上面公共接口的依賴

  <dependency>
   <groupId>com.badao.gmall</groupId>
   <artifactId>gmall-interface</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>

 

而後還要引入dubbo以及zookeeper的依賴

 

 <!-- 引入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>

 

完整pom.xml代碼

<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.badao.gmall</groupId>
  <artifactId>user-service-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  <dependency>
   <groupId>com.badao.gmall</groupId>
   <artifactId>gmall-interface</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </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>
 </dependencies>
</project>

 

在項目下新建service.impl包,包下新建實現類

package com.badao.gmall.service.impl;

import java.util.Arrays;
import java.util.List;

import com.badao.gmall.bean.UserAddress;
import com.badao.gmall.service.UserService;

 

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);
 }

}

 

而後按照其官方指示,新建配置文件並配置服務提供者

 

 

在src/main/resource下新建配置文件provider.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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
  http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

 <!-- 1、指定當前服務/應用的名字(一樣的服務名字相同,不要和別的服務同名) -->
 <dubbo:application name="user-service-provider"></dubbo:application>
 
 <!-- 2、指定註冊中心的位置 兩種方式 -->
 <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
 <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
 
 <!-- 3、指定通訊規則(通訊協議?通訊端口) -->
 <dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
 
 <!-- 4、暴露服務   ref:指向服務的真正的實現對象 -->
 <dubbo:service interface="com.badao.gmall.service.UserService" 
  ref="userServiceImpl" timeout="1000" version="1.0.0">
  <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
 </dubbo:service>
 
 <!--統一設置服務提供方的規則  -->
 <dubbo:provider timeout="1000"></dubbo:provider>
 
 <bean id="userServiceImpl" class="com.badao.gmall.service.impl.UserServiceImpl"></bean>
 
</beans>

 

而後在com.badao.gmall包下新建MainApplication.java

讀取配置文件並暴露服務。

package com.badao.gmall;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApplication {
 
 public static void main(String[] args) throws IOException {
  ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
  ioc.start();
  System.in.read();
 }

}

 

搭建服務消費者程序

參照上面流程新建order-service-consumer服務消費者程序

在pom.xml中添加公共接口依賴以及dubbo和zookeeper相關依賴

<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.badao.gmall</groupId>
  <artifactId>user-service-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  <dependency>
   <groupId>com.badao.gmall</groupId>
   <artifactId>gmall-interface</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </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>
 </dependencies>
</project>

 

而後新建接口實現類OrderServiceImpl

package com.badao.gmall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.badao.gmall.bean.UserAddress;
import com.badao.gmall.service.OrderService;
import com.badao.gmall.service.UserService;

/**
 * 一、將服務提供者註冊到註冊中心(暴露服務)
 *   1)、導入dubbo依賴(2.6.2)\操做zookeeper的客戶端(curator)
 *   2)、配置服務提供者
 * 
 * 二、讓服務消費者去註冊中心訂閱服務提供者的服務地址
 * @author badao
 *
 */
@Service
public class OrderServiceImpl implements OrderService {

 @Autowired
 UserService userService;
 public List<UserAddress> initOrder(String userId) {
  // TODO Auto-generated method stub
  System.out.println("用戶id:"+userId);
  //一、查詢用戶的收貨地址
  List<UserAddress> addressList = userService.getUserAddressList(userId);
  for (UserAddress userAddress : addressList) {
   System.out.println(userAddress.getUserAddress());
  }
  return addressList;
 }
 
 

}

 

按照官方指示,一樣新建服務消費者配置文件consumer.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://dubbo.apache.org/schema/dubbo"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
  http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
  http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 <context:component-scan base-package="com.badao.gmall.service.impl"></context:component-scan>


 <dubbo:application name="order-service-consumer"></dubbo:application>
 
 <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
 
 
 
 <!--聲明須要調用的遠程服務的接口;生成遠程服務代理  -->
 <!-- 
  1)、精確優先 (方法級優先,接口級次之,全局配置再次之)
  2)、消費者設置優先(若是級別同樣,則消費方優先,提供方次之)
 -->
 <dubbo:reference interface="com.badao.gmall.service.UserService" 
  id="userService" timeout="5000" retries="3" version="*">
 </dubbo:reference>
  
</beans>

 

而後新建MainApplication,加載Spring配置,並調用遠程服務

package com.badao.gmall;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.badao.gmall.service.OrderService;

public class MainApplication {
 
 @SuppressWarnings("resource")
 public static void main(String[] args) throws IOException {
  ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
  OrderService orderService = applicationContext.getBean(OrderService.class);
  orderService.initOrder("1");
  System.out.println("調用完成....");
  System.in.read();
 }

}

 

開始測試

參照官網快速搭建入門程序的指導:

http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

搭建好服務提供者和服務消費者以後,參照上面博客搭建好zookeeper服務註冊中心後,啓動ZooKeeper服務端。

而後再參照博客搭建dubbo-admin管理控制檯程序。

啓動服務提供者的MainApplication使服務暴露。

此時刷新管理控制檯,可見此時服務提供者已經暴露。

 

 

而後運行服務消費者的MainApplication,遠程調用成功。

 

 

此時刷新管理控制檯

 

 

服務提供者與服務消費者各一。

代碼下載

關注公衆號:

霸道的程序猿

回覆:

Dubbo入門代碼

相關文章
相關標籤/搜索