学习目标
- 分布式系统中的相关概念
- Dubbo概述
- Dubbo快速入门
- Dubbo高级特性
Dubbo
是一款高性能、轻量级的开源Java RPC框架,提供面向接口代理的高性能RPC调用、智能负载均衡、服务自动注册和发现、运行期流量调度、可视化服务治理和运维等功能。
互联网项目特点:
单体架构:把所有的功能模块都放在一起,部署在服务器上,形成一个app。
部署在一个tomcat服务器上,叫做单机单体架构;也可以部署在多个服务器上,称为多机单体架构。
优点:
缺点:
分布式架构存在的问题:
特点:
Dubbo是SOA时代的产物,SpringCloud是微服务时代的产物。
Dubbo是阿里巴巴公司开源的一个高性能、轻量级的Java RPC框架。
致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
官网: http://dubbo.apache.org
扩展:Dubbo是如何解析@Service、@Reference的?
Zookeeper是Apacahe Hadoop的子项目,是一个树型的目录服务,支持变更推送,适合作为Dubbo服务的注册中心,工业强度较高,可用于生产环境,并推荐使用。
① 环境准备
ZooKeeper服务器是用Java创建的,它运行在JVM之上。需要安装JDK 8或更高版本。
② 上传
将下载的ZooKeeper放到/opt/zookeeper目录下
# 上传zookeeper安装包到服务器
put f:/setup/apache-zookeeper-3.5.6-bin.tar.gz
# 在/opt目录下创建zookeeper目录
mkdir /opt/zookeeper
# 将zookeeper安装包移动到 /opt/zookeeper
mv apache-zookeeper-3.5.6-bin.tar.gz /opt/zookeeper/
③ 解压
将tar包解压到/opt/zookeeper目录下
tar -zxvf apache-zookeeper-3.5.6-bin.tar.gz -C /opt/zookeeper/
① 配置zoo.cfg
进入到conf目录拷贝一个zoo_sample.cfg并完成配置
# 进入到conf目录
cd /opt/zookeeper/apache-zookeeper-3.5.6-bin/conf/
# 拷贝 (配置文件必须叫zoo.cfg才能生效)
cp zoo_sample.cfg zoo.cfg
修改zoo.cfg
# 打开目录
cd /opt/zookeeper/
# 创建zookeeper存储目录
mkdir zkdata
# 修改zoo.cfg
vim /opt/zookeeper/apache-zookeeper-3.5.6-bin/conf/zoo.cfg
# 修改存储目录
将zk默认存储数据的临时目录dataDir=/tmp/zookeeper,修改为:dataDir=/opt/zookeeper/zkdata
2181端口默认是ZK的客户端端口号,记得防火墙把这个端口给开放出来,不然客户端连接不上。
# 添加2181端口到防火墙允许列表
firewall-cmd --zone=public --add-port=2181/tcp --permanent
# 立即生效
firewall-cmd --reload
# 查看Linux防火墙允许通过的端口号
firewall-cmd --zone=public --list-ports
② 启动ZooKeeper
cd /opt/zookeeper/apache-zookeeper-3.5.6-bin/bin/
# 启动
./zkServer.sh start
③ 查看ZooKeeper状态
# 查看ZooKeeper状态
./zkServer.sh status
# 停止ZooKeeper
./zkServer.sh stop
# 开启ZooKeeper
./zkServer.sh start
# 重启ZooKeeper
./zkServer.sh restart
zookeeper没有启动。
zookeeper启动成功。standalone代表zk没有搭建集群,现在是单节点。
1)创建一个空项目dubbo-pro,里面新建两个maven项目,dubbo-service (producer) 和 dubbo-web (consumer)
项目结构:
2)复制下面的依赖到dubbo-web和dubbo-service的pom.xml中。注意web模块是最后打成war包部署到tomcat容器上的,需要导入启动tomcat的插件依赖。而service模块打成jar包,不需要启动tomcat,因此不需要复制导入tomcat的插件依赖。
dubbo-web:
<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.0modelVersion>
<groupId>com.itheimagroupId>
<artifactId>dubbo-webartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<maven.compiler.encoding>UTF-8maven.compiler.encoding>
<java.version>1.8java.version>
<spring.version>5.1.9.RELEASEspring.version>
<springmvc.version>5.1.9.RELEASEspringmvc.version>
<dubbo.version>2.7.4.1dubbo.version>
<zookeeper.version>4.0.0zookeeper.version>
properties>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${springmvc.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>com.itheimagroupId>
<artifactId>dubbo-serviceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>8000port>
<path>/path>
configuration>
plugin>
plugins>
build>
project>
dubbo-service:
<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.0modelVersion>
<groupId>com.itheimagroupId>
<artifactId>dubbo-serviceartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<maven.compiler.encoding>UTF-8maven.compiler.encoding>
<java.version>1.8java.version>
<spring.version>5.1.9.RELEASEspring.version>
<springmvc.version>5.1.9.RELEASEspringmvc.version>
<dubbo.version>2.7.4.1dubbo.version>
<zookeeper.version>4.0.0zookeeper.version>
properties>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${zookeeper.version}version>
dependency>
dependencies>
project>
3)service模块中创建com.itheima.service.UserService和com.itheima.service.impl.UserServiceImpl:
import org.springframework.stereotype.Service;
public interface UserService {
public String sayHello();
}
import com.itheima.service.UserService;
@Service
public class UserServiceImpl implements UserService {
public String sayHello() {
return "hello dubbo!~";
}
}
4)创建Spring的核心配置文件applicationContext.xml和log4j.properties:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
">
<context:component-scan base-package="com.itheima.service"/>
beans>
# DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootLogger=info, stdout,file
# My logging configuration...
#log4j.logger.com.tocersoft.school=DEBUG
#log4j.logger.net.sf.hibernate.cache=debug
## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=../logs/iask.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n
5)dubbo-web模块中要引入dubbo-service模块的依赖,创建com.itheima.controller.UserController,创建SpringMVC核心配置文件springmvc.xml和log4j.properties,创建webapp、WEB-INF和web.xml:
<dependency>
<groupId>com.itheimagroupId>
<artifactId>dubbo-serviceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
// 注入Service
@Autowired
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello() {
return userService.sayHello();
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.itheima.controller"/>
beans>
# DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootLogger=info, stdout,file
# My logging configuration...
#log4j.logger.com.tocersoft.school=DEBUG
#log4j.logger.net.sf.hibernate.cache=debug
## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=../logs/iask.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:spring/applicationContext*.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/springmvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
web-app>
6)先把依赖的service模块通过maven插件进行install,再对web模块进行run
如果出现Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project dubbo-service: Compilation failure错误。
解决办法是 settings -> build,Execution —> compiler -->java compiler 修改jdk版本,可参考:https://blog.csdn.net/qq_39968986/article/details/113461797
如果还不行可在pom.xml指定项目compiler-plugin的版本,jdk的版本:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
测试访问http://localhost:8000/user/sayHello.do
上面只是做了基本的Spring和SpringMVC整合操作,web模块本地依赖于service模块,利用maven进行了模块拆分和引用。
此时并不是分布式架构,而是对单体架构进行了模块化开发。
因为分布式需要各模块间可以进行RPC远程调用(protocol://ip:port/access_path),每个模块能够独立地启动,独立地对外提供服务。
接下来我们将它们改造成分布式SOA架构,先改造service模块,再改造web模块。
服务之间的调用使用注册中心,同步调用使用Feign,异步调用使用MQ。
1)先引入Dubbo依赖坐标 和 两个Zookeeper注册中心客户端依赖坐标。
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${zookeeper.version}version>
dependency>
2)为了使dubbo-service对外独立启动,需要将默认jar的打包方式改成war,再添加一个tomcat的启动插件,将端口设置为9000。
<groupId>com.itheimagroupId>
<artifactId>dubbo-serviceartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>9000port>
<path>/path>
configuration>
plugin>
plugins>
build>
3)在实现类上使用@Service(org.apache.dubbo.config.annotation.Service),Dubbo 2.7.7以后@Service被@DubboService替代。
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Service;
//@Service // bean定义,将该类的对象创建出来,放到Spring的IOC容器中。
@Service // 将这个类提供的方法(服务)对外发布。将访问地址(ip、端口、访问路径)注册到ZK注册中心
public class UserServiceImpl implements UserService {
public String sayHello() {
return "hello dubbo!~";
}
}
扩展:Dubbo:@DubboService和@Service、@DubboReference和@Reference的区别和关系,Dubbo学习之DubboService
4)接着在applicationContext.xml中先引入dubbo的约束信息。
<beans xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
">
beans>
再配置Dubbo项目的名称、注册中心的地址、dubbo包扫描路径。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
">
<context:component-scan base-package="com.itheima.service"/>
<dubbo:application name="dubbo-service"/>
<dubbo:registry address="zookeeper://192.168.8.100:2181"/>
<dubbo:annotation package="com.itheima.service.impl"/>
beans>
5)将dubbo-web模块的webapp目录复制到dubbo-service模块的main文件夹下,将web.xml中SpringMVC的配置信息去掉。web.xml的version版本如果是4.0,一定要删去Lister监听器。
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:spring/applicationContext*.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
web-app>
1)之前dubbo-web模块的pom.xml中对service模块的本地调用可以先注释掉了,这里也是需要引入dubbo的那三个依赖坐标。
此时UserController会报错,先创建一个UserService接口不让它报错。
public interface UserService {
String sayHello();
}
web.xml中去掉Spring的配置
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/springmvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
web-app>
2)使用dubbo远程注入注解@Reference(org.apache.dubbo.config.annotation.Reference)代替本地注入注解@Autowired。
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
// 注入Service
//@Autowired // 本地注入
/*
dubbo 2.7版本后 @Reference已弃用,建议使用@DubboReference
1.从zookeeper注册中心获取UserService的访问url
2.进行远程调用RPC
3.将结果封装为一个代理对象。给变量赋值
*/
@Reference // 远程注入
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello() {
return userService.sayHello();
}
}
3)在springmvc.xml中配置dubbo和注册中心地址。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.itheima.controller"/>
<dubbo:application name="dubbo-web"/>
<dubbo:registry address="zookeeper://192.168.8.100:2181"/>
<dubbo:annotation package="com.itheima.controller"/>
beans>
4)运行dubbo-web,重新访问http://localhost:8000/user/sayHello.do,可以打印输出结果,但是后台报了一个本地22222端口占用的错。
qos其实是dubbo用于做远程监控的一个组件,而这两个模块都会有一个qos,都部署在同一台机器上就会出现这个错误。但实际生成环境下,这两个模块会部署在不同的服务器上,就不会报错了。
我们可以在核心配置文件springmvc.xml中对端口号进行修改。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.itheima.controller"/>
<dubbo:application name="dubbo-web">
<dubbo:parameter key="qos.port" value="33333"/>
dubbo:application>
<dubbo:registry address="zookeeper://192.168.8.100:2181"/>
<dubbo:annotation package="com.itheima.controller"/>
beans>
5)重启dubbo-web服务,重新访问http://localhost:8000/user/sayHello.do,可以打印输出结果,后台也不报错了。
6)思考:在解除了web与service模块的依赖关系后,web中的UserService接口就会报错,解决方法是在web和service中定义同样声明的接口,包名也要一样。但是实际开发环境下,这两个模块可能是不同团队开发的,声明很多个接口就无法保证两边一致。
解决方案:将公共接口抽取出来,形成独立的模块,让web和service分别去依赖这个公共接口。
7)在dubbo-pro模块下新建一个maven模块dubbo-interface,然后在里面创建com.itheima.service.UserService接口。
之后进行修改pom.xml后,对dubbo-interface模块进行maven install。
<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.0modelVersion>
<groupId>com.itheimagroupId>
<artifactId>dubbo-interfaceartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<maven.compiler.encoding>UTF-8maven.compiler.encoding>
<java.version>1.8java.version>
properties>
project>
8)删除web和service模块中的UserService接口,在各自的pom.xml中添加dubbo-interface公共接口依赖坐标。
dubbo-web的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.0modelVersion>
<groupId>com.itheimagroupId>
<artifactId>dubbo-webartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<maven.compiler.encoding>UTF-8maven.compiler.encoding>
<java.version>1.8java.version>
<spring.version>5.1.9.RELEASEspring.version>
<springmvc.version>5.1.9.RELEASEspringmvc.version>
<dubbo.version>2.7.4.1dubbo.version>
<zookeeper.version>4.0.0zookeeper.version>
properties>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${springmvc.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>com.itheimagroupId>
<artifactId>dubbo-interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>8000port>
<path>/path>
configuration>
plugin>
plugins>
build>
project>
dubbo-service的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.0modelVersion>
<groupId>com.itheimagroupId>
<artifactId>dubbo-serviceartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<maven.compiler.encoding>UTF-8maven.compiler.encoding>
<java.version>1.8java.version>
<spring.version>5.1.9.RELEASEspring.version>
<springmvc.version>5.1.9.RELEASEspringmvc.version>
<dubbo.version>2.7.4.1dubbo.version>
<zookeeper.version>4.0.0zookeeper.version>
properties>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>com.itheimagroupId>
<artifactId>dubbo-interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>9000port>
<path>/path>
configuration>
plugin>
plugins>
build>
project>
为UserServiceImpl重新导包,修改返回的字符串,重启两个模块并访问。
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Service;
//@Service // bean定义,将该类的对象创建出来,放到Spring的IOC容器中。
// 2.7.7提供了@EnableDubbo
@Service // 将这个类提供的方法(服务)对外发布。将访问地址(ip、端口、访问路径)注册到ZK注册中心
public class UserServiceImpl implements UserService {
public String sayHello() {
return "Hello Dubbo RPC Zookeeper!~";
}
}