生活随笔
收集整理的這篇文章主要介紹了
最简单的dubbo教程-快速入门
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
所需環(huán)境
zookeeper作為dubbo的注冊中心,dubbo服務(wù)提供方和消費(fèi)方都需要在zookeeper注冊中心注冊。
可參考:http://blog.csdn.net/jingyangv587/article/details/78901508
注意:啟動后,請勿關(guān)閉!
開始搭建
###1. 服務(wù)提供方和消費(fèi)方都需要的包(這里我新建的maven工程為pom工程,將共同的項目依賴寫到pom.xml中)
-
總的項目結(jié)構(gòu)為
-
pom.xml文件內(nèi)容為
<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.test</groupId><artifactId>dubbo-demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><properties><motan.version>0.3.0</motan.version><!-- 在阿里巴巴內(nèi)部廣泛使用的GA版本為:2.4.9,強(qiáng)烈推薦此版本 --><dubbo.version>2.5.3</dubbo.version><dubbox.version>2.8.4</dubbox.version><spring.version>4.3.6.RELEASE</spring.version><java.version>1.7</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency><!-- spring相關(guān) --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.6.11</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.6.11</version></dependency></dependencies><modules><module>demo-api</module><module>dubbo-consumer</module><module>dubbo-provider</module></modules>
</project>
2. 在demo-api中定義服務(wù)接口(注意服務(wù)提供方和消費(fèi)方都需要依賴這個項目)
package com.test;public interface DemoService{ String sayHello(String name);
}
3. 服務(wù)提供方實現(xiàn)
- 項目結(jié)構(gòu)
- 實現(xiàn)接口
package com.test;
import org.springframework.stereotype.Service;import com.test.DemoService;
@Service("demoService")
public class DemoServiceImpl implements DemoService{@Overridepublic String sayHello(String name) {// TODO Auto-generated method stubreturn name; }
}
<?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"><!-- 提供方應(yīng)用信息,用于計算依賴關(guān)系 --> <dubbo:application name="dubbo_provider" /> <!-- 使用zookeeper注冊中心暴露服務(wù)地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo協(xié)議在20880端口暴露服務(wù) --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 聲明需要暴露的服務(wù)接口 --> <dubbo:service interface="com.test.DemoService" ref="demoService" />
</beans>
- 在springmvc.xml中掃描service注解并將dubbo-provider.xml中的相關(guān)的dubbo配置引入進(jìn)來
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"default-autowire="byName"><aop:aspectj-autoproxy /><context:component-scan base-package="com.test" /><import resource="classpath:dubbo-provider.xml" />
</beans>
package com.test;import java.io.IOException;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:springmvc.xml");context.start();System.out.println("Dubbo provider start...");try {System.in.read(); // 按任意鍵退出} catch (IOException e) {e.printStackTrace();} }
}
###4. 服務(wù)消費(fèi)者實現(xiàn)
- 項目結(jié)構(gòu)
- 在dubbo-consumer.xml中聲明所所需要消費(fèi)的服務(wù)
<?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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!-- 消費(fèi)方應(yīng)用名,用于計算依賴關(guān)系,不是匹配條件,不要與提供方一樣 --> <dubbo:application name="dubbo_consumer" /> <!-- 使用multicast廣播注冊中心暴露發(fā)現(xiàn)服務(wù)地址 --> <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" /> <!-- 生成遠(yuǎn)程服務(wù)代理,可以和本地bean一樣使用demoService --> <dubbo:reference id="demoService" interface="com.test.DemoService" />
</beans>
- 在springmvc.xml中掃描service注解并將dubbo-consumer.xml中的相關(guān)的dubbo配置引入進(jìn)來
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"default-autowire="byName"><aop:aspectj-autoproxy /><context:component-scan base-package="com.test" /><import resource="classpath:/dubbo-consumer.xml" />
</beans>
- 加載Spring配置,調(diào)用服務(wù):
package com.test;import java.io.IOException;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:springmvc.xml" });context.start();DemoService demoService = (DemoService) context.getBean("demoService");System.out.println(demoService.sayHello("哈哈哈"));try {System.in.read();} catch (IOException e) {e.printStackTrace();}}
}
- 如果出現(xiàn)以下結(jié)果則調(diào)用成功
- 通過dubbo-admin管理后臺可以看到服務(wù)的提供方與消費(fèi)方
具體搭建可參考博客:http://blog.csdn.net/jingyangv587/article/details/78904369
提供方:
消費(fèi)者:
點(diǎn)擊下載示例項目(含admin包)
參考博客:https://www.cnblogs.com/Javame/p/3632473.html
想進(jìn)一步了解dubbo請移步我的另外的dubob相關(guān)的文章:https://blog.csdn.net/jingyangV587/article/details/84983770
總結(jié)
以上是生活随笔為你收集整理的最简单的dubbo教程-快速入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。