Dubbo实例

Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。 主要核心部件


Remoting: 网络通信框架,实现了sync-over-async 和 request-response 消息机制.


RPC: 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能


Registry: 服务目录框架用于服务的注册和服务事件发布和订阅。


Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。

dubbo架构图如下所示:



节点角色说明:
       Provider: 暴露服务的服务提供方。
       Consumer: 调用远程服务的服务消费方。
       Registry: 服务注册与发现的注册中心。
       Monitor: 统计服务的调用次调和调用时间的监控中心。
       Container: 服务运行容器。
调用关系说明:
0 服务容器负责启动,加载,运行服务提供者。
1. 服务提供者在启动时,向注册中心注册自己提供的服务。
2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。



2.   实例



2.1 pom.xml

首先maven项目增加dubbo的jar依赖,因为要用到zookeeper注册中心,也要依赖但是要去掉自带的log4j。

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <modelVersion>4.0.0</modelVersion>  
  6.   
  7.     <groupId>test</groupId>  
  8.     <artifactId>test</artifactId>  
  9.     <version>1.0-SNAPSHOT</version>  
  10.     <packaging>jar</packaging>  
  11.   
  12.     <properties>  
  13.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  14.         <spring.version>3.1.4.RELEASE</spring.version>  
  15.         <slf4j.version>1.6.6</slf4j.version>  
  16.     </properties>  
  17.   
  18.     <dependencies>  
  19.         <!-- dubbo  -->  
  20.         <dependency>  
  21.             <groupId>com.alibaba</groupId>  
  22.             <artifactId>dubbo</artifactId>  
  23.             <version>2.5.3</version>  
  24.         </dependency>  
  25.         <!-- zookeeper  -->  
  26.         <dependency>  
  27.             <groupId>org.apache.zookeeper</groupId>  
  28.             <artifactId>zookeeper</artifactId>  
  29.             <version>3.4.6</version>  
  30.             <exclusions>  
  31.                 <exclusion>  
  32.                     <groupId>log4j</groupId>  
  33.                     <artifactId>log4j</artifactId>  
  34.                 </exclusion>  
  35.             </exclusions>  
  36.         </dependency>  
  37.   
  38.         <!-- Spring -->  
  39.         <dependency>  
  40.             <groupId>org.springframework</groupId>  
  41.             <artifactId>spring-aop</artifactId>  
  42.             <version>${spring.version}</version>  
  43.         </dependency>  
  44.         <dependency>  
  45.             <groupId>org.springframework</groupId>  
  46.             <artifactId>spring-asm</artifactId>  
  47.             <version>${spring.version}</version>  
  48.         </dependency>  
  49.         <dependency>  
  50.             <groupId>org.springframework</groupId>  
  51.             <artifactId>spring-core</artifactId>  
  52.             <version>${spring.version}</version>  
  53.         </dependency>  
  54.         <dependency>  
  55.             <groupId>org.springframework</groupId>  
  56.             <artifactId>spring-beans</artifactId>  
  57.             <version>${spring.version}</version>  
  58.         </dependency>  
  59.         <dependency>  
  60.             <groupId>org.springframework</groupId>  
  61.             <artifactId>spring-context</artifactId>  
  62.             <version>${spring.version}</version>  
  63.         </dependency>  
  64.         <dependency>  
  65.             <groupId>org.springframework</groupId>  
  66.             <artifactId>spring-expression</artifactId>  
  67.             <version>${spring.version}</version>  
  68.         </dependency>  
  69.         <!-- log -->  
  70.         <dependency>  
  71.             <groupId>log4j</groupId>  
  72.             <artifactId>log4j</artifactId>  
  73.             <version>1.2.16</version>  
  74.         </dependency>  
  75.     </dependencies>  
  76.   
  77. </project>  

因为要增加zookeeper的注册管理,所以如果有可用的zookeeper就用可用的zookeeper。本实例使用的zk地址是192.168.4.114:2181

2.2 dubbo-provider.xml

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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 = "hello-world-app-provider" />  
  12.   
  13.     <!--zookeeper注册中心 -->  
  14.     <dubbo:registry protocol="zookeeper" address="192.168.4.114:2181" />  
  15.   
  16.     <!-- 用dubbo协议在20880端口暴露服务 -->  
  17.     <dubbo:protocol name="dubbo" port="20880" />  
  18.   
  19.     <!-- 声明需要暴露的服务接口 -->  
  20.     <dubbo:service interface="com.mt.test.DemoService" ref="demoProvideService"/>  
  21.     <!-- 具体的实现bean -->  
  22.     <bean id="demoProvideService" class="com.mt.test.DemoServiceImpl" />  
  23. </beans>  


2.3 dubbo-consumer.xml
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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="hello-world-app-consumer"/>  
  12.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
  13.     <dubbo:registry protocol="zookeeper" address="192.168.4.114:2181"/>  
  14.     <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->  
  15.     <dubbo:reference id="demoConsumeService" interface="com.mt.test.DemoService"/>  
  16.   
  17. </beans>  

2.4 DemoService.java

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.mt.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. public interface DemoService {  
  6.     public String sayHello(String name);  
  7.   
  8.     public List getUsers();  
  9. }  


2.5 DemoServiceImpl.java
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.mt.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class DemoServiceImpl implements DemoService{  
  7.     @Override  
  8.     public String sayHello(String name) {  
  9.         return "Hello " + name;  
  10.     }  
  11.   
  12.     @Override  
  13.     public List getUsers() {  
  14.         List list = new ArrayList();  
  15.         User u1 = new User();  
  16.         u1.setName("jack");  
  17.         u1.setAge(20);  
  18.         u1.setSex("男");  
  19.   
  20.         User u2 = new User();  
  21.         u2.setName("tom");  
  22.         u2.setAge(21);  
  23.         u2.setSex("女");  
  24.   
  25.         User u3 = new User();  
  26.         u3.setName("rose");  
  27.         u3.setAge(19);  
  28.         u3.setSex("女");  
  29.   
  30.         list.add(u1);  
  31.         list.add(u2);  
  32.         list.add(u3);  
  33.         return list;  
  34.     }  
  35. }  

2.6 LuncherProvider.java

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.mt.test;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4. import org.springframework.context.ApplicationContext;  
  5.   
  6. public class LuncherProvider {  
  7.     public static void main(String[] args) throws Exception {  
  8.         LuncherProvider luncher = new LuncherProvider();  
  9.         luncher.start();  
  10.         System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟  
  11.     }  
  12.   
  13.     public void start() {  
  14.         String configLocation = "spring/dubbo-provider.xml";  
  15.         ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);  
  16.         String[] names = context.getBeanDefinitionNames();  
  17.         System.out.print("Beans:");  
  18.         for (String string : names)  
  19.             System.out.print(string + ",");  
  20.         System.out.println();  
  21.     }  
  22. }  


2.7 LuncherConsumer.java

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.mt.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class LuncherConsumer {  
  7.     public static void main(String[] args) throws Exception {  
  8.         LuncherConsumer luncher = new LuncherConsumer();  
  9.         luncher.start();  
  10.     }  
  11.   
  12.   
  13.     void start() {  
  14.         String configLocation = "spring/dubbo-consumer.xml";  
  15.         ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);  
  16.         String[] names = context.getBeanDefinitionNames();  
  17.         System.out.print("Beans:");  
  18.         for (String string : names) {  
  19.             System.out.print(string);  
  20.             System.out.print(",");  
  21.         }  
  22.         System.out.println();  
  23.   
  24.   
  25.         DemoService ds = (DemoService) context.getBean("demoConsumeService");  
  26.         System.out.println(ds.sayHello("hehe"));  
  27.         System.out.println(ds.getUsers());  
  28.     }  
  29. }  

2.8 User.java

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.mt.test;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5.   
  6. //必须实现Serializable接口  
  7. public class User implements Serializable {  
  8.     private static final long serialVersionUID = -2814022769568306965L;  
  9.     private String name;  
  10.     private Integer age;  
  11.     private String sex;  
  12.   
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.   
  17.     public void setName(String name) {  
  18.         this.name = name;  
  19.     }  
  20.   
  21.     public Integer getAge() {  
  22.         return age;  
  23.     }  
  24.   
  25.     public void setAge(Integer age) {  
  26.         this.age = age;  
  27.     }  
  28.   
  29.     public String getSex() {  
  30.         return sex;  
  31.     }  
  32.   
  33.     public void setSex(String sex) {  
  34.         this.sex = sex;  
  35.     }  
  36.   
  37.   
  38.     @Override  
  39.     public String toString() {  
  40.         return "User{" +  
  41.                 "name='" + name + '\'' +  
  42.                 ", age=" + age +  
  43.                 ", sex='" + sex + '\'' +  
  44.                 '}';  
  45.     }  
  46. }<strong>  
  47. </strong>  

运行结果:

(1)provider:

Beans:hello-world-app-provider,com.alibaba.dubbo.config.RegistryConfig,dubbo,com.mt.test.DemoService,demoProvideService,

(2)consumer:

Beans:hello-world-app-consumer,com.alibaba.dubbo.config.RegistryConfig,demoConsumeService,
Hello hehe
[User{name='jack', age=20, sex='男'}, User{name='tom', age=21, sex='女'}, User{name='rose', age=19, sex='女'}]

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值