搭建环境
开发工具:IntelliJ IDEA 2021.3.2
JDK版本:jdk 1.8
Spring版本:5.0.2.RELEASE
Dubbo版本:2.7.3
zookeeper版本:apache-zookeeper-3.5.10
tomcat版本:apache-tomcat-8.5.82
前期准备
下载安装zookeeper
②下好之后解压
③进入conf目录下,复制一份zoo_sample.cfg到当前目录下并重命名为zoo.cfg
④进入bin目录,双击zkServer.cmd启动zookeeper服务即可使用
下载安装tomcat
②下载解压即可使用
项目搭建
创建空maven项目
①File -> New -> Project -> Maven -> Next ->Finish创建项目作为父项目
②注意填写GroupId和Version,整个项目的这两个属性都一样
③删除src包
创建接口项目(公共项目)
①右建点击父项目 New -> Moudle -> Maven -> Next
②填写项目名,GroupId和Version,点击Finish 创建接口项目
③创建一个接口
package com.agi.api; public interface IDemoService { public String getName(); }
创建提供者项目
①右建点击父项目 New -> Moudle -> Maven -> Next
②填写项目名,GroupId和Version,点击Finish 创建接口项目
③导入Spring依赖
<!--spring依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.2.RELEASE</version> </dependency>
④导入Servlet依赖
<!--servlet依赖--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
⑤导入dubbo依赖
<!--dubbo依赖--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.3</version> </dependency>
⑥导入zookeeper依赖
<!--zookeeper依赖--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.10</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-client</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.1.0</version> </dependency>
⑦导入日志依赖
<!--日志--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.8.0-alpha2</version> </dependency>
⑧接口项目依赖
<!--接口层依赖--> <dependency> <groupId>com.agi</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
⑨点击File -> Project Structure -> Moudles 创建webapp,WEB-INF和web.xml
⑩编写业务层代码
package com.agi.Impl; import com.agi.api.IDemoService; import org.apache.dubbo.config.annotation.Service; //注意,这是dubbo的@Service注解 @Service public class DemoService implements IDemoService { @Override public String getName() { return DemoService.class.getName(); } }
⑪编写dubbo配置文件,在resource文件夹下创建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://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-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--定义服务提供方名称--> <dubbo:application name="dubbo-provider"> <dubbo:parameter key="qos.port" value="22223"/> </dubbo:application> <!--注册中心地址--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!--指明使用的协议--> <dubbo:protocol name="dubbo" port="20881"/> <!--包扫描(dubbo的@Service注解)--> <dubbo:annotation package="com.agi.impl"/> </beans>
⑫编写web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--spring配置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:provider.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
⑬配置Artifacts,选择dubbo-provider
⑭配置tomcat,最后点击Fix之后删除Application context里面的前缀,访问路径上不加前缀也可以访问
创建消费者项目
①右建点击父项目 New -> Moudle -> Maven -> Next
②填写项目名,GroupId和Version,点击Finish 创建接口项目
③导入依赖
<!--接口层依赖--> <dependency> <groupId>com.agi</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--zookeeper依赖--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.10</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-client</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.1.0</version> </dependency> <!--dubbo依赖--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.3</version> </dependency> <!--servlet依赖--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--spring依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.2.RELEASE</version> </dependency>
④点击File -> Project Structure -> Moudles 创建webapp,WEB-INF和web.xml
⑤编写控制层代码
package com.agi.controller; import com.agi.api.IDemoService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/demo") public class DemoController { @Reference private IDemoService demoService; @RequestMapping("/getName") public String getName(){ return demoService.getName(); } }
⑥编写dubbo配置文件,在resource文件下创建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://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-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 定义服务消费方应用名 --> <dubbo:application name="dubbo-consumer"> <dubbo:parameter key="qos.port" value="22224"/> </dubbo:application> <!--注册中心地址--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!--指明使用的协议--> <dubbo:protocol name="dubbo" port="20881"/> <!--dubbo包扫描(@Refer)--> <dubbo:annotation package="com.agi.controller"/> <!--spring包扫描(@RequestMapping,@RestController...)--> <context:component-scan base-package="com.agi.controller"/> </beans>
⑦编写web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--springmvc配置--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:consumer.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
⑧配置artifacts,选择dubbo-consumer
⑨配置tomcat
大功告成
测试
启动Zookeeper
启动Provider
启动Consumer
浏览器测试