admin控制台安装
1.控制台下载地址https://github.com/apache/incubator-dubbo/releases
2.找到dubbo-admin
3.修改webapp/WEB-INF/dubbo.properties
dubbo.registry.address=zookeeper的集群地址
4.控制中心是用来做服务治理的,比如控制服务的权重、服务的路由、。。。
simple监控中心
1.下载dubbo-monitor-simple-2.5.3-assembly.tar.gz包
2.解压后到解压目录下的dubbo-monitor-simple-2.5.3\conf中找到dubbo.properties文件,修改成zookeeper注册中心地址
3.点击bin目录下的start.bat启动,启动成功后,通过http://localhost:8080来访问监控中心。
Monitor也是一个dubbo服务,所以也会有端口和url
4.监控服务的调用次数、调用关系、响应事件
简单实现的大概流程:
1.定义接口以及接口的实现类
2.配置好Dubbo配置文件后,通过Main方法启动服务端,将服务暴露
服务端配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://code.alibabatech.com/schema/dubbo 8 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 9 10 <!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签--> 11 <dubbo:application name="order-provider" owner="provider"/> 12 13 <dubbo:monitor protocol="registry"/> 14 15 <!--dubbo这个服务所要暴露的服务地址所对应的注册中心--><!-- N/A:代表没有使用注册中心 --> 16 <dubbo:registry protocol="zookeeper" address="47.107.121.215:2181"/> 17 18 <!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http--> 19 <dubbo:protocol name="dubbo" port="20880"/> 20 21 <!--服务发布的配置,需要暴露的服务接口--> 22 <dubbo:service 23 interface="com.karat.cn.order_api.IOrderServices" 24 ref="orderService"/> 25 26 <!--接口的实现 Bean bean定义--> 27 <bean id="orderService" 28 class="com.karat.cn.order_provider.OrderServiceImpl"/> 29 30 </beans>
Main方法:
1 package com.karat.cn.order_provider; 2 import com.alibaba.dubbo.container.Main; 3 /** 4 * Hello world! 5 * 发布方法 6 */ 7 public class App 8 { 9 public static void main( String[] args ) 10 { 11 Main.main(args); 12 } 13 }
启动成功后日志中内容如下:
1 2019-03-21 11:28:38,415 INFO [org.I0Itec.zkclient.ZkClient] - zookeeper state changed (SyncConnected) 2 2019-03-21 11:28:38,416 INFO [com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry] - [DUBBO] Register: dubbo://10.0.1.183:20880/com.karat.cn.order_api.IOrderServices?anyhost=true&application=order-provider&dubbo=2.5.3&interface=com.karat.cn.order_api.IOrderServices&methods=doOrder&owner=provider&pid=17524&side=provider×tamp=1553138908571, dubbo version: 2.5.3, current host: 127.0.0.1 3 2019-03-21 11:28:38,600 INFO [com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry] - [DUBBO] Subscribe: provider://10.0.1.183:20880/com.karat.cn.order_api.IOrderServices?anyhost=true&application=order-provider&category=configurators&check=false&dubbo=2.5.3&interface=com.karat.cn.order_api.IOrderServices&methods=doOrder&owner=provider&pid=17524&side=provider×tamp=1553138908571, dubbo version: 2.5.3, current host: 127.0.0.1 4 2019-03-21 11:28:38,850 INFO [com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry] - [DUBBO] Notify urls for subscribe url provider://10.0.1.183:20880/com.karat.cn.order_api.IOrderServices?anyhost=true&application=order-provider&category=configurators&check=false&dubbo=2.5.3&interface=com.karat.cn.order_api.IOrderServices&methods=doOrder&owner=provider&pid=17524&side=provider×tamp=1553138908571, urls: [empty://10.0.1.183:20880/com.karat.cn.order_api.IOrderServices?anyhost=true&application=order-provider&category=configurators&check=false&dubbo=2.5.3&interface=com.karat.cn.order_api.IOrderServices&methods=doOrder&owner=provider&pid=17524&side=provider×tamp=1553138908571], dubbo version: 2.5.3, current host: 127.0.0.1 5 2019-03-21 11:28:38,933 INFO [com.alibaba.dubbo.container.Main] - [DUBBO] Dubbo SpringContainer started!, dubbo version: 2.5.3, current host: 127.0.0.1 6 [2019-03-21 11:28:38] Dubbo service server started!
第二行的:dubbo://10.0.1.183:20880/com.karat.cn.order_api.IOrderServices 是后面需要用到的服务url地址,使用zookeeper也是让zookeeper来管理这个东西
3.消费端通过Dubbo配置文件调用服务端服务
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://code.alibabatech.com/schema/dubbo 8 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 9 <!--消费端不需要配置协议端口 --> 10 11 12 <!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签--> 13 <dubbo:application name="order-provider" owner="provider"/> 14 15 <!--dubbo这个服务所要暴露的服务地址所对应的注册中心--><!-- N/A:代表没有使用注册中心 --> 16 <!-- 17 配置集群地址: <dubbo:registry address="zookeeper://192.168.11.129:2181?backup=192.168.11.137:2181,192.168.11.138:2181,192.168.11.139:2181"/> 18 --> 19 <dubbo:registry address="zookeeper://47.107.121.215:2181"/> 20 21 <!--生成一个远程服务的调用代理--><!--引用一个服务 通过url关联 --> 22 <dubbo:reference id="orderServices" 23 interface="com.karat.cn.order_api.IOrderServices"/> 24 <!-- 使用zookeeper注册中心后不需要下面的url了,在zookeeper中能拿到url --> 25 <!-- url="dubbo://10.0.1.183:20880/com.karat.cn.order_api.IOrderServices" --> 26 </beans>
4.消费端测试
public static void main( String[] args ) throws IOException { ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("order-consumer.xml"); //用户下单过程 IOrderServices services=(IOrderServices)context.getBean("orderServices"); DoOrderRequest request=new DoOrderRequest(); request.setName("Hello world!"); DoOrderResponse response=services.doOrder(request); System.out.println(response); }
消费端的pom中需要引入服务端项目打包成的jar包
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <dependencies> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</artifactId> 5 <version>3.8.1</version> 6 <scope>test</scope> 7 </dependency> 8 <dependency> 9 <groupId>com.karat.cn</groupId> 10 <artifactId>order_api</artifactId> 11 <version>0.0.1-SNAPSHOT</version> 12 <scope>C:\Users\devel\Desktop\aa.jar</scope> 13 </dependency> 14 15 <dependency> 16 <groupId>com.alibaba</groupId> 17 <artifactId>dubbo</artifactId> 18 <version>2.5.3</version> 19 </dependency> 20 <dependency> 21 <groupId>com.github.sgroschupf</groupId> 22 <artifactId>zkclient</artifactId> 23 <version>0.1</version> 24 </dependency> 25 </dependencies>
invoker:cmd中Dubbo调用命令