Maven + Zookeeper + Dubbo 搭建简单分布式项目

mac2024-05-30  52

前言:对于第一次接触分布式dubbo框架的小伙伴,可先自行了解一下dubbo的相关原理和运行机制。博主推荐一篇不错的博文,小伙伴们可以阅览后,再学习本文dubbo分布式项目搭建。

dubbo基本原理:https://blog.csdn.net/en_joker/article/details/89946034

本文借鉴以下博客:Dubbo入门---搭建一个最简单的Demo框架

1. Zookeeper 安装配置

下载地址:https://zookeeper.apache.org/releases.html

官网安装教程:http://zookeeper.apache.org/doc/r3.5.5/zookeeperStarted.html

百度网盘链接:https://pan.baidu.com/s/141kQcKae7ay7-O7WVAORsg   提取码:jaij 

下载解压后,将bin下的zoo_sample.cfg文件复制一份并修改文件名为zoo.cfg,编辑该文件配置信息如下所示:

# The number of milliseconds of each tick 心跳间隔 毫秒每次 tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting anacknowledgement syncLimit=5 # the directory where the snapshot isstored. //镜像数据位置 dataDir=D:\\layduo\\dubbo\\data #日志位置 dataLogDir=D:\\layduo\\dubbo\\logs # the port at which the clients willconnect 客户端连接的端口 clientPort=2181

完成以上配置后,启动Zookeeper服务,进入安装目录bin下,输入以下命令:

zkServer.cmd

重新cmd开启另一个窗口,输入以下命令查看启动状态:

jps -l -v

进入安装目录bin下,输入以下命令连接Zookeeper:

zkCli.cmd 127.0.0.1:2181

2. dubbo-admin安装配置 

由于dubbo被阿里捐献给了apache,这次安装admin时,参考网上的资料,地址还是停留在之前的链接,踩了不少坑,这里记录下。

dubbo-admin下载地址:

地址一:https://github.com/apache/incubator-dubbo/releases 该地址2.6版本以上的包中没有dubbo-admin ,2.5x版本的有。

地址二:https://github.com/apache/incubator-dubbo-ops 该地址中的dubbo-admin(默认端口7001)模块被单独拎出来了,springboot方式启动,可以直接运行main方法,或者使用 java -jar 方式启动,很方便,有github账号的可以fork一下。

现成的dubbo-admin.war百度网盘链接:https://pan.baidu.com/s/1ZeD_FJbwA58ji1msfVz41A  提取码:evgq 

下载后把dubbo-admin通过mvn命令行将dubbo-admin打成war包,进入dubbo-admin目录下,通过以下命令打包: 

mvn package -Dmaven.skip.test=true

 接着将打包好war包放在tomcat的webapps目录下,启动tomcat,war包将会自动解压。然后修改WEB-INF下的dubbo.properties文件,初始root和guest账户密码:

dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.admin.root.password=root dubbo.admin.guest.password=guest

最后测试dubbo-admin访问是否能成功,浏览器访问http://ip:端口/dubbo-admin,输入账号密码进入主页面说明配置成功。

3. 项目搭建(开发环境jdk1.7 + maven + spring + dubbo + zookeeper + idea)

项目结构如下所示:其中dubbo是父级项目,里面的三个都是子项目,dubbo-api是接口管理,dubbo-provider是服务提供方,dubbo-consumer是服务消费方。

 

 

 dubbo项目根路径下的pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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.layduo</groupId> <artifactId>dubbo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>dubbo-api</module> <module>dubbo-provider</module> <module>dubbo-consumer</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <!-- spring版本号 --> <spring.version>4.2.5.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.2.8</mybatis.version> <!-- mysql驱动版本号 --> <mysql-driver.version>5.1.29</mysql-driver.version> <!-- log4j日志包版本号 --> <slf4j.version>1.7.18</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <!-- 添加junit4依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- 指定范围,在测试时才会加载 --> <scope>test</scope> </dependency> <!-- 添加jstl依赖 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- 添加spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</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-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- 添加mybatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- 添加mybatis/spring整合包依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- 添加mysql驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-driver.version}</version> </dependency> <!-- 添加数据库连接池依赖 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- 添加fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.22</version> </dependency> <!-- 添加日志相关jar包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</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> <!-- log end --> <!-- 映入JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency> <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> </dependencies> <build> <finalName>dubbo</finalName> </build> </project>

 dubbo-api下的pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>dubbo</artifactId> <groupId>com.layduo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-api</artifactId> <packaging>war</packaging> <name>dubbo-api Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>dubbo-api</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>

dubbo-provider下的pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>dubbo</artifactId> <groupId>com.layduo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-provider</artifactId> <dependencies> <dependency> <groupId>com.layduo</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>

dubbo-consumer下的pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>dubbo</artifactId> <groupId>com.layduo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-consumer</artifactId> <dependencies> <dependency> <groupId>com.layduo</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>

注意:dubbo-provider和dubbo-consumer中pom.xml记得添加一下依赖:

<dependencies> <dependency> <groupId>com.layduo</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>

api声明接口,如下所示:

package com.layduo.dubbo.demo; import java.util.List; /** * Created by wy on 2017/4/13. */ public interface DemoService { List<String> getPermissions(Long id); }

 provider提供服务,如下所示:

package com.layduo.dubbo.demo.impl; import com.alibaba.dubbo.config.annotation.Service; import com.layduo.dubbo.demo.DemoService; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component("demoService") @Service(version = "1.0") public class DemoServiceImpl implements DemoService { @Override public List<String> getPermissions(Long id) { List<String> demo = new ArrayList<String>(); demo.add(String.format("Permission_%d", id - 1)); demo.add(String.format("Permission_%d", id)); demo.add(String.format("Permission_%d", id + 1)); return demo; } }

 

provider.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-admin 或 dubbo-monitor 会显示这个名字,方便辨识--> <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/> <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper--> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!--设置超时时间--> <dubbo:provider timeout="600000"/> <!--1、通过扫描servive注解注入实现类的bean--> <!--扫描dubbo的service注解,在接口实现类上使用注解注入bean --> <!-- 配置dubbo注解识别处理器,不指定包名的话会在spring bean中查找对应实例的类配置了dubbo注解的 --> <dubbo:annotation package="com.layduo.dubbo.demo.impl"/> <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口--> <dubbo:service interface="com.layduo.dubbo.demo.DemoService" ref="demoService" protocol="dubbo" /> <!--2、通过手动配置实现类的bean注入,每个接口实现类都得注入,不推荐这样的写法--> <!--具体实现该接口的 bean--> <!--<bean id="demoService" class="com.layduo.dubbo.demo.impl.DemoServiceImpl"/>--> </beans>

consumer.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服务--> <dubbo:application name="demotest-consumer" owner="programmer" organization="dubbox"/> <!--设置超时时间--> <dubbo:consumer timeout="600000"/> <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送--> <dubbo:registry address="zookeeper://localhost:2181"/> <!--使用 dubbo 协议调用定义好的 api.DemoService 接口--> <dubbo:reference id="demoService" interface="com.layduo.dubbo.demo.DemoService"/> </beans>

调用远程服务前,请确保服务先要启动。

启动远程服务:

package com.layduo.dubbo.demo.impl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Provider { private static final Logger logger = LoggerFactory.getLogger(Provider.class); public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); logger.info(context.getDisplayName() + ": here"); context.start(); logger.info("服务已经启动..."); System.in.read(); } }

启动消费者调用远程服务:

package com.layduo.dubbo.consumer; import com.layduo.dubbo.demo.DemoService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer { private static final Logger logger = LoggerFactory.getLogger(Consumer.class); //使用@Reference注解注入dubbo服务 public static void main(String[] args){ //测试常规服务 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); context.start(); logger.info("consumer start"); DemoService demoService = context.getBean(DemoService.class); logger.info("consumer"); System.out.println(demoService.getPermissions(1L)); } }

 最后浏览器登录dubbo-admin查看服务提供者和消费者的情况:

 需要项目源码的可以用git从https://github.com/builthuLin/dubbo.git 克隆到自己本地运行~

最新回复(0)