zl程序教程

您现在的位置是:首页 >  后端

当前栏目

dubbo 使用 学习二(spring+dubbo+zookeeper单机服务)详解架构师

Springdubbozookeeper服务学习 使用 详解 架构师
2023-06-13 09:20:20 时间

学习目录在上一篇中有了,这篇我们来写一个本地的单服务的demo,下面我们看看需要什么!!!

1、zookeeper的安装:

(1)、zookeeper 的安装很简单,下载架包解压缩,解压缩后的目录下有个conf目录也就是zookeeper的配置文件在该目录下,该目录下有一个zoo_sample.cfg 文件,复制一个修改为zoo.cfg,像我们这里是单服务的所以这里的配置只需要修改dataDir的目录就可以了其他的可以不用修改,dataDir的目录可以自己随便定义!!!

2、服务提供者

创建一个maven 工程,配置spring ,dubbo,zookeeper等相关的jar包,下面来具体看看!

(1)、目录结构

dubbo 使用 学习二(spring+dubbo+zookeeper单机服务)详解架构师

(2)、我们配置pom.xml下载我们需要的jar包,pom.xml配置文件如下:

 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.test /groupId 

 artifactId dubboser /artifactId 

 version 0.0.1 /version 

 packaging jar /packaging 

 name dubboserver /name 

 url http://maven.apache.org /url 

 properties 

 project.build.sourceEncoding UTF-8 /project.build.sourceEncoding 

 spring.version 3.1.4.RELEASE /spring.version 

 slf4j.version 1.6.6 /slf4j.version 

 /properties 

 dependencies 

 dependency 

 groupId junit /groupId 

 artifactId junit /artifactId 

 version 3.8.1 /version 

 scope test /scope 

 /dependency 

 !-- Spring -- 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-aop /artifactId 

 version ${spring.version} /version 

 /dependency 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-asm /artifactId 

 version ${spring.version} /version 

 /dependency 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-core /artifactId 

 version ${spring.version} /version 

 /dependency 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-beans /artifactId 

 version ${spring.version} /version 

 /dependency 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-context /artifactId 

 version ${spring.version} /version 

 /dependency 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-expression /artifactId 

 version ${spring.version} /version 

 /dependency 

 !-- spring end -- 

 !-- log -- 

 dependency 

 groupId log4j /groupId 

 artifactId log4j /artifactId 

 version 1.2.16 /version 

 /dependency 

 dependency 

 groupId org.slf4j /groupId 

 artifactId slf4j-api /artifactId 

 version ${slf4j.version} /version 

 /dependency 

 dependency 

 groupId org.slf4j /groupId 

 artifactId slf4j-log4j12 /artifactId 

 version ${slf4j.version} /version 

 /dependency 

 !-- dubbo -- 

 dependency 

 groupId com.alibaba /groupId 

 artifactId dubbo /artifactId 

 version 2.5.3 /version 

 /dependency 

 !-- zkclient -- 

 dependency 

 groupId com.github.sgroschupf /groupId 

 artifactId zkclient /artifactId 

 version 0.1 /version 

 /dependency 

 !-- zookeeper -- 

 dependency 

 groupId org.apache.zookeeper /groupId 

 artifactId zookeeper /artifactId 

 version 3.4.5 /version 

 /dependency 

 /dependencies 

 build 

 finalName dubbo-demo /finalName 

 plugins 

 !-- 非多个资源配置 start-- 

 plugin 

 groupId org.apache.maven.plugins /groupId 

 artifactId maven-compiler-plugin /artifactId 

 version 2.1 /version 

 configuration 

 source 1.5 /source 

 target 1.5 /target 

 encoding UTF-8 /encoding 

 failOnError false /failOnError 

 /configuration 

 /plugin 

 !-- 非多个资源配置 end-- 

 /plugins 

 /build 

 /project 

(3)、写服务接口,服务接口的实现

package com.test.dubboser; 

public interface ServiceDemo { 

public String say(String str); 

package com.test.dubboser; 

public class ServiceImp implements ServiceDemo{ 

 public String say(String str) { 

 System.err.println("server: "+str); 

 return "hello: "+str; 

(4)、向注册中心注册服务(将接口暴露出去),这里的注册中心是zookeeper

下面我们来看看,服务的配置也就是applicationProvider.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 

 !-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -- 

 dubbo:application name="hello-world-app-my" / 

 !-- 使用multicast广播注册中心暴露服务地址 -- 

 !-- dubbo:registry address="multicast://192.168.0.101:2181" / -- 

 !-- 使用zookeeper广播注册中心暴露服务地址 -- 

 dubbo:registry protocol="zookeeper" address="192.168.0.101:2181"/ 

 !--和上面的配置是一样的效果 -- 

 !-- dubbo:registry address="zookeeper://192.168.0.101:2181" / -- 

 !-- 本机 伪集群 测试 -- 

 !-- dubbo:registry protocol="zookeeper" address="192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183" / -- 

 !-- 用dubbo协议在20880端口暴露服务 -- 

 dubbo:protocol name="dubbo" port="20880" / 

 !-- 声明需要暴露的服务接口 -- 

 dubbo:service interface="com.test.dubboser.ServiceDemo" 

 ref="demoService" / 

 !-- 和本地bean一样实现服务 -- 

 bean id="demoService" / 

 /beans 

(5)、所有的准备都好了,下面我们启动服务提供者(提前启动zookeeper)

package com.test.dubboser; 

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(); 

ok这样我们的服务提供者就好了,是不是很乱,我感觉也有点,所以之后会有个自己的小总结……

我们的服务提供者好了,我们来看看我们的服务消费者,也就是调用我们这里的服务的程序……

3、服务消费者开发

服务消费者的编写还是按照服务提供者一样的顺序来

(1)、目录结构

dubbo 使用 学习二(spring+dubbo+zookeeper单机服务)详解架构师

(2)、maven的pom.xml配置文件获取需要的架包

 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.test /groupId 

 artifactId dubbocli /artifactId 

 version 0.0.1-SNAPSHOT /version 

 packaging jar /packaging 

 name dubboclient /name 

 url http://maven.apache.org /url 

 properties 

 project.build.sourceEncoding UTF-8 /project.build.sourceEncoding 

 spring.version 3.1.4.RELEASE /spring.version 

 slf4j.version 1.6.6 /slf4j.version 

 /properties 

 dependencies 

 dependency 

 groupId junit /groupId 

 artifactId junit /artifactId 

 version 3.8.1 /version 

 scope test /scope 

 /dependency 

 !-- Spring -- 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-aop /artifactId 

 version ${spring.version} /version 

 /dependency 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-asm /artifactId 

 version ${spring.version} /version 

 /dependency 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-core /artifactId 

 version ${spring.version} /version 

 /dependency 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-beans /artifactId 

 version ${spring.version} /version 

 /dependency 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-context /artifactId 

 version ${spring.version} /version 

 /dependency 

 dependency 

 groupId org.springframework /groupId 

 artifactId spring-expression /artifactId 

 version ${spring.version} /version 

 /dependency 

 !-- spring end -- 

 !-- log -- 

 dependency 

 groupId log4j /groupId 

 artifactId log4j /artifactId 

 version 1.2.16 /version 

 /dependency 

 dependency 

 groupId org.slf4j /groupId 

 artifactId slf4j-api /artifactId 

 version ${slf4j.version} /version 

 /dependency 

 dependency 

 groupId org.slf4j /groupId 

 artifactId slf4j-log4j12 /artifactId 

 version ${slf4j.version} /version 

 /dependency 

 !-- dubbo -- 

 dependency 

 groupId com.alibaba /groupId 

 artifactId dubbo /artifactId 

 version 2.5.3 /version 

 /dependency 

 !-- zkclient -- 

 dependency 

 groupId com.github.sgroschupf /groupId 

 artifactId zkclient /artifactId 

 version 0.1 /version 

 /dependency 

 !-- zookeeper -- 

 dependency 

 groupId org.apache.zookeeper /groupId 

 artifactId zookeeper /artifactId 

 version 3.4.5 /version 

 /dependency 

 /dependencies 

 build 

 finalName dubbo-demo /finalName 

 plugins 

 !-- 非多个资源配置 start-- 

 plugin 

 groupId org.apache.maven.plugins /groupId 

 artifactId maven-compiler-plugin /artifactId 

 version 2.1 /version 

 configuration 

 source 1.5 /source 

 target 1.5 /target 

 encoding UTF-8 /encoding 

 failOnError false /failOnError 

 /configuration 

 /plugin 

 !-- 非多个资源配置 end-- 

 /plugins 

 /build 

 /project 

注意:服务提供者 ,服务消费者以及安装的zookeeper保持相同的版本号,这样能省去很多麻烦,而不同版本的错误在前中有记录过可以看看……

(3)、服务消费者的配置applicationConsumer.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="consumer-of-helloworld-app-my" / 

 !-- 使用zookeeper广播注册中心暴露发现服务地址 -- 

 dubbo:registry protocol="zookeeper" address="192.168.0.101:2181" / 

 !-- 和上面的一样 -- 

 !-- dubbo:registry address="zookeeper://192.168.0.101:2181" / -- 

 !--本地伪集群配置 -- 

 !-- dubbo:registry protocol="zookeeper" address="192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183" / -- 

 !-- 生成远程服务代理,可以和本地bean一样使用demoService -- 

 dubbo:reference id="demoServicemy" interface="com.test.dubboser.ServiceDemo" / 

 /beans 

(4)、消费端,也就是调用服务端的方法

package com.test.dubbocli; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

import com.test.dubboser.ServiceDemo; 

public class Main { 

 public static void main(String[] args) throws InterruptedException { 

 int i=0; 

 while(i++ 5){ 

 run(); 

 Thread.sleep(3000); 

 public static void run(){ 

 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" }); 

 context.start(); 

 ServiceDemo demoServer = (ServiceDemo) context.getBean("demoServicemy"); 

 String str=demoServer.say("java ---- 

 System.err.println("res: "+str); 

注意:这里我们引入了一个类 com.test.dubboser.ServiceDemo 类,这个类是服务端的接口,所以在启动消费端的时候我们要把服务端生成jar包导入到消费端……

我们看到了消费端调用服务端的方法就和调用本地的方法一样方便,ok 例子就到这里……

4、整个开发流程:

(1)、安装zookeeper,修改dataDir 

(2)、开发服务端即服务提供者,当然前提示已经有了zookeeper dubbo spring 等jar包,这里使用了maven也就是pom.xml 配置

(3)、将服务端注册到注册中心暴露服务地址配置添加

(4)、使用dubbo协议将指定的服务端口暴露配置添加

(5)、声明需要暴露的服务接口

(6)、实现服务接口

(7)、启动服务端

这样 整个服务提供者就ok了

(1)、服务消费者开发,同样需要zookeeper dubbo spring 等相关的jar包,这里使用的是maven所以配置pom.xml

(2)、使用zookeeper广播注册中心发现暴露的服务地址的配置添加

(3)、生成 远程代理的配置添加

(4)、将服务端生成jar包引入

(5)、调用服务端服务

5、注意点

(1)、三个地方提到了zookeeper 、zookeeper的安装、服务端注册、客户端注册 这三个地方的zookeeper使用同一版本,这样能省去一些不必要的错误!!!

(2)、记得在服务消费者引入服务提供者的jar,也就是要把服务端打成jar包引入到服务消费者!!!

6、使用dubbo、zookeeper到底做了什么

我们在网上能查到dubbo、zookeeper是做什么的,这里不说的那么具体还详细,第一你能看到官网的解释,第二我也刚开始学习不能误人子弟,下面就是我个人的简单理解:

zookeeper 是注册中心,我们写的服务注册到注册到zookeeper,zookeeper将服务端的相关配置存储并管理更新,而服务消费者链接到zookeeper注册中心通过zookeeper这个注册中心获取服务端的相关信息,生成远程代理服务就调用服务端提供的方法了!!!这是我简单的一个理解,当然zookeeper还有软负载等功能,但是具体的怎么实现的也不太清,先知道的自己可以查看资料学习!!!

7、demo源码下载

(1)、zookeeper 安装包下载:http://download.csdn.net/detail/qh_java/9655026

               zookeeper安装包下载地址

(2)、服务端服、服务消费者以及服务端的jar包(maven工程)下载:http://download.csdn.net/download/qh_java/9655491

             dubbo zookeeper小demo下载

ok到此我们简单的小demo学习就ok了 下片我们看看本地伪集群的配置

 

下面推荐一篇学习理解zookeeper的文章:https://mp.weixin.qq.com/s?__biz=MzAwNjA3NDMyOA== mid=2659762989 idx=4 sn=6f26a96969cb3c06202f8ab24c5b1921 chksm=806e999ab719108c648504ec56bcc8e51a8a5f36cb91d2a6a8826879aee8063e9228d493c1b5 scene=0 key= ascene=7 uin= devicetype=android-23 version=26031b31 nettype=WIFI

zookeeper学习地址:一篇不错的zookeeper学习文章

6870.html

架构架构师架构设计