首页 > 代码库 > dubbo简单使用
dubbo简单使用
一、安装zookeeper
参考http://www.cnblogs.com/ywlaker/p/6060005.html
二、安装dubbo管理中心
参考http://www.cnblogs.com/ywlaker/p/6060146.html
三、创建demo项目
创建三个项目
- dubbo-demo-client 项目用来声明服务接口与数据对象,简单java项目,声明dubbo服务接口,无main方法,打包jar格式。
- dubbo-demo-server 项目用来实现服务接口(dubbo服务提供者),一般为简单java项目,包含main方法,打包jar或者zip格式。
- dubbo-demo-test 项目用来消费服务(第三方dubbo服务使用者),一般为spring mvc项目,打包war或者zip格式。
四、dubbo服务声明
在dubbo-demo-client项目中创建接口DemoService.java
package org.dubbo.demo.client; public interface DemoService { public String test(String str); }
五、dubbo服务实现
dubbo-demo-server项目修改pom.xml,添加以下依赖,要添加对dubbo-demo-client的依赖
<dependency> <groupId>com.xmyself</groupId> <artifactId>dubbo-demo-client</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency>
在dubbo-demo-server项目中创建类DemoServiceImpl.java,实现dubbo-demo-client声明的接口
package org.dubbo.demo.server; import org.dubbo.demo.client.DemoService; import org.springframework.stereotype.Service; @Service("demoService") public class DemoServiceImpl implements DemoService { public String test(String str) { return "hello world " + str; } }
创建目录
src/main/resources/META-INF/spring
在该目录下创建dubbo-server.xml,配置zookeeper地址和端口以及dubbo服务版本号
<?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"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo-demo" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry protocol="zookeeper" address="172.17.210.124:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="org.dubbo.demo.client.DemoService" ref="demoService" version="0.0.1" /> <bean id="demoService" class="org.dubbo.demo.server.DemoServiceImpl" /> </beans>
创建Main.java
package org.dubbo.demo.server; public class Main { public static void main(String[] args) { /** * 启动容器,自动加载资源目录下的/META-INF/spring/*.xml初始化spring容器 */ com.alibaba.dubbo.container.Main.main(args); } }
启动该主类,即启动了dubbo服务。主类也可以用如下的方式来实现
public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/dubbo-server.xml"); context.start(); System.in.read();//阻塞 }
六、dubbo服务消费
dubbo-demo-test项目首先配置好spring mvc,参考http://www.cnblogs.com/ywlaker/p/6046221.html,然后修改pom.xml添加依赖,要添加client的依赖
<dependency> <groupId>com.xmyself</groupId> <artifactId>dubbo-demo-client</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <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.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> <exclusions> <exclusion> <artifactId>org.apache.zookeeper</artifactId> <groupId>zookeeper</groupId> </exclusion> <exclusion> <artifactId>zookeeper</artifactId> <groupId>org.apache.zookeeper</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency>
在src/main/resources/spring目录下创建dubbo-client.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 应用信息 --> <dubbo:application name="dubbo-demo" /> <!-- zookeeper注册中心 --> <dubbo:registry protocol="zookeeper" address="172.17.210.124:2181" /> <!-- 服务接口 --> <dubbo:reference id="demoService" interface="org.dubbo.demo.client.DemoService" version="0.0.1" /> </beans>
在spring初始化配置文件中加载dubbo-client.xml
<!-- 加载spring-dubbo-client.xml --> <import resource="classpath*:/spring/dubbo-client.xml" />
创建DemoController.java
package org.dubbo.demo.test.controller; import org.dubbo.demo.client.DemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class DemoController { @Autowired private DemoService demoService; @RequestMapping("/test") @ResponseBody public String test() { return demoService.test("dubbo-demo-test"); } }
启动项目,在浏览器地址栏输入
http://127.0.0.1:8080/dubbo-demo-test/test
dubbo简单使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。