Dubbo入門教程

服務端(dubbo-server)

1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lwb</groupId><artifactId>dubbo-server</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- spring begin --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.6.RELEASE</version></dependency><!-- spring end --><!-- dubbo begin --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version></dependency><!-- dubbo end --><!-- 注冊中心zookeeper begin --><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.3.6</version></dependency><!-- 注冊中心zookeeper end --><!-- log begin --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.15</version><exclusions><exclusion><groupId>com.sun.jdmk</groupId><artifactId>jmxtools</artifactId></exclusion><exclusion><groupId>com.sun.jmx</groupId><artifactId>jmxri</artifactId></exclusion><exclusion><artifactId>jms</artifactId><groupId>javax.jms</groupId></exclusion><exclusion><artifactId>mail</artifactId><groupId>javax.mail</groupId></exclusion></exclusions></dependency><!-- log end --><!-- other begin --><dependency><groupId>org.jboss.netty</groupId><artifactId>netty</artifactId><version>3.2.0.Final</version></dependency><dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>0.8</version></dependency><!-- other end --></dependencies>
</project>

2. dubbo.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:application name="dubbo-server" owner="lwb" /><!-- zookeeper注冊中心 --><dubbo:registry address="zookeeper://127.0.0.1:2181" /><dubbo:protocol contextpath="dubbo" port="20881" /><dubbo:monitor protocol="registry"/><!-- 服務具體實現 --><bean id="elasticService" class="com.lwb.service.impl.ElasticServiceImpl" /><!-- 向注冊中心注冊暴漏服務地址,注冊服務 --><dubbo:service interface="com.lwb.service.ElasticService" ref="elasticService" protocol="dubbo" timeout="50000"/>

3. 代碼

package com.lwb.service;/*** @author liuweibo* @date 2018/5/9*/
public interface ElasticService {String helloDubbo(String msg);
}
package com.lwb.service.impl;import com.lwb.service.ElasticService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;/*** @author liuweibo* @date 2018/5/9*/
public class ElasticServiceImpl implements ElasticService {private static final Logger LOGGER = LoggerFactory.getLogger(ElasticServiceImpl.class);public String helloDubbo(String msg) {System.out.println(msg);return "hi!" + msg;}
}

4. 項目結構

在這里插入圖片描述

5. 啟動類(Application.java)

這里就直接用main方法啟動了

package com.lwb;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;/*** @author liuweibo* @date 2018/5/9*/
public class Application {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "dubbo.xml" });context.start();System.out.println("任意按鍵退出");System.in.read();context.close();}
}

客戶端(dubbo-client)

1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lwb</groupId><artifactId>dubbo-client</artifactId><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- spring begin --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.6.RELEASE</version></dependency><!-- spring end --><!-- dubbo begin --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version></dependency><!-- dubbo end --><!-- 注冊中心zookeeper begin --><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.3.6</version></dependency><!-- 注冊中心zookeeper end --><!-- log begin --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.15</version><exclusions><exclusion><groupId>com.sun.jdmk</groupId><artifactId>jmxtools</artifactId></exclusion><exclusion><groupId>com.sun.jmx</groupId><artifactId>jmxri</artifactId></exclusion><exclusion><artifactId>jms</artifactId><groupId>javax.jms</groupId></exclusion><exclusion><artifactId>mail</artifactId><groupId>javax.mail</groupId></exclusion></exclusions></dependency><!-- log end --><!-- other begin --><dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>0.8</version></dependency><dependency><groupId>com.lwb</groupId><artifactId>dubbo-server</artifactId><version>1.0-SNAPSHOT</version></dependency><!-- other end --></dependencies></project>

2. dubbo.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:application name="consumer-of-dubbo-demo" /><dubbo:registry address="zookeeper://127.0.0.1:2181" /><!-- 向注冊中心訂閱服務 --><dubbo:reference id="elasticService" interface="com.lwb.service.ElasticService" /></beans>

3. 服務調用(這里為了簡單,就在main方法里面處理了哈,Application.java)

package com.lwb;import com.lwb.service.ElasticService;
import com.lwb.service.impl.ElasticServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author liuweibo* @date 2018/5/9*/
public class Application {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "dubbo.xml" });context.start();ElasticService service = (ElasticService) context.getBean("elasticService");System.out.println(service.helloDubbo("world"));context.close();}
}

4. 項目結構

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/249687.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/249687.shtml
英文地址,請注明出處:http://en.pswp.cn/news/249687.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

NSAssert和NSParameterAssert

2016.05.05 18:34* 字數 861 閱讀 5127評論 0喜歡 17https://www.jianshu.com/p/3072e174554fNSAssert和NSParameterAssert在開發環境中經常被使用&#xff0c;調試和驗證代碼參數的完整性&#xff0c;斷言為真&#xff0c;則表明程序運行正常&#xff0c;而斷言為假&#xff0…

【PAT】B1070 結繩(25 分)

此題太給其他25分的題丟人了&#xff0c;只值15分 注意要求最終結果最長&#xff0c;而且向下取整 #include<stdio.h> #include<algorithm> using namespace std; float arr[10005]; int main(){int N;scanf("%d",&N);for(int i0;i<N;i)//輸入數據…

Java代碼實現負載均衡五種算法

前言&#xff1a; 負載均衡是為了解決并發情況下&#xff0c;多個請求訪問&#xff0c;把請求通過提前約定好的規則轉發給各個server。其中有好幾個種經典的算法。在用java代碼編寫這幾種算法之前&#xff0c;先來了解一下負載均衡這個概念。 1.概念 負載&#xff0c;從字面…

使用Nodejs發送郵件

嘗試用了Nodemailer來發送郵件&#xff0c;結果成功了&#xff0c;雖然是相對比較簡單的&#xff0c;但還是記錄一下吧。 Nodemailer 是 Node.js 應用程序的一個模塊&#xff0c;可以方便地發送電子郵件。 使用 # 初始化 pageage.json 文件 $ npm init # 安裝依賴 $ npm ins…

HTTP同源策略

同源策略是web安全策略中的一種&#xff0c;非常重要。 同源策略明確規定&#xff1a;不同域的客戶端在沒有明確授權的情況下&#xff0c;不能讀寫對方的資源。 簡單說來就是web瀏覽器允許第一個頁面的腳本訪問訪問第二個頁面的數據&#xff0c;但是也只有在兩個頁面有相同的…

Spring Cloud 微服務架構

一、分布式服務框架的發展 1.1 第一代服務框架   代表&#xff1a;Dubbo(Java)、Orleans(.Net)等 特點&#xff1a;和語言綁定緊密 1.2 第二代服務框架   代表&#xff1a;Spring Cloud等 現狀&#xff1a;適合混合式開發&#xff08;例如借助Steeltoe OSS可以讓ASP.Ne…

JZOJ 4421. aplusb

4421. aplusb Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Goto ProblemSetDescription SillyHook要給小朋友出題了&#xff0c;他想&#xff0c;對于初學者&#xff0c;第一題肯定是ab 啊&#xff0c;但當他出完數據后神奇地發現.in不見了&#xff0c…

跨域資源共享CORS詳解

最近深入了解了CORS的相關東西&#xff0c;覺得阮一峰老師的文章寫得最詳細易懂了&#xff0c;所有轉載作為學習筆記。 原文地址&#xff1a;跨域資源共享 CORS 詳解 CORS是W3C的一個標準&#xff0c;全稱是跨域資源共享&#xff08;Cross-origin resource sharing&#xff0…

計算機網絡(十),HTTP的關鍵問題

目錄 1.在瀏覽器地址欄鍵入URL&#xff0c;按下回車之后經歷的流程 2.HTTP狀態碼 3.GET請求和POST請求的區別 4.Cookie和Session的區別 5.IPV4和IPV6 十、HTTP的關鍵問題 1.在瀏覽器地址欄鍵入URL&#xff0c;按下回車之后經歷的流程 &#xff08;1&#xff09;DNS解析 &#x…

云技術

云技術是指在廣域網或局域網內將硬件、軟件、網絡等系列資源統一起來&#xff0c;實現數據的計算、儲存、處理和共享的一種托管技術。

vue中 mock使用教程

//mock/index.js import Mock from mockjs //引入mockjs&#xff0c;npm已安裝 import { Random,toJSONSchema } from mockjs // 引入random對象,隨機生成數據的對象&#xff0c;&#xff08;與占位符一樣&#xff09; Mock.setup({timeout:1000 //設置請求延時時間 }) const …

前端開發掌握nginx常用功能之rewrite

上一篇博文對nginx最常用功能的server及location的匹配規則進行了講解&#xff0c;這也是nginx實現控制訪問和反向代理的基礎。掌握請求的匹配規則算是對nginx有了入門&#xff0c;但是這些往往還是不能滿足實際的需求場景&#xff0c;例如請求url重寫、重定向等等&#xff0c;…

vue2.0腳手架的webpack 配置文件分析

前言 作為 Vue 的使用者我們對于 vue-cli 都很熟悉&#xff0c;但是對它的 webpack 配置我們可能關注甚少&#xff0c;今天我們為大家帶來 vue-cli#2.0 的 webpack 配置分析 vue-cli 的簡介、安裝我們不在這里贅述&#xff0c;對它還不熟悉的同學可以直接訪問 vue-cli 查看 …

一個可供中小團隊參考的微服務架構技術棧

一個可供中小團隊參考的微服務架構技術棧

WinSxS文件夾瘦身

WinSxS文件夾瘦身2014-5-8 18:03:32來源&#xff1a;IT之家作者&#xff1a;阿象責編&#xff1a;阿象 評論&#xff1a;27剛剛&#xff0c;我們分享了如何用DISM管理工具查看Win8.1 WinSxS文件夾實際大小。對于WinSxS文件夾&#xff0c;幾乎每個Windows愛好者都認識到其重要性…

bcrypt的簡單使用

前段時間在搗鼓個人項目的時候用到了nodejs做服務端&#xff0c;發現使用加密的方法和之前常用的加密方式不太一致&#xff0c;下面以demo的形式總結一下bcrypt對密碼進行加密的方法。 一、簡介 Bcrypt簡介&#xff1a; bcrypt是一種跨平臺的文件加密工具。bcrypt 使用的是布…

盒子居中

1、未脫標 margin&#xff1a;0 auto&#xff1b; 2、脫標&#xff08;absolute、fixed&#xff09; left&#xff1a;50%&#xff1b; margin-left&#xff1a;width/2&#xff1b; 轉載于:https://www.cnblogs.com/liujianing/p/10356984.html

織夢無子欄目時禁止調用同級欄目

1. 修改文件 \include\taglib\channel.lib.php 把代碼 if($typeson && $reid!0 && $totalRow0) 改為 if($typeson && $reid!0 && $totalRow0 && $noself) 2. 使用channel標簽時添加noself屬性 {dede:channel noselfyes} {/dede:channe…

nodejs實現文件上傳

前段時間在做個人項目的時候&#xff0c;用到了nodejs服務端上傳文件&#xff0c;現在回頭把這個小結一下&#xff0c;作為記錄。 本人上傳文件時是基于express的multiparty&#xff0c;當然也可以使用connect-multiparty中間件實現&#xff0c;但官方似乎不推薦使用connect-m…