分布式学习系列【dubbo入门实践】
分布式学习系列【dubbo入门实践】
dubbo架构
组成部分:provider,consumer,registry,monitor; provider,consumer注册,订阅类似于消息队列的注册订阅
一、环境安装
1、dubbo admin 管理控制台安装(Windows环境)
#下载dubbo-admin-2.5.3.war,部署到tomcat下,根据需要编辑WEB-INF/dubbo.properties文件:
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
#dubbo admin 部署问题:版本2.5.3启动报错:Bean property 'URIType' is not writable or has an invalid
原因:使用了JDK1.8;
解决方法:将环境变量JAVA_HOME设置为JDK1.7;
2、zookeeper 安装
dubbo对注册中心实现了抽象,实现的注册中心有:ZooKeeper,Redis,Multicast;这里采用zookeeper进行演示。
下载zookeeper解压,在zookeeper->conf目录下复制一份zoo_sample.cfg为zoo.cfg,修改配置
添加配置:(默认不会自动创建,需要手动添加)
dataDir=D:\\zookeeper\\data
dataLogDir=D:\\zookeeper\\log
下载后windows下直接进入doc,进入zookeeper->bin目录运行zkServer.cmd命令;
可以用netstat -ano|findstr "2181"检查是否启动成功
TCP 0.0.0.0:2181 0.0.0.0:0 LISTENING 5716 TCP 127.0.0.1:2181 127.0.0.1:53785 ESTABLISHED 5716 TCP 127.0.0.1:53785 127.0.0.1:2181 ESTABLISHED 9752 TCP [::]:2181 [::]:0 LISTENING 5716
也可用JPS查看:
8608 SmartGit 8992 Jps 5716 QuorumPeerMain 9752 Bootstrap
3、启动tomcat访问:localhost:8080
二、实践步骤
#创建服务接口
public interface DemoService { String sendMessage(String message); }
#创建接口实现
public class DemoServiceImpl implements DemoService { @Override public String sendMessage(String message){ System.out.println("this demoService get message:"+message); return "provirder return message success"; } }
#创建提供方spring配置文件provider.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-test" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.demo.DemoService" /> </beans>
#创建提供方启动程序:DemoProvider
public class DemoProvider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.out.println("provider started ..."); System.in.read(); } }
#创建消费方spring配置文件consumer.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-test" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.demo.DemoService" /> </beans>
#创建消费方方启动程序:DemoConsumer
public class DemoConsumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); String returnMessage = demoService.sendMessage("consumer send message: hello my first dubbo service"); System.out.println("provider return message:"+returnMessage); } }
#添加项目maven依赖:
分别加入spring IOC,zkClient,dubbo依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>
#启动提供方服务,消费方进行调用
运行启动提供方DemoProvider,启动后进入待服务状态
观察dubbo admin发现已经刚才的启动服务在zookeepr成功注册
这时消费方可以进行调用,运行DemoConsumer
此时服务端收到请求:
调用方收到响应:
附上示例代码: https://files.cnblogs.com/files/yuxuan/dubbo-demo.rar