文章出處

github上有一個開源項目spring-boot-starter-dubbo 提供了spring-boot與dubbo的集成功能,直接拿來用即可。(記得給作者點贊,以示感謝!)

下面是使用步驟,先看下工程的大致結構:

一、引入相關的依賴項

 1 subprojects {
 2     buildscript {
 3         ext {
 4             springBootVersion = '1.5.3.RELEASE'
 5         }
 6         repositories {
 7             mavenLocal()
 8             maven {
 9                 url "http://maven.aliyun.com/nexus/content/groups/public/"
10             }
11             mavenCentral()
12         }
13         dependencies {
14             classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
15         }
16     }
17 
18     apply plugin: "java"
19     apply plugin: "maven"
20     apply plugin: 'idea'
21 
22 
23     targetCompatibility = 1.8
24     sourceCompatibility = 1.8
25 
26     repositories {
27         mavenLocal()
28         maven {
29             url "http://maven.aliyun.com/nexus/content/groups/public/"
30         }
31         mavenCentral()
32     }
33 
34     configurations.all {
35         resolutionStrategy.cacheChangingModulesFor 1, "minutes"
36     }
37 
38     dependencies {
39         compile('io.dubbo.springboot:spring-boot-starter-dubbo:1.0.0')
40         compile('org.springframework.boot:spring-boot-starter-web:1.5.3.RELEASE')
41     }
42 }
View Code

這是最外層根目錄下的build.gradle,關鍵地方就是最后dependencies引入的2個依賴項

 

二、service-api中定義接口

1 package com.cnblogs.yjmyzz.service.api;
2 
3 /**
4  * Created by 菩提樹下的楊過(http:/yjmyzz.cnblogs.com) on 2017/5/21.
5  */
6 public interface DemoService {
7     String hello(String nickName);
8 }
View Code

這一步平淡無奇,沒什么好說的

 

三、service-provider

3.1 提供接口實現

 1 package com.cnblogs.yjmyzz.service.impl;
 2 
 3 import com.alibaba.dubbo.config.annotation.Service;
 4 import com.cnblogs.yjmyzz.service.api.DemoService;
 5 import org.slf4j.Logger;
 6 import org.slf4j.LoggerFactory;
 7 
 8 /**
 9  * Created by 菩提樹下的楊過(http:/yjmyzz.cnblogs.com) on 2017/5/21.
10  */
11 @Service(version = "1.0.0")
12 public class DemoServiceImpl implements DemoService {
13 
14     Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
15 
16     public String hello(String nickName) {
17         logger.info(nickName + " call me!");
18         return String.format("hi , %s!", nickName);
19     }
20 }
View Code

常規套路,不用多說

3.2 編寫ServiceProvider主類

 1 package com.cnblogs.yjmyzz.service;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 /**
 7  * Created by yangjunming on 2017/5/21.
 8  */
 9 @SpringBootApplication
10 public class ServiceProvider {
11     public static void main(String[] args) {
12         SpringApplication.run(ServiceProvider.class, args);
13     }
14 }
View Code

仍然是spring-boot的經典套路,跟dubbo也沒任何關系

3.3 application.yml配置

 1 server:
 2   port: 8001
 3 
 4 spring:
 5   dubbo:
 6     scan: com.cnblogs.yjmyzz.service
 7     application:
 8       name: provider
 9     registry:
10       address: zookeeper://127.0.0.1:2181
11     protocol:
12       name: dubbo
13       port: 20880

這里是重點,指定了dubbo服務提供方啟動所需的zk注冊地址,協議類型及端口,包括掃描的包。

 

四、service-consumer

4.1 定義一個輔助用的Proxy

 1 package com.cnblogs.yjmyzz.service.proxy;
 2 
 3 import com.alibaba.dubbo.config.annotation.Reference;
 4 import com.cnblogs.yjmyzz.service.api.DemoService;
 5 import org.springframework.stereotype.Component;
 6 
 7 /**
 8  * Created by yangjunming on 2017/5/21.
 9  */
10 @Component
11 public class ServiceProxy {
12 
13     @Reference(version = "1.0.0")
14     public DemoService demoService;
15 }

就是一個標準的spring組件(不管是叫proxy還是叫container都無所謂,隨個人喜好),在該組件中持有對Service的引用實例,注意:如果指定了version,則該版本號要與service-provider中的版本號一致

4.2 調用服務

 1 package com.cnblogs.yjmyzz.service;
 2 
 3 import com.cnblogs.yjmyzz.service.proxy.ServiceProxy;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.context.ConfigurableApplicationContext;
 7 
 8 /**
 9  * Created by 菩提樹下的楊過(http:/yjmyzz.cnblogs.com) on 2017/5/21.
10  */
11 @SpringBootApplication
12 public class ServiceConsumer {
13 
14     public static void main(String[] args) {
15         ConfigurableApplicationContext ctx = SpringApplication.run(ServiceConsumer.class, args);
16         ServiceProxy proxy = ctx.getBean(ServiceProxy.class);
17         System.out.println(proxy.demoService.hello("菩提樹下的楊過"));//調用服務
18     }
19 }
View Code

一看即明,不多解釋。

4.3 application.yml配置

 1 server:
 2   port: 8002
 3 
 4 spring:
 5   dubbo:
 6     scan: com.cnblogs.yjmyzz.service
 7     application:
 8       name: consumer
 9     registry:
10       address: zookeeper://127.0.0.1:2181

ok,搞定!

 

上述示例源代碼,已托管至github,有需要的朋友自行下載:https://github.com/yjmyzz/spring-boot-dubbo-demo


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()