Dubbo 基礎篇 (一) - hello world

開發(fā)環(huán)境
Dubbo 架構
  • 服務提供方
  • 服務消費方
  • 注冊中心 (使用zookeeper作為本例注冊中心)
  • 監(jiān)控中心
準備工作

為了更好的理解Dubbo的工作流程疯暑,我們先下載dubbo源碼和管理后臺源碼

# 下載dubbo源碼
git clone git@github.com:freeseawind/incubator-dubbo.git

# 下載管理后臺源碼
git clone https://github.com/dubbo/dubbo-ops.git 

使用以下命令分別做一次構建,然后我們可以和好基友happy的去玩兩把游戲等待編譯結束

mvn clean install -Dmaven.test.skip

N分鐘后,自從Dubbo “高富帥“ 以后茶没,這個構建流程就越來越麻麻dei(慢)了登下。好吧匠童,這咱都能忍巩趁,但是what竟然編譯失敗栈幸,這簡直不能忍撰茎。

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.
6.0:testCompile (default-testCompile) on project dubbo-config-spring: Compilatio
n failure -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureExc
eption
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command

[ERROR]   mvn <goals> -rf :dubbo-config-spring

嗯嵌牺、啊、唉龄糊,研究了半天還是無果逆粹,決定先跳過dubbo-config-spring的編譯,如果有知道的朋友歡迎告知本菜 (T T)炫惩。

編碼時刻

我們使用spring boot 2.x + dubbo 來作為本次學習的例子

使用xml配置方式見官方DEMO, 這里演示如何在spring boot 2中運行Dubbo 的 hello world

1. 創(chuàng)建Maven項目
├── copycat-dubbo
│   ├── pom.xml
│   └── copycat-dubbo-example
│       └── src/main/java
│       └── src/main/resource
│           └── dubbo.properties
│           └── log4j.properties
│           └── application-customer.properties
│           └── application.properties
│       └──  pom.xml

copycat-dubbo項目pom文件部分配置如下:

  <!-- 以上部分忽略-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.2</version>
            </dependency>
                        <!-- zookeeper依賴包 -->
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-client</artifactId>
                <version>4.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.0.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

copycat-dubbo-example項目pom文件部分配置如下:

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
2. 定義服務接口和實現(xiàn)
定義服務接口
public interface DemoService
{
    String sayHello(String name);
}
定義接口實現(xiàn)
@Service(timeout = 5000)
public class DemoServiceImpl implements DemoService
{
    @Override
    public String sayHello(String name)
    {
        return "Hello " + name;
    }
}

細心的朋友會發(fā)現(xiàn)在接口的實現(xiàn)上僻弹,增加了@Service(timeout = 5000)的注解,這個注解的主要作用是暴露和注冊服務他嚷。

3. 服務消費者
服務消費者配置
@Configuration
public class ConsumerConf
{
    @Bean
    public ApplicationConfig applicationConfig()
    {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("consumer-test");
        return applicationConfig;
    }

    /**
     * 服務消費者配置
     * 
     * @author freeseawind
     * @return
     */
    @Bean
    public ConsumerConfig consumerConfig()
    {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setTimeout(3000);
        return consumerConfig;
    }

    /**
     * 注冊中心配置
     * 
     * @author freeseawind
     * @return
     */
    @Bean
    public RegistryConfig registryConfig()
    {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        return registryConfig;
    }
}

這里的配置信息只是把xml替換成API的方式蹋绽,通過Spring的IOC容器對Bean進行管理。詳見API 參考手冊schema 配置參考手冊

消費者啟動類
@SpringBootApplication
@Controller
@DubboComponentScan(basePackages = "github.freeseawind.springboot.helloworld.consumer")
public class ConsumerApplication
{
    @Reference
    private DemoService demoService;
    
    public static void main(String[] args)
    {
        SpringApplication.run(ConsumerApplication.class, args);
    }
    
    @RequestMapping("/a")
    @ResponseBody
    public String sayHello()
    {
        return demoService.sayHello("freeseawind");
    }
}

@DubboComponentScan 是Dubbo自定義bean加載的擴展筋蓖。它在消費方主要的作用是卸耘,掃描需要注入的服務接口(該服務接口以@Reference注解標識),同時向注冊中心發(fā)起注冊消費者服務請求粘咖。

3. 服務提供者
服務提供者配置
@Configuration
public class ProviderConf
{
    @Bean
    public ApplicationConfig applicationConfig()
    {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("provider-test");
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig()
    {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        return registryConfig;
    }
    
    @Bean
    public ProtocolConfig protocolConfig()
    {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setPort(20881);
        protocolConfig.setName("dubbo");
        return protocolConfig;
    }
}
服務提供者啟動類
@SpringBootApplication
@EnableAutoConfiguration
@DubboComponentScan(basePackages = "github.freeseawind.service.impl")
public class ProviderApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

@DubboComponentScan 掃描的是服務接口的實現(xiàn)(該實現(xiàn)以@Service注解標識)蚣抗,同時向注冊中心發(fā)起注冊服務提供者請求。

順序啟動服務
  • 注冊中心
  • 服務提供者
  • 服務消費者
  • 管理后臺
1. 啟動注冊中心

2. 啟動服務提供者

截取部分啟動日志如下

[DUBBO] Export dubbo service github.freeseawind.service.DemoService to url dubbo://169.254.63.31:20881/
[DUBBO] Start NettyServer bind /0.0.0.0:20881, export /169.254.63.31:20881
[DUBBO] Subscribe: provider://169.254.63.31:20881/github.freeseawind.service.DemoService?
3. 啟動服務消費者

配置啟動參數(shù)


啟動參數(shù)配置

截取部分啟動日志如下

[DUBBO] Subscribe: consumer://169.254.63.31/github.freeseawind.service.DemoService?
[DUBBO] Successed connect to server /169.254.63.31:20881 from NettyClient 169.254.63.31
[DUBBO] Start NettyClient xiaolong-PC/169.254.63.31 connect to the server /169.254.63.31:20881

從啟動日志中我們可以看到消費者和服務提供者建立了連接

4. 啟動管理后臺
管理后臺工程結構

訪問 http://localhost:7001/
用戶名:root 密碼:root

相關配置在application.properties中可找到

我們可以看到服務提供者和消費者均已經(jīng)注冊成功瓮下,并可在管理后臺查看

服務提供者
服務消費者
驗證
請求服務

下一篇:Dubbo 基礎篇 - 配置和目錄

Github工程地址

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末翰铡,一起剝皮案震驚了整個濱河市钝域,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌两蟀,老刑警劉巖网梢,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異赂毯,居然都是意外死亡战虏,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進店門党涕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來烦感,“玉大人,你說我怎么就攤上這事膛堤∈秩ぃ” “怎么了?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵肥荔,是天一觀的道長绿渣。 經(jīng)常有香客問我,道長燕耿,這世上最難降的妖魔是什么中符? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮誉帅,結果婚禮上淀散,老公的妹妹穿的比我還像新娘。我一直安慰自己蚜锨,他們只是感情好档插,可當我...
    茶點故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著亚再,像睡著了一般郭膛。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上针余,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天饲鄙,我揣著相機與錄音,去河邊找鬼圆雁。 笑死忍级,一個胖子當著我的面吹牛,可吹牛的內容都是我干的伪朽。 我是一名探鬼主播轴咱,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了朴肺?” 一聲冷哼從身側響起窖剑,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎戈稿,沒想到半個月后西土,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡鞍盗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年需了,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片般甲。...
    茶點故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡肋乍,死狀恐怖,靈堂內的尸體忽然破棺而出敷存,到底是詐尸還是另有隱情墓造,我是刑警寧澤,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布锚烦,位于F島的核電站觅闽,受9級特大地震影響,放射性物質發(fā)生泄漏涮俄。R本人自食惡果不足惜谱煤,卻給世界環(huán)境...
    茶點故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望禽拔。 院中可真熱鬧,春花似錦室叉、人聲如沸睹栖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽野来。三九已至,卻和暖如春踪旷,著一層夾襖步出監(jiān)牢的瞬間曼氛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工令野, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留舀患,地道東北人。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓气破,卻偏偏與公主長得像聊浅,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,107評論 2 356