淘宝SOA框架dubbo学习(1)--first demo

本文介绍了如何使用Dubbo框架实现服务提供者、服务消费者以及监控中心的部署与测试流程。包括服务提供者jar生成、部署服务容器、服务部署、服务消费者代码编写与测试等步骤,以及通过Zookeeper实现服务注册中心的替换。最后,文章还演示了添加监控中心以实时监控服务状态的方法。

------------------------------------------------------------通过广播---------------------------------------------

部署开发,需要三部分:服务提供者、服务容器、服务消费者

本人用 eclipse 开发

1、服务提供者jar生成

A、项目截图


B、源码:

?
1
2
3
4
5
package  com.alibaba.dubbo.demo;
 
public  interface  DemoService {
     String sayHello(String name);
}
?
1
2
3
4
5
6
7
8
9
package  com.alibaba.dubbo.demo.provider;
 
import  com.alibaba.dubbo.demo.DemoService;
 
public  class  DemoServiceImpl  implements  DemoService{
     public  String sayHello(String name) {
         return  "Hello "  + name;
     }
}

provider.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<? 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 = "hello-world-app"   />
  
     <!-- 使用multicast广播注册中心暴露服务地址 -->
     < dubbo:registry  address = "multicast://224.5.6.7:1234"  />
  
     <!-- 用dubbo协议在20880端口暴露服务 -->
     < dubbo:protocol  name = "dubbo"  port = "20880"  />
  
     <!-- 声明需要暴露的服务接口 -->
     < dubbo:service  interface = "com.alibaba.dubbo.demo.DemoService"  ref = "demoService"  />
  
     <!-- 和本地bean一样实现服务 -->
     < bean  id = "demoService"  class = "com.alibaba.dubbo.demo.provider.DemoServiceImpl"  />
  
</ beans >

C、生成jar包

右键项目名称->export->java ->jar file - > 点击下一步

按下图所示,进行选择

点击finish,jar包导出到路径:C:\Users\michael\Desktop\app.jar

注:jar包叫什么名字,没任何关系

2、部署服务容器:

下载

dubbo-demo-provider-2.5.4-SNAPSHOT-assembly.tar.gz

下载页面

http://alibaba.github.io/dubbo-doc-static/Download-zh.htm

注:可能下载不下来,我是从dubbo的QQ群里下载下来的

解压缩后,容器部署完成

3、部署服务

第2步中解压缩的目录,将第2步中的jar包,放入到 dubbo-demo-provider-2.5.4-SNAPSHOT\conf或者dubbo-demo-provider-2.5.4-SNAPSHOT\lib目录下

进入demo-provider-2.5.4-SNAPSHOT\bin

注:修改bin\start.bat文件,classpath中加入:..\conf\app.jar

双击:start.bat

服务正式启动

4、在eclipse中写消费者程序测试部署的服务

A、项目分布

B、源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import  org.springframework.context.support.ClassPathXmlApplicationContext;
 
import  com.alibaba.dubbo.demo.DemoService;
 
public  class  Consumer {
 
     /**
      * @param args
      */
     public  static  void  main(String[] args) {
         ClassPathXmlApplicationContext context =  new  ClassPathXmlApplicationContext(
                 new  String[] {  "classpath:consumer.xml"  });
         context.start();
 
         DemoService demoService = (DemoService) context.getBean( "demoService" );  // 获取远程服务代理
         String hello = demoService.sayHello( "world" );  // 执行远程方法
 
         System.out.println(hello);  // 显示调用结果
     }
}
?
1
2
3
4
5
package  com.alibaba.dubbo.demo;
 
public  interface  DemoService {
     String sayHello(String name);
}

consumer.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<? 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 = "consumer-of-helloworld-app"   />
  
     <!-- 使用multicast广播注册中心暴露发现服务地址 -->
     < dubbo:registry  address = "multicast://224.5.6.7:1234"  />
  
     <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
     < dubbo:reference  id = "demoService"  interface = "com.alibaba.dubbo.demo.DemoService"  />
  
</ beans >


C、启动

Consumer类中的main方法

D、OK,完事


---------------------------------------通过zookeeper------------------------------------

1、下载 Zookeeper

下载页面地址:

http://www.apache.org/dist/zookeeper/zookeeper-3.4.6/


注:下面步骤,windows和linux下几乎没什么太大区别


2、解压缩后进入

C:\zookeeper-3.4.6

目录结构如下图

3、进入conf目录

备份zoo_sample.cfg文件,然后将zoo_sample.cfg 更名为zoo.cfg

4、编辑zoo.cfg为以下内容,(其中data目录需改成你真实输出目录)

注:本人此次实验为单机版的zookeeper注册中心,多机版会有很大不同

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/dubbo/zookeeper-3.3.3/data
clientPort=2181

5、启动zookeeper

windows双击:bin目录下zkServer.cmd文件

linux下,在bin目录执行zkServer.sh命令

6、telnet 127.0.0.1 2181

OK,zookeeper 启动成功

7、需要修改

服务提供者配置文件

provider.xml

服务消费者配置文件

provider.xml

中的

?
1
2
     <!-- 使用multicast广播注册中心暴露服务地址 -->
     < dubbo:registry  address = "multicast://224.5.6.7:1234"  />

修改为

?
1
2
     <!-- 使用zookeeper注册中心暴露发现服务地址 -->
     < dubbo:registry  address = "zookeeper://127.0.0.1:2181"  />


8、eclipse下测试用的DubboTestConsumer项目,需要新加入三个jar包

zkclient-0.1.jar

zookeeper-3.3.3.jar

log4j-1.2.16.jar

9、修改DUBBO_HOME/conf/dubbo.properties为以下内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
dubbo.container=log4j,spring
dubbo.application.name=hello-world-app
dubbo.application.owner=
#dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.registry.address=redis://127.0.0.1:6379
#dubbo.registry.address=dubbo://127.0.0.1:9090
dubbo.monitor.protocol=registry
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.service.loadbalance=roundrobin
dubbo.log4j.file=logs/dubbo-demo-provider.log
dubbo.log4j.level=WARN

10、启动zookeeper

11、启动dubbo provider

12、运行eclipse下的DubboTestConsumer项目

一切OK,心情不错的一天

----------------------------------------------------------------添加监控中心-----------------------------------

紧接上一篇,继续我的dubbo的学习之旅

1、下载监控中心dubbo服务安装包

下载地址:

http://code.alibabatech.com/mvn/releases/com/alibaba/dubbo-monitor-simple/2.4.1/dubbo-monitor-simple-2.4.1-assembly.tar.gz


注:此地址,可能下载不了,我是群共享文件里,下载的


2、解压缩后,编辑conf/dubbo.properties

内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
dubbo.container=log4j,spring,registry,jetty
dubbo.application.name=simple-monitor
dubbo.application.owner=
#dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.registry.address=redis://127.0.0.1:6379
#dubbo.registry.address=dubbo://127.0.0.1:9090
dubbo.protocol.port=7070
dubbo.jetty.port=8080
dubbo.jetty.directory=${user.home}/monitor
dubbo.charts.directory=${dubbo.jetty.directory}/charts
dubbo.statistics.directory=${user.home}/monitor/statistics
dubbo.log4j.file=logs/dubbo-monitor-simple.log
dubbo.log4j.level=WARN


3、进入bin目录,双击:start.bat 命令,启动监控服务

4、访问监控服务地址:http://127.0.0.1:8080/

如下图:


5、一切OK,今天的任务,貌似很简单

-------------------------------------------------------------------------------------例子-------------------------------------

开始,先说一句,还是用maven的好


1、由于没用maven,和对dubbo不是很了解的原因,这次,总因为jar包不对,而导致:dubbo客户端程序,启动不起来

所以决定:将原来用过的所有jar包全部去,将dubbo-demo-provider-2.5.4-SNAPSHOT/lib下的所有jar包全部导入项目中

一切就OK了


2、服务消费者代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import  java.util.Set;
 
import  javax.validation.ConstraintViolation;
import  javax.validation.ConstraintViolationException;
 
import  org.springframework.context.support.ClassPathXmlApplicationContext;
 
import  com.alibaba.dubbo.demo.ValidationParameter;
import  com.alibaba.dubbo.demo.ValidationService;
import  com.alibaba.dubbo.rpc.RpcException;
 
public  class  Consumer {
 
     /**
      * @param args
      * @throws Exception
      */
     public  static  void  main(String[] args)  throws  Exception {
         ClassPathXmlApplicationContext context =  new  ClassPathXmlApplicationContext(
                 new  String[] {  "classpath:consumer.xml"  });
         context.start();
 
         // DemoService demoService = (DemoService)
         // context.getBean("demoService");
         // while (true) {
         // String hello = demoService.sayHello("world");
         // System.out.println(hello);
         //
         // Thread.sleep(100);
         // }
 
 
         ValidationService validationService = (ValidationService) context.getBean( "validationService" );
 
         while  ( true ) {
             ValidationParameter parameter =  new  ValidationParameter();
             parameter.setAge( 2 );
             parameter.setEmail( "han@qq.com" );
 
             try  {
                 String result = validationService.intsert(parameter);
 
                 System.out.println(result);
             catch  (RpcException e) {  // 抛出的是RpcException
                 ConstraintViolationException ve = (ConstraintViolationException) e.getCause();  // 里面嵌了一个ConstraintViolationException
                 Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();  // 可以拿到一个验证错误详细信息的集合
                 System.out.println(violations);
             }
         }
     }
}


3、服务消费者配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<? 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 = "consumer-of-helloworld-app"   />
  
     <!-- 使用zookeeper注册中心暴露发现服务地址 -->
     < dubbo:registry  address = "zookeeper://127.0.0.1:2181"  />
  
     <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
     < dubbo:reference  id = "demoService"  interface = "com.alibaba.dubbo.demo.DemoService"
          retries = "2"
           />
  
     <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
     < dubbo:reference  id = "validationService"  interface = "com.alibaba.dubbo.demo.ValidationService"
          retries = "2"  validation = "true"
           />
  
</ beans >

注:重点关注一下,带有下面信息的那一行,就OK了,此处表明,是在客户端侧进行参数验证

?
1
validation= "true"

4、服务提供者配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<? 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 = "hello-world"   />
  
     <!-- 使用zookeeper注册中心暴露发现服务地址 -->
     < dubbo:registry  address = "zookeeper://127.0.0.1:2181"  />
  
     <!-- 用dubbo协议在20880端口暴露服务 -->
     < dubbo:protocol  name = "dubbo"  port = "20880"  />
  
     <!-- 声明需要暴露的服务接口 -->
     < dubbo:service  interface = "com.alibaba.dubbo.demo.DemoService"  ref = "demoService"  />
  
     <!-- 和本地bean一样实现服务 -->
     < bean  id = "demoService"  class = "com.alibaba.dubbo.demo.provider.DemoServiceImpl"  />
  
     <!-- 声明需要暴露的服务接口 -->
     < dubbo:service  interface = "com.alibaba.dubbo.demo.ValidationService"  ref = "validationService"  />
  
     <!-- 和本地bean一样实现服务 -->
     < bean  id = "validationService"  class = "com.alibaba.dubbo.demo.provider.ValidationServiceImpl"  />
  
</ beans >


5、服务消费者和提供者共用的类和接口

?
1
2
3
4
5
6
7
8
package  com.alibaba.dubbo.demo;
 
public  interface  ValidationService {
     @interface  Intsert {
     }
 
     String intsert(ValidationParameter parameter);
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package  com.alibaba.dubbo.demo;
 
import  java.io.Serializable;
 
import  javax.validation.constraints.Max;
import  javax.validation.constraints.Min;
import  javax.validation.constraints.Pattern;
 
public  class  ValidationParameter  implements  Serializable {
     private  static  final  long  serialVersionUID = 3469571402386167794L;
 
     @Pattern (regexp =  "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$" )
     private  String email;
 
     @Min ( 18 )
     // 最小值
     @Max ( 100 )
     // 最大值
     private  int  age;
 
     public  String getEmail() {
         return  email;
     }
 
     public  void  setEmail(String email) {
         this .email = email;
     }
 
     public  int  getAge() {
         return  age;
     }
 
     public  void  setAge( int  age) {
         this .age = age;
     }
 
}

6、服务提供者接口实现类

?
1
2
3
4
5
6
7
8
9
10
11
package  com.alibaba.dubbo.demo.provider;
 
import  com.alibaba.dubbo.demo.ValidationParameter;
import  com.alibaba.dubbo.demo.ValidationService;
 
public  class  ValidationServiceImpl  implements  ValidationService {
     @Override
     public  String intsert(ValidationParameter parameter) {
         return  parameter.getEmail() +  "=="  + parameter.getAge();
     }
}


7、运行客户端程序时,控制台会输出以下信息:

?
1
[ConstraintViolationImpl{interpolatedMessage= '最小不能小于18' , propertyPath=age, rootBeanClass= class  com.alibaba.dubbo.demo.ValidationParameter, messageTemplate= '{javax.validation.constraints.Min.message}' }]


8、OK,此时已经可以验证,参数验证已经开始起作用了。



内容概要:本文设计了一种基于PLC的全自动洗衣机控制系统内容概要:本文设计了一种,采用三菱FX基于PLC的全自动洗衣机控制系统,采用3U-32MT型PLC作为三菱FX3U核心控制器,替代传统继-32MT电器控制方式,提升了型PLC作为系统的稳定性与自动化核心控制器,替代水平。系统具备传统继电器控制方式高/低水,实现洗衣机工作位选择、柔和过程的自动化控制/标准洗衣模式切换。系统具备高、暂停加衣、低水位选择、手动脱水及和柔和、标准两种蜂鸣提示等功能洗衣模式,支持,通过GX Works2软件编写梯形图程序,实现进洗衣过程中暂停添加水、洗涤、排水衣物,并增加了手动脱水功能和、脱水等工序蜂鸣器提示的自动循环控制功能,提升了使用的,并引入MCGS组便捷性与灵活性态软件实现人机交互界面监控。控制系统通过GX。硬件设计包括 Works2软件进行主电路、PLC接梯形图编程线与关键元,完成了启动、进水器件选型,软件、正反转洗涤部分完成I/O分配、排水、脱、逻辑流程规划水等工序的逻辑及各功能模块梯设计,并实现了大形图编程。循环与小循环的嵌; 适合人群:自动化套控制流程。此外、电气工程及相关,还利用MCGS组态软件构建专业本科学生,具备PL了人机交互C基础知识和梯界面,实现对洗衣机形图编程能力的运行状态的监控与操作。整体设计涵盖了初级工程技术人员。硬件选型、; 使用场景及目标:I/O分配、电路接线、程序逻辑设计及组①掌握PLC在态监控等多个方面家电自动化控制中的应用方法;②学习,体现了PLC在工业自动化控制中的高效全自动洗衣机控制系统的性与可靠性。;软硬件设计流程 适合人群:电气;③实践工程、自动化及相关MCGS组态软件与PLC的专业的本科生、初级通信与联调工程技术人员以及从事;④完成PLC控制系统开发毕业设计或工业的学习者;具备控制类项目开发参考一定PLC基础知识。; 阅读和梯形图建议:建议结合三菱编程能力的人员GX Works2仿真更为适宜。; 使用场景及目标:①应用于环境与MCGS组态平台进行程序高校毕业设计或调试与运行验证课程项目,帮助学生掌握PLC控制系统的设计,重点关注I/O分配逻辑、梯形图与实现方法;②为工业自动化领域互锁机制及循环控制结构的设计中类似家电控制系统的开发提供参考方案;③思路,深入理解PL通过实际案例理解C在实际工程项目PLC在电机中的应用全过程。控制、时间循环、互锁保护、手动干预等方面的应用逻辑。; 阅读建议:建议结合三菱GX Works2编程软件和MCGS组态软件同步实践,重点理解梯形图程序中各环节的时序逻辑与互锁机制,关注I/O分配与硬件接线的对应关系,并尝试在仿真环境中调试程序以加深对全自动洗衣机控制流程的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值