标签:
这里最熟悉的就是spring了,项目中应用很多。dubbo是一个实现分布式的框架,zookeeper是注册中心。给我的感觉就像多对多关系的两者表,zookeeper相当于第三张表维护关系。下面通过一个小程序加深认识。
一、安装zookeeper
去官网下载zookeeper, 然后解压到目录中,我解压到了E:\zookeeper-3.3.6,在启动zookeeper之前,首先找到conf文件夹下的 zoo_sample.cfg,重新命名为zoo.cfg,网上说zookeeper启动的时候这个文件会是默认的配置文件。接下来到bin目录下,双击 启动zkServer.cmd,启动成功了如下:
建的项目是maven项目,所以jar包的依赖都通过maven引用的,项目通过spring容器管理。
二、Server
项目结构图:
1.接口
- package com.mor.server.dubbo.service;
-
- public interface DemoServer {
-
- String sayHello(String str);
-
- }
2.实现
- package com.mor.server.dubbo.service;
-
- import java.util.Date;
-
- public class DemoServerImpl implements DemoServer {
-
- public String sayHello(String str) {
- str = "Hello " + str + " 2:" + new Date();
- System.err.println("server:" + str);
- return str;
- }
- }
3.pom文件
通过maven引用需要的jar包
4.spring配置文件
先引入dubbo的标签
- <?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" />
- <dubbo:registry protocol="zookeeper" address="192.168.24.140:2181" />
- <dubbo:protocol name="dubbo" port="20880" />
- <dubbo:service interface="com.mor.server.dubbo.service.DemoServer" ref="demoService" />
- <bean id="demoService" class="com.mor.server.dubbo.service.DemoServerImpl" />
- </beans>
5.执行入口
- package com.mor.main;
- import java.io.IOException;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class Main {
-
- public static void main(String[] args) throws IOException {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });
- context.start();
- System.out.println("按任意键退出");
- System.in.read();
- }
-
- }
三、Client
项目结构图:
1.接口同服务端
2.调用接口
- package com.mor.server.dubbo.service;
-
- import java.util.Date;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class ChatAction {
-
- public void SayHello(){
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
- context.start();
- DemoServer demoServer = (DemoServer) context.getBean("demoService");
- System.out.println("client:"+demoServer.sayHello("zx"+"1:"+new Date())+"3:"+new Date());
- }
- }
3.pom文件引用的jar都相同,只是修改一下基本的配置就可以了。
4.spring配置文件
- <?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" />
- <dubbo:registry protocol="zookeeper" address="192.168.24.140:2181,,192.168.24.140:2182,192.168.24.140:2183" />
- <dubbo:reference id="demoService" interface="com.mor.server.dubbo.service.DemoServer" />
- </beans>
5.执行入口
- package com.mor.client.dubbo.main;
- import com.mor.server.dubbo.service.ChatAction;
-
- public class Main {
-
- public static void main(String[] args) throws InterruptedException {
- int i=0;
- while(i++<100){
- ChatAction act = new ChatAction();
- act.SayHello();
- Thread.sleep(3000);
- }
- }
-
- }
四、测试
先启动zookeeper,再依次启动服务器和客户端。
服务器启动成功如下:
客户端访问成功如下:
五、总结
运用dubbo能实现分布式,dubbo也是面向服务的架构。zookeeper做为注册中心,拿到服务器端暴露的接口,客户端也向zookeepe去注册,客户端需要什么服务注册中心就提供给客户端。这样客户端和服务端很好的解耦了。
[Dubbo实战]dubbo + zookeeper + spring 实战 (转)
标签:
原文地址:http://www.cnblogs.com/zhengah/p/5189412.html