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

来源:https://www.cnblogs.com/badaoliumangqizhi/archive/2019/12/23/12089140.html
-Advertisement-
Play Games

場景 Dubbo簡介與基本概念: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103555224 Dubbo環境搭建-ZooKeeper註冊中心: https://blog.csdn.net/BADAO_LIUMANG_QIZH ...


場景

Dubbo簡介與基本概念:

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

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

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

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

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

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

註:

博客:
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、將服務提供者註冊到註冊中心(暴露服務)
 *   1)、導入dubbo依賴(2.6.2)\操作zookeeper的客戶端(curator)
 *   2)、配置服務提供者
 * 
 * 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);
  //1、查詢用戶的收貨地址
  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入門代碼


 

 


 


 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 案例:旋轉木馬 頁面載入時候出現的效果,script標簽寫在head裡面,body上面 顯示一個圖片散開的動畫,遍歷之後,把每個圖片用封裝的動畫函數移動到指定目標(同時改變多屬性:寬,透明度,層級,top, left) 在做左右按鈕點擊事件。 右邊按鈕,用數組裡面的push和shift,數組第一個去 ...
  • 在項目運行過程中發現,用戶在有左右滑動前進後退的功能的瀏覽器上簽字時,偶然觸發了前進後退會導致canvas像是重置了一樣內容消失,所以需要在代碼中處理這種情況。 基本原理就是在touchmove事件中阻止預設事件,使瀏覽器不會觸發前進後退事件,但是也會無法觸發scroll事件讓頁面正常滾動,後續如何 ...
  • 循序漸進,看看只使用 CSS ,可以鼓搗出什麼樣的充電動畫效果。 畫個電池 當然,電池充電,首先得用 CSS 畫一個電池,這個不難,隨便整一個: 歐了,勉強就是它了。有了電池,那接下來直接充電吧。最最簡單的動畫,那應該是用色彩把整個電池灌滿即可。 方法很多,代碼也很簡單,直接看效果: 有內味了,如果 ...
  • 樣式操作模塊可用於管理DOM元素的樣式、坐標和尺寸,本節講解一下尺寸這一塊 jQuery通過樣式操作模塊里的尺寸相關的API可以很方便的獲取一個元素的寬度、高度,而且可以很方便的區分padding、border、 margin等,主要有六個API,如下: heihgt(size)、width(siz ...
  • Proise實例的then方法是定義在原型對象Promise.prototype上的,它的作用是為Promise實例添加狀態改變時的回調函數。 該方法可以接收兩個回調函數作為參數,其中第二個回調函數是可選的。第一個回調函數是 對象的狀態變為 時調用,第二個回調函數是 對象的狀態變為 時調用。 下麵從 ...
  • 一、finally語句塊 1.註意點: (1)finally語句塊可以直接和try語句塊聯合使用。try...finally.... (2)try.....catch.....finally也可以執行; (3)在finally語句塊中的代碼是一定會執行的。 package com.bjpowerno ...
  • package main import ( "fmt" "reflect" ) type BinaryNode struct { Data interface{} //數據 lChild *BinaryNode //左子樹 rChild *BinaryNode //右子樹 } //創建二叉樹 fun... ...
  • 1. 冒泡排序(bubble sort)的基本思想:比較相鄰兩個 元素的關鍵字值,如果反序,則交換 2. 快速排序 快速排序(quick sort)是一種分區交換排序演算法. 它的基本思想:在數據序列中選擇一個值作為比較的基準值, 每趟從數據序列的兩端開始交替進行,將小於基準值的元素交換到序列前端,將 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...