dubbo快速入门demo
参考文章 https://blog.csdn.net/abcwanglinyong/article/details/81906027
该demo包含三个项目,分别是:
服务提供端项目:provider
服务消费端项目:consumer
共用服务接口项目:api
1、新建maven项目api
注意将其pom.xml中的打包方式改为jar
然后在com.dubbo.api.service包下新建DemoService接口,如下:
package com.dubbo.service; public interface DemoService { String sayHello(String name); }
项目结构如图,将api工程打成jar 安装到maven仓库
2.dubbo服务提供者provider项目
新建maven项目provider,并在pom.xml中添加以下依赖:
<dependencies> <dependency> <groupId>com.dubbo.api</groupId> <artifactId>api</artifactId> <version>1.0-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.10</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.5.RELEASE</version> </dependency> </dependencies>
这里引入了上面的生成的api接口依赖、dubbo依赖、zookeeper客服端依赖以及启动项目必需的依赖
然后在com.dubbo.provider.service.impl包下新建DemoServiceImp并实现DemoService接口,如下:
package com.dubbo.provider.service.impl; import com.dubbo.service.DemoService; public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hello " + name; } }
然后在resources下新建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-admin 或 dubbo-monitor 会显示这个名字,方便辨识--> <dubbo:application name="demo-provider" owner="xxy" organization="dubbox"/> <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper--> <dubbo:registry address="zookeeper://192.168.64.123:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口--> <dubbo:service interface="com.dubbo.service.DemoService" ref="demoService" protocol="dubbo" /> <!--具体实现该接口的 bean--> <bean id="demoService" class="com.dubbo.provider.service.impl.DemoServiceImpl"/> </beans>
然后在java下直接新建Provider用于启动项目:
package com.dubbo.provider.service.impl; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Provider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.out.println("dubbo服务提供端已启动...."); System.in.read(); // 按任意键退出 } }
provider项目结构如图:
然后直接运行该main函数-dubbo服务提供者已启动!
3.dubbo服务消费者consumer项目
新建maven项目consumer,并在pom.xml中添加以下依赖:
<?xml version="1.0" encoding="UTF-8"?> <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.dubbo.consumer</groupId> <artifactId>consumer</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.dubbo.api</groupId> <artifactId>api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.5.RELEASE</version> </dependency> </dependencies> </project>
和provider项目添加的依赖一样。
然后在resources下新建comsumer.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="demo-consumer" owner="xxy" organization="dubbox"/> <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送--> <dubbo:registry address="zookeeper://192.168.64.123:2181"/> <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口--> <dubbo:reference id="demoService" interface="com.dubbo.service.DemoService"/> </beans>
然后在java下新建Consumer.class用于启动该项目,如下:
package com.dubbo.cusumer; import com.dubbo.service.DemoService; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Consumer { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "cosumer.xml" ); context.start(); System.out.println("dubbo服务消费端已启动..."); DemoService demoService = (DemoService) context.getBean("demoService"); String hello = demoService.sayHello("world"); System.out.println(hello);//显示调用结果 System.in.read(); // 按任意键退出 } }
启动该项目,显示调用结果,如图:
到这里就已经完成了dubbo的远程调用。下面介绍下dubbo-admin管理控制台
4、dubbo-admin管理控制台
通过dubbo-admin可以更好的管理dubbo服务。
首先下载dubbo-admin的war包,下载地址:
http://www.java1234.com/a/javabook/javaweb/2018/0224/10496.html
下载完成后,将dubbo-admin-2.6.0.war复制到tomcat的webapps目录下。然后启动tomcat,访问(这里我的tomcat版本是apache-tomcat-7.0.91,8.5的我这边有问题)
修改 dubbo.properties
dubbo.registry.address=zookeeper://192.168.64.123:2181 dubbo.admin.root.password=服务器账号 dubbo.admin.guest.password=服务器密码
http://localhost:8080/dubbo-admin-2.6.0/
也可以将dubbo-admin-2.6.0重命名为dubbo-admin访问的时候直接访问http://localhost:8080/dubbo-admin即可
用户名和密码都是root
登录后就可以通过网页进行管理dubbo服务了,如图:
5、最后看一下 zookeeper 的文件结构
[zk: 192.168.64.123:2181(CONNECTED) 27] ls /dubbo [com.dubbo.service.DemoService] [zk: 192.168.64.123:2181(CONNECTED) 28]