上次springboot集成dubbo寫過一個簡單的demo調用的方式,因爲spring版本的問題,此次使用duboox,徹底基於註解的方式。java
dubbox 地址:https://github.com/dangdangdotcom/dubboxgit
因爲dubbox沒有發佈到maven中央倉庫,須要本身安裝到本地maven庫或者私庫github
git clone https://github.com/dangdangdotcom/dubboxspring
在dubbox目錄執行mvn install -Dmaven.test.skip=true來嘗試編譯一下dubbo(並將dubbo的jar安裝到本地maven庫)apache
1.導入maven依賴springboot
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.7</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency>
2.配置dubboapp
dubbo.xml maven
<?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-3.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"default-lazy-init="false"> <dubbo:protocol name="dubbo" host="${dubbo.protocol.dubbo.host}" port="${dubbo.protocol.dubbo.port}" charset="UTF-8" threadpool="fixed" threads="500" /> <dubbo:application name="${dubbo.application.name}"></dubbo:application> <dubbo:registry address="${dubbo.registry.address}" check="true" username="${dubbo.admin.username}" password="${dubbo.admin.password}"></dubbo:registry> <dubbo:provider timeout="6000" /> //表示爲提供者 <dubbo:consumer timeout="6000" check="false"/>//表示爲消費者 check是檢查是否有對應的服務提供 <dubbo:annotation package="com.example" /> </beans>
.propertieside
#服務名 dubbo.application.name=dubbo-01 #註冊的組 dubbo.registry.group=test2 #註冊服務的域名 dubbo.protocol.dubbo.host= #註冊服務的端口 dubbo.protocol.dubbo.port=-1 #zookeeper地址 dubbo.registry.address=zookeeper://:2181 #用戶名 dubbo.admin.username=dubbo #密碼 dubbo.admin.password=
3.建立接口 spa
package com.example.dubbo; /** * Created by 王念 on 2016/4/21. */ public interface Iservice { String test(); }
該接口提供者和消費者都須要依賴。 提供者實現該方法,消費者調用該方法
4.提供者實現
package com.example.dubbo; import com.alibaba.dubbo.config.annotation.Service; //使用該註解註冊dubbo服務 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; /** * Created by 王念 on 2016/4/21. */ @Service @EnableAutoConfiguration public class ServiceImpl implements Iservice { @Override public String test() { System.out.printf("RPC調用成功"); return "RPC調用成功"; } }
4.消費者調用
package com.example; import com.alibaba.dubbo.config.annotation.Reference; import com.example.dubbo.Iservice; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = DemoApplication.class) public class DemoApplicationTests { @Reference//使用該註解注入服務 Iservice iservice; @Test public void contextLoads() { System.out.println(iservice.test());//調用 } }
5.main方法
@SpringBootApplication @ImportResource("classpath:/dubbo-provider.xml")//啓動加在dubbo配置文件 public class DemoApplication{ public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); try { System.out.println("啓動成功,按任意鍵關閉"); System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
本文地址(防爬蟲不標註來源):http://my.oschina.net/wangnian/blog/665299