说明:大概是3年前在一个Saas平台中接触到dubbo,此博文是复习笔记,分享给需要的读者。学习dubbo要有一定的java和maven基础,因为此演示全程都离不开maven。此演示侧重dubbo原理图中的Provider—>Registy<—>Consumer 实战搭建,并且编写演示代码及dubbo平台管理, 笔者将其称为” Dubbo第二炮”。(很久没玩dubbo了,博文用于复习与交流)
此演示内容如下(这里的演示相当于手把手教妹子):

1.dubbo环境搭建简介
前言:Dubbo框架如何搭建?
有过SSH,SSMJ,PAFA,JALOR等框架经验的道友,估计都有搭建框架的经验,不论你是学习时还是工作时搭建的。它们都有一个共性,基本上离不开jar包导入和配置文件的编写,当然咯,现在傻B(SpringBoot简称SB)和maven进行了大量的简化,但是仍然离不开jar包和配置文件的管理。
搭建框架本质就是如何正确消费别人的提供的技术。这些技术构建成jar,库等供大家使用。
此演示环境说明:
Eclipse : Version: Kepler Service Release 2
Tomcat 5.23
Jdk: java version “1.8.0_31”
Zookeeper服务器 zookeeper3.4.12
Maven服务器apache-maven-3.0.5 (看清楚,这里指服务器不是指jar包版本)
2.dubbo工作原理图

3.在eclipse中新建4个maven工程
这些工程分别是
user(代表父工程,管理和聚合所有的工程)
user-api(编写接口和pojo或domain)
user-provider(服务提供者,对应dubbo原理图中的Provider)
user-consumer(服务消费者,调用user-provider中提供/暴露的服务或接口)

(1) user工程中主要资源pom.xml

===>>>>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">
<!-- 作为maven父工程,管理公用的依赖资源和聚合相关联的项目 -->
<modelVersion>4.0.0</modelVersion>
<groupId>org.yl.dubbo</groupId>
<artifactId>user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- maven中央工厂,如果本地仓库settings配置了镜像mirror则会覆盖url,从中央资源工厂下载资源时通过Nexus私服管理 -->
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- maven聚合 -->
<modules>
<module>../user-api</module>
<module>../user-provider</module>
</modules>
<!-- 公共依赖管理 -->
<dependencyManagement>
<dependencies>
<!-- 配置Spring 相应的jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-bean</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
<!-- 配置注册中心(zookeeper相关联的依赖)所需要的jar -->
<dependency>
<groupId>zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
<!-- 配置阿里儿子jar -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.10</version>
</dependency>
<!-- 测试依赖jar -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 插件管理 -->
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_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-jar-plugin</artifactId>
<version>3.0.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>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
(2) user-api工程

===>>> User.java 代码
package org.yl.dubbo.user_api.pojo;
import java.io.Serializable;
public class User implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = -1279924345353308732L;
private Long userId;
private String username;
private String password;
private String sex;
private String telephone;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("{userId:").append(this.userId).append(" ,")
.append("username:").append(this.username).append(" ,")
.append("password:").append(this.password).append(" ,")
.append("sex:").append(this.sex).append(" ,")
.append("telephone:").append(this.telephone)
.append("}");
return sb.toString();
}
}
===>>>> UserService.java代码
package org.yl.dubbo.user_api.service;
import org.yl.dubbo.user_api.pojo.User;
public interface UserService {
User findUserById(Long userId);
int addUser(User user);
}
===>>>> user-appi/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>
<!-- 继承 -->
<parent>
<groupId>org.yl.dubbo</groupId>
<artifactId>user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 相对路径 -->
<relativePath>../user/pom.xml</relativePath>
</parent>
<artifactId>user-api</artifactId>
<name>user-api</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(3) user-provider工程

====>>>> UserProvider2Application.java代码
package org.yl.dubbo.user.impl;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 初始化Spring容器,用于启动user-provider项目来提供服务
* @author 拈花为何不一笑
*
*此演示"dubbo环境搭建" 执行顺序: zookeeper –->
* user-provider(运行UserProvider2Application.java)-->
* user-consumer(运行ConsumerUserService.java)
*/
public class UserProvider2Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-provider.xml");
context.start();
System.out.println("user-provider: " + context.getDisplayName());
System.out.println("===>>>> 启动user-provider应用...");
try {
System.in.read();
} catch (IOException e) {
System.out.println("===>>>> UserProvider2Application异常...");
e.printStackTrace();
}
}
}
===>>>> UserServiceImpl.java代码
package org.yl.dubbo.user.impl;
import org.yl.dubbo.user_api.pojo.User;
import org.yl.dubbo.user_api.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public User findUserById(Long userId) {
System.out.println("===>>> 模拟查询用户");
User user = new User();
user.setUserId(userId);
user.setUsername("lulu");
user.setSex("female");
user.setTelephone("13656821236");
return user;
}
@Override
public int addUser(User user) {
System.out.println("===>>> 添加一个用户");
return 0;
}
}
===>>>> application-provider.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
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="user-provider" owner="admin" organization="dubbox" />
<!-- 使用zookeeper注册中心,注册服务提供给其它项目或模块使用即服务发布 -->
<dubbo:registry address="zookeeper://localhost:2181" client="zkclient" />
<!-- 用dubbo协议暴露20880端口 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 服务提供者的接口暴露 -->
<dubbo:service interface="org.yl.dubbo.user_api.service.UserService" ref="userService" />
<!-- 服务提供者的接口实现的bean -->
<bean id="userService" class="org.yl.dubbo.user.impl.UserServiceImpl"/>
<!--
<dubbo:reference id="" interface="" check="false"/>
<bean id="userService" class="com.alibaba.dubbo.governance.service.impl.UserServiceImpl">
<property name="rootPassword" value="${dubbo.admin.root.password}"/>
<property name="guestPassword" value="${dubbo.admin.guest.password}"/>
</bean>
-->
</beans>
===>>>> user-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">
<modelVersion>4.0.0</modelVersion>
<!-- 继承 -->
<parent>
<groupId>org.yl.dubbo</groupId>
<artifactId>user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 相对路径 -->
<relativePath>../user/pom.xml</relativePath>
</parent>
<artifactId>user-provider</artifactId>
<name>user-provider</name>
<!-- 依赖的jar -->
<dependencies>
<!-- 引入依赖项目user-api打成的jar包 -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>user-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- spring相关联的jar(发现依赖父类工程user其中一个spring jar其它的也依赖进来了) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- 依赖dobbo jar -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- zookeeper相关的jar -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<!-- 测试相关联的jar -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 依赖的插件 -->
<build>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(4) user-consumer工程

===>>>> ConsumerUserService.java代码
package org.yl.dubbo.user_consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.yl.dubbo.user_api.pojo.User;
import org.yl.dubbo.user_api.service.UserService;
/**
* 消费者,调用提供者提供的服务
* 即user-consumer工程调用user-provider工程(这里采用dubbo提供的RPC)
* @author 拈花为何不一笑
*
* 此演示"dubbo环境搭建" 执行顺序: zookeeper –->
* user-provider(运行UserProvider2Application.java)-->
* user-consumer(运行ConsumerUserService.java)
*/
public class ConsumerUserService {
//通过Spring来启动user-consumer工程
public static void main(String[] args) {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-consumer.xml");
context.start();
System.out.println("user-consumer: " + context.getDisplayName());
System.out.println("===>>>> 启动user-consumer应用...");
//获取远程服务代理(这玩意跟EJB中的JNDI很相似)
UserService userService = (UserService) context.getBean("userService");
// 调用远程服务即user-provider提供的服务
User userResult = (User)userService.findUserById(1012L);
System.out.println("===>>>> userResult: " + userResult);
}
}
===>>>> application-consumer.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
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="user-consumer" owner="admin" organization="dubbox" />
<!-- 使用zookeeper注册中心,用于访问注册中心注册的的服务即服务订阅 -->
<dubbo:registry address="zookeeper://localhost:2181" client="zkclient" />
<!-- 调用注册的服务或接口 -->
<dubbo:reference id="userService" interface="org.yl.dubbo.user_api.service.UserService" check="false"/>
<!--
<dubbo:reference id="" interface="" check="false"/>
<bean id="userService" class="com.alibaba.dubbo.governance.service.impl.UserServiceImpl">
<property name="rootPassword" value="${dubbo.admin.root.password}"/>
<property name="guestPassword" value="${dubbo.admin.guest.password}"/>
</bean>
-->
</beans>
===>>>> user-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">
<modelVersion>4.0.0</modelVersion>
<!-- 继承 -->
<parent>
<groupId>org.yl.dubbo</groupId>
<artifactId>user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 相对路径 -->
<relativePath>../user/pom.xml</relativePath>
</parent>
<artifactId>user-consumer</artifactId>
<name>user-consumer</name>
<!-- 依赖的jar -->
<dependencies>
<!-- 引入依赖项目user-api打成的jar包 -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>user-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- spring相关联的jar(发现依赖父类工程user其中一个spring jar其它的也依赖进来了) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- 依赖dobbo jar -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- zookeeper相关的jar -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 依赖的插件 -->
<build>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.maven工程编译,打包,安装(complie, package, install)
关于maven怎么使用,这里不详细,不会的自己去学习!贴一张图,给上面四个工程编译,打包和安装….

- 启动服务器和应用项目
顺序:zookeeper–> tomcat --> user-provider工程中的UserProvider2Application.java程序—>user-consumer工程中的ConsumerUserService.java程序
这其中,有遇到的问题及解决方案,请仔细阅读下面内容
(1)启动zookeeper服务器
双击zkServer.cmd (windwos系统中), 不知道怎么玩zookeeper看笔者另一篇博文“dubbo第一炮”

成功启动后如下图:

(2)启动tomcat服务器
说明tomcat服务器中部署了dubbo-admin.war应用项目,可参考笔者另一篇博文”dubbo第一炮”
在tomcat安装目录\bin 中找到startup.bat 双击即可启动(无需改监听端口),成功启动后如下图:

(3)启动应用项目user-provider
运行UserProvider2Application.java程序即可启动user-provider工程
成功如下图

遇到问题及解决方案(由于笔者落掉了配置导致),如下图,具体解决方案在图中已提供


(4) 启动应用项目user-consumer
运行ConsumerUserService.java程序即可启动user-consumer工程
成功如下图

遇到问题及解决方案(由于笔者落掉了配置导致),如下图,具体解决方案在图中已提供

-
服务器和应用项目信息分析或展示
(1)zookeeper服务器与user-provider和user-consumer
a) Zookeeper与user-provider工程连接信息

b) Zookeeper与user-consumer工程连接信息

(2)tomcat服务器中dubbo-admin应用项目



希望与大家交流,探讨。谢谢! @author;拈花为何不一笑。敬请观看即将出炉的 博文”dubbo第三炮”

本文是一篇Dubbo框架的复习笔记,详细介绍了如何搭建Provider到Consumer的环境,包括Eclipse中创建4个Maven工程,分别对应用户接口、服务提供者和服务消费者。文章还涉及Zookeeper的配置和使用,以及在遇到问题时的解决方案,帮助读者理解Dubbo的工作原理和实际操作。
989

被折叠的 条评论
为什么被折叠?



